i am very new to android development.
in my case i have one main activity and one fragment created.Main activity as two buttons.
when i click on button, my fragment should load .how can i achieve that?
Mainactivity .XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:id="@+id/fragment_button_2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:id="@+id/fragment_button_1"
android:layout_above="@+id/fragment_button_2"
android:layout_alignLeft="@+id/fragment_button_2"
android:layout_alignStart="@+id/fragment_button_2"
android:layout_marginBottom="33dp" />
</RelativeLayout>
Main activity .java
package com.bentgeorge.fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button fragment_btn_1;
private Button fragment_btn_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_btn_1 = (Button) findViewById(R.id.fragment_button_1);
fragment_btn_2 = (Button) findViewById(R.id.fragment_button_2);
}
@Override
public void onClick(View v) {
Fragment1 frag = new Fragment1();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainlayout,frag,"Test Fragment");
transaction.commit();
}
}
fragment_fragment1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bentgeorge.fragment.Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Fragment1.java
package com.bentgeorge.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
Not done much codes as i am confused with internet available tutorials.
Please help me to fix this
Thanks & Regards,
Update:
i have done some changes and my fragment is getting loaded now. ut the buttons are still visible . How can i hide the buttons
All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Fragment1) in that, and then call on your onClick(),
More info : here
use getActivity() from the fragment to access activity functions. If you want to access functions specific to your own activity class. Cast the acticity object you get first before using it see example below
Create one more activity say ExampleFragmentActivity.class, and include fragment tag in ExampleFragmentActivity's layout within Framelayout.
example_fragment_activity.xml(modified)
In Onclick method of your MainAcitivity.class
You can also include fragment dynamically using fragment manager.
in you onClick() method -
where R.id.fragment_frame - rootContainer Id rootView in MainActivity.java
you should use Framelayout for placing fragment in activity.
like below - activity_layout.xml -
and your activity - MainActivity.java
btw your fragment looks good. hope it will help you.
Updated: Please change your MainActivity as like this:
Change your activity_main.xml as like this:
Please create an xml fragment_main.xml:
Also create a fragment as MainFragment:
Hope this will helps you. If you had any queries, let me know.