-1

I am working on a project, where i need to show different views in a single window by selecting different tabs from the top.

I have googled this topic a lot, but i dont know what exact terminology is used for this view, so finding it difficult to search it. I am working on a view just like this,

Tab Activities

I have gone through several examples like this

and this

but i could not get desired answer.

Any usefull link that could direct me to implement this view would be helpfull.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100

2 Answers2

2

tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#0F5EC6"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:gravity="center"
            android:text="ThirdEye"
            android:textSize="20dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:gravity="center"
            android:text="Logout" />
    </LinearLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
</LinearLayout>

tabactivity

public class PostTabActivity extends TabActivity implements TabHost.OnTabChangeListener   {
private TabHost mTabHost;
Button back,logout;
private String sessid,Name;
double lati,longi;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
       sessid = extras.getString("sid"); 
       lati = extras.getDouble("EXTRA_latitude"); 
       longi = extras.getDouble("EXTRA_longitude");
       Name= extras.getString("NICK_NAME");
       System.out.println("........................"+lati+".................."+longi);
    } 
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.tab_host);
    logout = (Button) findViewById(R.id.logout);
    logout.setOnTouchListener(new OnTouchListener()
    {

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
            new Logout(PostTabActivity.this,sessid);
                //finish();
                break;  
            }
            return true;
        }
    });
    back= (Button) findViewById(R.id.back);
    back.setOnTouchListener(new OnTouchListener()
    {

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                Intent myIntent = new Intent(PostTabActivity.this, GetPost.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                myIntent.putExtra("EXTRA_SESSION_ID", sessid);
                myIntent.putExtra("EXTRA_latitude",lati);
                myIntent.putExtra("EXTRA_longitude", longi);
                myIntent.putExtra("NICK_NAME", Name);
                startActivity(myIntent);
                finish();
                break;  
            }
            return true;
        }


    }
        );


    mTabHost = getTabHost();
    mTabHost.setOnTabChangedListener(this);

    /*Tab One */

    Intent mIntent= new Intent("proto.thirdeye.PostText");
    mIntent.setClass(this, PostText.class);
    mIntent.putExtra("sid",sessid);
    mIntent.putExtra("EXTRA_latitude",lati);
    mIntent.putExtra("EXTRA_longitude", longi);
    mIntent.putExtra("NICK_NAME", Name);

    mTabHost.addTab(mTabHost.newTabSpec("text1")
            .setIndicator("Text",
                    getResources().getDrawable(R.drawable.image))
            .setContent(mIntent));

    /*Tab Two */

    Intent mIntent1 = new Intent("proto.thirdeye.PostImage");
    mIntent1.setClass(this, PostImage.class);
    mIntent1.putExtra("sid",sessid);
    mIntent1.putExtra("EXTRA_latitude",lati);
    mIntent1.putExtra("EXTRA_longitude", longi);
    mIntent1.putExtra("NICK_NAME", Name);
    mTabHost.addTab(mTabHost.newTabSpec("image1")
            .setIndicator("Image",
                    getResources().getDrawable(R.drawable.image))
            .setContent(mIntent1));


    /*Tab Three */

    Intent mIntent2 = new Intent("proto.thirdeye.PostVideo");
    mIntent2.setClass(this, PostVideo.class);
    mIntent2.putExtra("sid",sessid);
    mIntent2.putExtra("EXTRA_latitude",lati);
    mIntent2.putExtra("EXTRA_longitude", longi);
    mIntent2.putExtra("NICK_NAME", Name);
    mTabHost.addTab(mTabHost.newTabSpec("video")
            .setIndicator("Video",
                    getResources().getDrawable(R.drawable.image))
            .setContent(mIntent2));

    mTabHost.setCurrentTab(0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();

    }

    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    Intent myIntent = new Intent(PostTabActivity.this, GetPost.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    myIntent.putExtra("EXTRA_SESSION_ID", sessid);
    myIntent.putExtra("EXTRA_latitude",lati);
    myIntent.putExtra("EXTRA_longitude", longi);
    myIntent.putExtra("NICK_NAME", Name);
    startActivity(myIntent);
    finish();
    return;
}
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    Log.i("Aru","                    "+tabId);
    Activity activity = getLocalActivityManager().getActivity(tabId);
    if (activity != null) {
        activity.onWindowFocusChanged(true);
    }
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("sid",sessid);
  savedInstanceState.putDouble("EXTRA_latitude",lati);
  savedInstanceState.putDouble("EXTRA_longitude",longi);
  savedInstanceState.putString("NICK_NAME", Name);
  // etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  sessid = savedInstanceState.getString("sid");
  lati = savedInstanceState.getDouble("EXTRA_latitude");
  longi = savedInstanceState.getDouble("EXTRA_longitude");
  Name = savedInstanceState.getString("NICK_NAME");
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //MyApplication.activityPaused();
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //MyApplication.activityResumed();
}
 }

You can define your own layout for PostImage,PostVideo,PostText. Customize this according to your needs.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

There are lot of types of TabHost in android. You can use that by default one or you can customize it with whatever you required with it.

Simply have a look at this example. It will provide you the default one of android tabHost Take a look at below code which will provide a TabHost as per your needs -

Main.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:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

And, take a look at here also.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173