We provide complete mobile and web apps development solutions

Showing posts with label fragments in android. Show all posts
Showing posts with label fragments in android. Show all posts

Sunday, February 3, 2013

Tabs like with ActionBar


 TabActivity is depreciated  in Api 13. Tabs are presented in latest versions with ActionBar.
ActionBar.newTab().

So, prefer to not to use depreciated classes and methods. So use latest classes and methods.

Here is the simple example of creating Tab like activity using fragments.



 MobEngineersActivity.java

package com.srinivas.mobengineers;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.os.Bundle;
import android.widget.Toast;


public class MobEngineersActivity extends Activity {
    public static Context appContext;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();

 
        ActionBar actionbar = getActionBar();       // initilize actionBar
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);     // set navigation mode
       
        ActionBar.Tab Category1Tab = actionbar.newTab().setText("FOOD");
        ActionBar.Tab Category2Tab = actionbar.newTab().setText("BEVERAGES");
       
        Fragment FoodFragment = new FoodFragment();
        Fragment BeveragesFragment = new BeveragesFragment();
       CategoryTab.setTabListener(new MyTabsListener(FoodFragment));
      BeveragesTab.setTabListener(new MyTabsListener(BeveragesFragment));

        actionbar.addTab(FoodTab);
        actionbar.addTab(BeveragesTab);
    }
Food.java

public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

Beverages.java
public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

 </LinearLayout>
</LinearLayout>



Monday, November 19, 2012

Fragments in android

Fragments represent behavior in particular portion in an activity.
Fragments must embedded in an activity and they cannot run independently without activity.
fragments having its own life cycle like activity life cycle.
To create fragement, we have to create a sub class of fragment.
Fragments introduced from android 3.0(HoneyComb) onwards
It contains callback methods similar to an activity such as   onCreate(), onStart(),onPause() and onStop().  


Fragment Lifecycle:

onCreate()    -     system calls when creating a fragment.Initialize components here.
onCreateView()-     system calls when the fragment to draw its user interface for 1st time.
onPause()     -     system calls when the user is leaving the fragment.
onAttach()    -     it is called when the fragment is associated with activity.
onActivityCreated()- it is called when the activity's onCreate() is returned.
onDestroyView()    - it is called when view hierarchy associated with the fragment is removed.
onDetach()         -it is called when the fragment is detached from the activity.

Apart from this we have several call back methods.

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts