How to Add fragmnet In Activity programmatically in android?
In this blog we will explain Step by Step how to Add fragment in Activity . Show below
Step 1: create MainActivtiy class in src folder...
MainActivity.java
...................................................................................................................................................................
package com.androidbeginner.testcode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Add Fragment");
setContentView(R.layout.activity_main);
final LinearLayout frgmentss=(LinearLayout)findViewById(R.id.fragment);
Button btnfragmnet=(Button)findViewById(R.id.button1);
btnfragmnet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
TestFragmnet myFragment = new TestFragmnet();
fragmentTransaction.add(frgmentss.getId(), myFragment);
fragmentTransaction.commit();
}
});
}
}
...................................................................................................................................................................
Step 2: create activity_main xml class inside res/layout folder
activity_main.xml.
.................................................................................................................................................................
<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:orientation="vertical"
android:gravity="center"
android:id="@+id/fragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Fragment" />
</LinearLayout>
...................................................................................................................................................................
Step 3.create fragmnet view xml class inside res/layout folder.
fragmnet_layout.xml
...................................................................................................................................................................
<RelativeLayout 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:orientation="vertical"
android:background="#ffffff">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/logoweb" />
</RelativeLayout>
...................................................................................................................................................................
Step 4: Create fragement java class inside src folder .
TestFragmnet .java
...................................................................................................................................................................
package com.androidbeginner.testcode;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TestFragmnet extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_layout, container, false);
return myFragmentView;
}
}
.................................................................................................................................................................
0 comments