Sunday 11 August 2013

tab implementation in android

//create xml tab.xml
****************

xml version="1.0"
encoding="utf-8"?>
<
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" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</TabHost>

//Create java file HomeActivity.java
****************************
package com.mrs.abd;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

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.1_TAB_TEXT).setIndicator(txtHome).setContent(intent);
  tabHost.addTab(spec);

  txtDe = new TextView(this);
  txtDe.setTypeface(CommanClass.SetCustomFont(getResources()));
  txtDe.setText(Constant.2_TAB_TEXT);
  txtDe.setTextColor(getResources().getColor(R.color.Black));
  txtDe.setBackgroundResource(R.drawable.img_tab_doneer);
  txtDe.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
  txtDe.setGravity(Gravity.CENTER);
  spec = tabHost.newTabSpec(Constant.3_TAB_TEXT).setIndicator(txtDelen).setContent(intent);
  tabHost.addTab(spec);

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

  intent = new Intent().setClass(this, Over.class);
  txtO = new TextView(this);
  txtO.setTypeface(CommanClass.SetCustomFont(getResources()));
  txtO.setText(Constant.3_TAB_TEXT);
  txtO.setTextColor(getResources().getColor(R.color.Black));
  txtO.setBackgroundResource(R.drawable.img_tab_over);
  txtO.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
  txtO.setGravity(Gravity.CENTER);
  spec = tabHost.newTabSpec(Constant.3_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)){
   txtDo.setBackgroundResource(R.drawable.img_tab_doneer_selected);
   txtDo.setTypeface(CommanClass.SetCustomFont(getResources()));
   txtDo.setTextColor(getResources().getColor(R.color.White));
  }
  else{
   txtDo.setBackgroundResource(R.drawable.img_tab_doneer);
   txtDo.setTypeface(CommanClass.SetCustomFont(getResources()));
   txtDo.setTextColor(getResources().getColor(R.color.Black));
  }
  if(tabId.equals(Constant.4TAB_TEXT)){
   txt4.setBackgroundResource(R.drawable.img_tab_doneer_selected);
   txt4.setTypeface(CommanClass.SetCustomFont(getResources()));
   txt4.setTextColor(getResources().getColor(R.color.White));
  }
  else{
   txt4setBackgroundResource(R.drawable.img_tab_doneer);
   txt4setTypeface(CommanClass.SetCustomFont(getResources()));
   txt4setTextColor(getResources().getColor(R.color.Black));
  }
  if(tabId.equals(Constant.4TAB_TEXT)){
   txt4setBackgroundResource(R.drawable.img_tab_4selected);
   txt.setTypeface(CommanClass.SetCustomFont(getResources()));
   txt4setTextColor(getResources().getColor(R.color.White));
  }
  else{
   txt4setBackgroundResource(R.drawable.img_4over);
   txt4setTypeface(CommanClass.SetCustomFont(getResources()));
   txt4setTextColor(getResources().getColor(R.color.Black));
  }
 }
}


//code for Give space in tab 
TabWidget widget = getTabWidget();
        final int tabChildrenCount = widget.getChildCount();
        LinearLayout.LayoutParams currentLayout;
        for (int i_tab = 0; i_tab < tabChildrenCount; i_tab++) {
            currentLayout = (LinearLayout.LayoutParams) widget
                    .getChildAt(i_tab).getLayoutParams();
            if (i_tab == 0) {
                currentLayout.setMargins(10, 0, 5, 0);
            } else if (i_tab == (tabChildrenCount - 1)) {
                currentLayout.setMargins(5, 0, 10, 0);
            } else {
                currentLayout.setMargins(5, 0, 5, 0);
            }
            currentLayout.height = 40;
        }
        widget.requestLayout();

No comments:

Post a Comment