Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Fragment Example in Android:-



The fragment is a predefined class in android, if we want to divide the screen into multipart and subpart then we can use Fragment class.


When we add a fragment then it will create a fragment class file and XML file.

Fragment class will load under activity and a design file is used to design the user interface using Fragment.


Example of Fragment using <fragment> tag:-


package com.example.customdrawer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}



package com.example.customdrawer;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
* Use the {@link SecondFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class SecondFragment extends Fragment {

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public SecondFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SecondFragment.
*/
// TODO: Rename and change types and number of parameters
public static SecondFragment newInstance(String param1, String param2) {
SecondFragment fragment = new SecondFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
}



package com.example.customdrawer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}



Example2 of Fragment:-

I am explaining to call Fragment class using Button Click.

package com.example.customdrawer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {

Button btnone;
Button btntwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnone = findViewById(R.id.btnfrg1);
btntwo = findViewById(R.id.btnfrg2);
btnone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.frgcontainer,new FirstFragment()).commit();
}
});

btntwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction().replace(R.id.frgcontainer,new SecondFragment()).commit();
}
});
}
}


<?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="#03A9F4"
tools:context=".FirstFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</FrameLayout>

<?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="#C81515"
tools:context=".SecondFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</FrameLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:background="#009688"
tools:context=".MainActivity2">

<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="#2196F3"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnfrg1"
android:text="First"
></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnfrg2"
android:text="Second"
></Button>
</LinearLayout>

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/frgcontainer"
android:background="#009688"
>

</FrameLayout>
</LinearLayout>

How to write Java Code on Fragment:-

We should always write code on onCreateView() 

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
btn = v.findViewById(R.id.btnfragmentclick);
txtv = v.findViewById(R.id.txtres3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtv.setText("Welcome in Fragment");
}
});
return v;
}


Code for Design View:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#03A9F4"
android:orientation="vertical"
tools:context=".FirstFragment">

<!-- TODO: Update blank fragment layout -->

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnfragmentclick"
android:text="CLICK"
>

</Button>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:id="@+id/txtres3"

/>

</LinearLayout>








Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post