Hey Guys, In this article we are going to discuss how to make fragment in android App
Fragment in android
Now go to main Activity.java and make two buttons in Activity
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" android:id="@+id/holder" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="150dp" android:layout_height="wrap_content" android:text="sign in" android:id="@+id/signIn" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" /> <Button android:layout_width="150dp" android:layout_height="wrap_content" android:text="sign up" android:id="@+id/signUp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="10dp" android:layout_marginRight="10dp"/> </RelativeLayout> </FrameLayout>
Now create two blank fragment and give it name SignInFragment and SignUpFragment
SignInFragment.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:background="#e2ed09" tools:context=".SignIn_Fragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Sign In" android:gravity="center" android:textColor="#000" android:textSize="40dp"/> </FrameLayout>
SignupFragment.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:background="#df1010" tools:context=".SignUp_Fragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Sign Up" android:textColor="#fff" android:gravity="center" android:textSize="40dp"/> </FrameLayout>
Now go to MainActivity.java and copy paste this code
MainActivity.java
package com.thecodingshef.fragmentdemo; import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn_SignIn,btn_SignUp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_SignIn=findViewById(R.id.signIn); btn_SignUp=findViewById(R.id.signUp); btn_SignIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { android.support.v4.app.FragmentManager fm=getSupportFragmentManager(); fm.beginTransaction().replace(R.id.holder,new SignIn_Fragment()).addToBackStack(null).commit(); } }); btn_SignUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { android.support.v4.app.FragmentManager fm=getSupportFragmentManager(); fm.beginTransaction().replace(R.id.holder,new SignUp_Fragment()).addToBackStack(null).commit(); } }); } }
Now our code is complete, and we have used fragment in our app successfully.