Wednesday, 31 July 2013

Simple Fragment Example in Android

This post will help you to create simple Fragment in android.

MainActivity.java

package com.simple.sfragmentexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 int i = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.fragment_simple);
  if (savedInstanceState == null) {
   Fragment newFragment = SimpleAddition.newInstance(i);
   FragmentTransaction ft = getFragmentManager().beginTransaction();
   ft.add(R.id.FrameLayout1, newFragment).commit();
  } else {
   i = savedInstanceState.getInt("level");
  }
 }

 @Override
 public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putInt("level", i);
 }

 public static class SimpleAddition extends Fragment {
  int mNum;

  static SimpleAddition newInstance(int num) {
   SimpleAddition f = new SimpleAddition();

   Bundle args = new Bundle();
   args.putInt("num", num);
   f.setArguments(args);

   return f;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   mNum = getArguments() != null ? getArguments().getInt("num") : 1;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View v = inflater.inflate(R.layout.activity_main, container, false);
   
   Button b = (Button) v.findViewById(R.id.button1);
   final EditText et1 = (EditText) v.findViewById(R.id.editText1);   
   final TextView tv = (TextView) v.findViewById(R.id.textView1);

   b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {        
     tv.setText(et1.getText().toString());
    }
   });   
   return v;
  }
 }

}


fragment_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip" >

    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </FrameLayout>

</LinearLayout>


activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="28dp"
        android:text="Button" />

</RelativeLayout>


Screen Shot:



1. Simple Fragments with Action Bar Example in Android

2. ListFragments in Android with simple example

3. Simple ListFragment Example in Android

I hope this post is useful to you. kindly share your feedback as comment here.



Thank You