Monday 1 April 2013

tab activity with view group organizer

Create  class HomeActivity.java
====================

public class HomeActivity extends  android.app.TabActivity implements OnTabChangeListener {
    private TabHost tabHost = null;
    private TextView txtHome  = null,txtDoneer = null,txtDelen= null,txtOver = null;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tabs);
       
        tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, ViewGroupOrganizer.class);
        txtHome = new TextView(this);
        txtHome.setTypeface(CommanClass.SetCustomFont(getResources()));
        txtHome.setText(Constant.HOME_TAB_TEXT);
        txtHome.setTextColor(getResources().getColor(R.color.White));
        txtHome.setBackgroundResource(R.drawable.img_tab_home_selected);
        txtHome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtHome.setGravity(Gravity.CENTER);
        spec = tabHost.newTabSpec(Constant.HOME_TAB_TEXT).setIndicator(txtHome).setContent(intent);
        tabHost.addTab(spec);
       
        txtDelen = new TextView(this);
        txtDelen.setTypeface(CommanClass.SetCustomFont(getResources()));
        txtDelen.setText(Constant.DELEN_TAB_TEXT);
        txtDelen.setTextColor(getResources().getColor(R.color.Black));
        txtDelen.setBackgroundResource(R.drawable.img_tab_doneer);
        txtDelen.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtDelen.setGravity(Gravity.CENTER);
        spec = tabHost.newTabSpec(Constant.DELEN_TAB_TEXT).setIndicator(txtDelen).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Doneer.class);
        txtDoneer = new TextView(this);
        txtDoneer.setText(Constant.DONEER_TAB_TEXT);
        txtDoneer.setTypeface(CommanClass.SetCustomFont(getResources()));
        txtDoneer.setTextColor(getResources().getColor(R.color.Black));
        txtDoneer.setBackgroundResource(R.drawable.img_tab_doneer);
        txtDoneer.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtDoneer.setGravity(Gravity.CENTER);
        spec = tabHost.newTabSpec(Constant.DONEER_TAB_TEXT).setIndicator(txtDoneer).setContent(intent);
        tabHost.addTab(spec);
       
        intent = new Intent().setClass(this, Over.class);
        txtOver = new TextView(this);
        txtOver.setTypeface(CommanClass.SetCustomFont(getResources()));
        txtOver.setText(Constant.OVER_TAB_TEXT);
        txtOver.setTextColor(getResources().getColor(R.color.Black));
        txtOver.setBackgroundResource(R.drawable.img_tab_over);
        txtOver.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtOver.setGravity(Gravity.CENTER);
        spec = tabHost.newTabSpec(Constant.OVER_TAB_TEXT).setIndicator(txtOver).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
   
        tabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(tabHost.getCurrentTab()==0)
                {
                    while(true)
                    {
                        if (ViewGroupOrganizer.history.size() > 1)
                        {
                            ViewGroupOrganizer.history.remove(ViewGroupOrganizer.history.size() - 1);
                            ViewGroupOrganizer.group.setContentView(ViewGroupOrganizer.history.get(ViewGroupOrganizer.history.size() - 1));
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    tabHost.setCurrentTab(0);
                }
            }
        });
       
        tabHost.getTabWidget().getChildAt(1).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ShareAlertdialog();
            }
        });
        tabHost.setOnTabChangedListener(this);
    }
   
