Tuesday 29 November 2011

Fragments in andriod

public class FragmentTestActivity extends Activity implements OnItemClickListener {
     Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ListView l = (ListView) findViewById(R.id.number_list);
        ArrayAdapter numbers = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1,
                new String [] {
            "one", "two", "three", "four", "five", "six", "seven","eight", "nine", "ten", "eleven", "twelve"  });
l.setAdapter(numbers);
        l.setOnItemClickListener(this);
    }   
    /**
     * Add a Fragment to our stack with n Androids in it
     */
    private void stackAFragment(int nAndroids) {
        Fragment f = new TestFragment(nAndroids);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.the_frag, f);
/*the frag is the name of fragment */
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }

    /**
     * Called when a number gets clicked
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        stackAFragment(position + 1);
    }
}




/*   TestFragment.java class  */


public class TestFragment extends Fragment {
    private int nAndroids;
   
    public TestFragment() {
       
    }

   /**
    * Constructor for being created explicitly
    */
   public TestFragment(int nAndroids) {
           this.nAndroids = nAndroids;
    }

    /**
     * If we are being created with saved state, restore our state
     */
    @Override
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        if (null != saved) {
            nAndroids = saved.getInt("nAndroids");
        }
    }
   
    /**
     * Save the number of Androids to be displayed
     */
    @Override
    public void onSaveInstanceState(Bundle toSave) {
        toSave.putInt("nAndroids", nAndroids);
    }

    /**
     * Make a grid and fill it with n Androids
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
        int n;
/* here we can any widget like button, editext , layout, etc    */
        Context c = getActivity().getApplicationContext();
        LinearLayout l = new LinearLayout(c);
        Button b=new Button(getActivity());
        l.addView(b);
        b.setPadding(0, 100,0,100);
        b.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "karan", 20000).show();
            }
        });
      
        for (n = 0; n < nAndroids; n++) {
            ImageView i = new ImageView(c);
            i.setImageResource(R.drawable.android);
            l.addView(i);

/* padding is use to set the ui 
here 100 is use to masgin from top of the image view*/ 

            i.setPadding(0, 100, 0,0);
        }
        return l;
    }
}



/*     main.xml         */

          <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frags">

    <ListView
            android:id="@+id/number_list"
            android:layout_width="250dip"
            android:layout_height="match_parent" />
/*com.example is packafe name of project   
fragmenttest   is class name of this  fragment
testfragment is function name of this fragment  */
    <fragment class="com.example.fragmenttest.TestFragment"
            android:id="@+id/the_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            />

</LinearLayout>



No comments:

Post a Comment