Hello friend,
This is our fragment demo.
Fragment is use into the activity.We can set multiple fragment in our android activity and using some method we can change fragment as per our requirement.
Let's understand with example so we can get perfect meaning of fragment.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.spider.fragment_activity.MainActivity">
<Button android:id="@+id/one" android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_marginTop="10dp"
android:gravity="center" android:text="Fragment One" />
<Button android:id="@+id/two" android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_below="@id/one" android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" android:text="Fragment Two" />
<TextView android:id="@+id/txt" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/two"
android:layout_centerHorizontal="true" android:layout_marginTop="20dp"
android:gravity="center" android:text="Hello World!" android:textSize="20dp" />
<FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_below="@id/txt"
android:layout_marginTop="20dp"></FrameLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one, two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentOne fragmentOne = new FragmentOne();
changeFragment(fragmentOne);
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTwo fragmentTwo = new FragmentTwo();
changeFragment(fragmentTwo);
}
});
}
public void changeFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment, fragment.getClass().getName());
fragmentTransaction.commitAllowingStateLoss();
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/colorAccent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerVertical="true"
android:text="Fragment One" android:textSize="50dp" />
</RelativeLayout>
FragmentOne.java
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerVertical="true"
android:text="Fragment Two" android:textSize="50dp" />
</RelativeLayout>
FragmentTwo.java
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
This is our fragment demo.
Fragment is use into the activity.We can set multiple fragment in our android activity and using some method we can change fragment as per our requirement.
Let's understand with example so we can get perfect meaning of fragment.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.spider.fragment_activity.MainActivity">
<Button android:id="@+id/one" android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_marginTop="10dp"
android:gravity="center" android:text="Fragment One" />
<Button android:id="@+id/two" android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_below="@id/one" android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" android:text="Fragment Two" />
<TextView android:id="@+id/txt" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/two"
android:layout_centerHorizontal="true" android:layout_marginTop="20dp"
android:gravity="center" android:text="Hello World!" android:textSize="20dp" />
<FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_below="@id/txt"
android:layout_marginTop="20dp"></FrameLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one, two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentOne fragmentOne = new FragmentOne();
changeFragment(fragmentOne);
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTwo fragmentTwo = new FragmentTwo();
changeFragment(fragmentTwo);
}
});
}
public void changeFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment, fragment.getClass().getName());
fragmentTransaction.commitAllowingStateLoss();
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/colorAccent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerVertical="true"
android:text="Fragment One" android:textSize="50dp" />
</RelativeLayout>
FragmentOne.java
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerVertical="true"
android:text="Fragment Two" android:textSize="50dp" />
</RelativeLayout>
FragmentTwo.java
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
OUTPUT:-
Comments
Post a Comment