//***********************************************************************************************************************
/**
 * @author Manisha
 * @param none
 * @return void
 * @use This Dialog is used for show share dialog
 */ 
    public void ShareAlertdialog() {
        final CharSequence[] items = { Constant.FACEBOOK, Constant.TWITTER};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(Constant.SHARE_ALERT_DIALOG_TITLE);
        builder.setNegativeButton(Constant.SHARE_CANCEL_BUTTON, null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    if(CommanClass.CheckNetwork(HomeActivity.this)){
                         Intent mIntent = new Intent(HomeActivity.this,Share_On_Facebook.class);
                         mIntent.setAction(Constant.FACEBOOK_ACTION);
                         startActivity(mIntent);   
                    }
                } else if(item == 1) {
                    if(CommanClass.CheckNetwork(HomeActivity.this)){
                        Intent mIntent = new Intent(HomeActivity.this,Share_On_Twitter.class);
                        mIntent.setAction("");
                        startActivity(mIntent);   
                    }
                }
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
   
        @Override
    public void onTabChanged(String tabId) {
        if(tabId.equals(Constant.HOME_TAB_TEXT)){
            txtHome.setBackgroundResource(R.drawable.img_tab_home_selected);
            txtHome.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtHome.setTextColor(getResources().getColor(R.color.White));
        }
        else{
            txtHome.setBackgroundResource(R.drawable.img_tab_home);
            txtHome.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtHome.setTextColor(getResources().getColor(R.color.Black));
        }
        if(tabId.equals(Constant.DONEER_TAB_TEXT)){
            txtDoneer.setBackgroundResource(R.drawable.img_tab_doneer_selected);
            txtDoneer.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtDoneer.setTextColor(getResources().getColor(R.color.White));
        }
        else{
            txtDoneer.setBackgroundResource(R.drawable.img_tab_doneer);
            txtDoneer.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtDoneer.setTextColor(getResources().getColor(R.color.Black));
        }
        if(tabId.equals(Constant.DELEN_TAB_TEXT)){
            txtDelen.setBackgroundResource(R.drawable.img_tab_doneer_selected);
            txtDelen.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtDelen.setTextColor(getResources().getColor(R.color.White));
        }
        else{
            txtDelen.setBackgroundResource(R.drawable.img_tab_doneer);
            txtDelen.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtDelen.setTextColor(getResources().getColor(R.color.Black));
        }
        if(tabId.equals(Constant.OVER_TAB_TEXT)){
            txtOver.setBackgroundResource(R.drawable.img_tab_over_selected);
            txtOver.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtOver.setTextColor(getResources().getColor(R.color.White));
        }
        else{
            txtOver.setBackgroundResource(R.drawable.img_tab_over);
            txtOver.setTypeface(CommanClass.SetCustomFont(getResources()));
            txtOver.setTextColor(getResources().getColor(R.color.Black));
        }
    }
}


Create a file named tabs.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
           />
    </LinearLayout>

</TabHost>

//Create a class view group organizer.class
=============================
public class ViewGroupOrganizer extends ActivityGroup {
    private static final String MAINACTIVITY="mainactivity";
    // Keep this in a static variable to make it accessible for all the nested
    // activities, lets them manipulate the view
    public static ViewGroupOrganizer group;

    // Need to keep track of the history if you want the back-button to work
    // properly, don't use this if your activities requires a lot of memory.
    public static ArrayList<View> history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        group = this;
        this.history = new ArrayList<View>();
        // Start the root activity within the group and get its view
        View view = getLocalActivityManager().startActivity(MAINACTIVITY,new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        // Replace the view of this ActivityGroup
        replaceView(view);
    }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);
    }

    public void back()
    {
        if (history.size() > 1)
        {
            history.remove(history.size() - 1);
            setContentView(history.get(history.size() - 1));
        }
        else
        {
            finish();
        }
    }
    public void onBackPressed() {
        ViewGroupOrganizer.group.back();
        return;
    }
}


create class mainActivity.class
=================

if(v==btnEdit){
            Intent mIntentEdit = new Intent(MainActivity.this,EditAlarm.class);
            View view = ViewGroupOrganizer.group.getLocalActivityManager().startActivity(EDITALARM,mIntentEdit.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
            ViewGroupOrganizer.group.replaceView(view);
        }

==============================================
From EditAlarm.java while want to back to main activity

 btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewGroupOrganizer.group.back();
            }
        });

No comments:

Post a Comment