Tuesday, 24 June 2014

[KEY : ANDROID CUSTOM CHECKBOX, CUSTOM CHECKBOX]

Custom Checkbox Android

How to create custom checkbox. Applying stylish checkbox Android

  1. Take two images for both states checked and unchecked. In png format.
  2. Create checkbox in xml.
  3. Create new xml file assign appropriate name like "checkbox_button.xml"

    <?xml version="1.0" encoding="utf-8"?>
    <selector 
    xmlns:android="http://schemas.android.com/apk/res/android">
      <!--checked image-->
        <item android:drawable="@drawable/checkbox_checked" 
        android:state_checked="true"/>
      <!--unchecked image-->
        <item android:drawable="@drawable/checkbox" 
        android:state_checked="false"/>
    
    </selector>

  4. Add button property in check box in xml

    android:button="@drawable/checkbox_button"


Monday, 23 June 2014

[KEY : ANDROID FRAGMENT, ACTIVITY, ADD FRAGMENT TO ACTIVITY]

Add Fragment to Activity

  1. Create simple activity with xml.
  2. Go to xml file add framelayout and assign id to it as follow:

     <FrameLayout
            android:id="@+id/your_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />

  3. Create java class with name like "MyFirstFragment.java" and extend fragment.
  4. Go to activity add the following code :

    Fragment myFragment = new MyFirstFragment();
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fTransaction = fragmentManager
       .beginTransaction()
       .replace(R.id.your_frame_layout, bodyFragment)
       .addToBackStack("myprofile");
       
     fTransaction.commit();
       


Saturday, 21 June 2014

[KEY : FRAGMENT, ADDING FRAGMENT, ANDROID FRAGMENT, CREATING FRAGMENT]

FRAGMENT, ANDROID

Android activity is simple and best way to provide interface to user. Fragment are very similar to activity. But it will be much better if we call it small piece of activity. So an activity can have multiple fragments

Creating fragment in android

  1. Create simple class like "MyFirstFragment.java".
  2. Extend Fragment class into "MyFirstFragment.java".
  3. Fragement is almost ready. Now add required methods into it, Specifically you require two methods onCreateView() and onAttach() as mentioned below :

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); }


    @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); }

  4. Inflate your own xml file and return it as view. So finally onCreateView() will look like as below:

    @Override
     public View onCreateView(LayoutInflater inflater, 
       ViewGroup container, Bundle savedInstanceState) {
      
      View localView = inflater.inflate(R.layout.your_xml.xml, 
        container, false);
      
      return localView; //super.onCreateView(inflater, container, savedInstanceState);
     }
    

  5. Create global object of your parent activity

    YourActivity yourActivityObject;

  6. Initialize yourActivityObject in onAttach method so it will look as below:

     @Override
     public void onAttach(Activity activity) {
    
      yourActivityObject = (YourActivity)activity;
    
      super.onAttach(activity);
     }
    

Start / Call new fragment from fragment

 Fragment yourSecondFragment = new YourSecondFragment();

 FragmentManager fm = yourActivityObject.getSupportFragmentManager();

 //yourSecondFragment.setArguments(Bundle) // to pass argument to next fragment.

 fm.beginTransaction().replace(R.id.parent_activity_frame_layout, 
 yourSecondFragment).addToBackStack("myfragmentstring").commit();

 // addToBackStack method before commit is used to implement back button functionality later.  

Adding back button functionality to fragment

In parent activity add onBackPressed() method and paste the following code

int f = getSupportFragmentManager().getBackStackEntryCount();
  Log.v(tag, "onBackPressed ["+f+"]");
  if (f <= 1)
   this.finish();
  else {
   getSupportFragmentManager().popBackStackImmediate();
  }