Translate

Tuesday, 22 March 2016

Android Fragment Example

Let's have a look at the simple example of android fragment.

activity_main.xml

File: activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" >  
  4.   
  5.     <fragment  
  6.         android:id="@+id/fragment2"  
  7.         android:name="com.example.fragmentexample.Fragment2"  
  8.         android:layout_width="0px"  
  9.         android:layout_height="match_parent"   
  10.         android:layout_weight="1"  
  11.         />  
  12.   
  13.     <fragment  
  14.         android:id="@+id/fragment1"  
  15.         android:name="com.example.fragmentexample.Fragment1"  
  16.         android:layout_width="0px"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_weight="1"  
  19.          />  
  20.   
  21. </LinearLayout>  
File: fragment1.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#00ff00"  
  7.      >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="fragment frist"  
  14.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  15.   
  16. </LinearLayout>  
File: fragment2.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#0000ff"  
  7.      >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Second Fragment"  
  14.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  15.   
  16. </LinearLayout>  

MainActivity class

File: MainActivity.java
  1. package com.example.fragmentexample;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. public class MainActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13. }  

File: Fragment1.java
  1. package com.example.fragmentexample;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class Fragment1 extends Fragment {  
  10.     @Override  
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  12.             Bundle savedInstanceState) {  
  13.         // TODO Auto-generated method stub  
  14.         return inflater.inflate(R.layout.fragment1,container, false);  
  15.     }  
  16.   
  17. }  

File: Fragment2.java
  1. package com.example.fragmentexample;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class Fragment2 extends Fragment {  
  10.       
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  12.             Bundle savedInstanceState) {  
  13.         // TODO Auto-generated method stub  
  14.         return inflater.inflate(R.layout.fragment2,container, false);  
  15.     }  
  16.   
  17. }  


Output:

android fragment

No comments:

Post a Comment