Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android — Add new tab when button click like Google Chrome new button?

In general, everything is working on Google Chrome. When the new tab button is clicked, a new tab is generated. In the same way, I want to add a new tab in the Android browser. How to do this — does anyone have any idea?

  1. First, is it possible in Android?

  2. If possible, how to do this?

    enter image description here

When I click on the + button, a new tab should be generated. How to do this?

like image 662
NagarjunaReddy Avatar asked Oct 27 '12 06:10

NagarjunaReddy


2 Answers

Ok Here is the code but it's an example only you may need to modify the code as per your requirement. I am giving you all the files code here hope you getting answer.

Your Manifest.xml file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.dynamictab"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BlankActivity"/>
    </application>

</manifest>

here is your activity_main.xml file for Tab activity

<?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"
        android:padding="5dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="none" 
                android:layout_weight="1">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="0dip"
                    android:layout_marginRight="0dip" />
            </HorizontalScrollView>
            <Button android:id="@+id/add_tab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:text="Add"/>
        </LinearLayout>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="2dp" />
    </LinearLayout>

</TabHost>

Put your TabWidget into the HorizontalScrollView so when more tabs are add it can scrolling and the Add but is out side of HorizontalScrollView. Every time you click on tab it will add new tab in TabWidget.

Here the code for tab_event.xml This layout will inflate and add into tab. You can change the style and its contain a single Button so you can add drawable image with text also.

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_event"
    android:clickable="true"
    android:focusable="true"
    android:text="Default Tab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Here is your MainActivity.java file

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
    private static int tabIndex = 0;
    private TabHost tabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();

        addTab();

        ((Button)findViewById(R.id.add_tab)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                tabIndex++;
                addTab();
            }
        });
    }
    private void addTab(){
        LayoutInflater layoutInflate = LayoutInflater.from(MainActivity.this);

        Button tabBtn = (Button)layoutInflate.inflate(R.layout.tab_event, null);
        tabBtn.setText("Tab "+tabIndex);
        Intent tabIntent = new Intent(MainActivity.this, BlankActivity.class);

        setupTab(tabBtn, tabIntent,"Tab "+tabIndex);
    }
    protected void setupTab(View tabBtn, Intent setClass,String tag) {
        TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabBtn).setContent(setClass);
        tabHost.addTab(setContent);
    }
}

And here is the BlankActivity.java file

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class BlankActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(BlankActivity.this);
        tv.setText("Blank activity");
        setContentView(tv);
    }
}
like image 148
Pratik Avatar answered Oct 13 '22 14:10

Pratik


For button click create a new tabspec.

TabSpeec spec = th.newTabSpec("tag");
spec.setContent(new TabHost.TabContentFactory(){
    //What ever thing you want to display inside the tab
    TextView text = new TextView(CONTEXT);
    text.setText("New tab");
    return(text);
    }
});
spec.setIndicator("New");
th.addTab(spec);

Use the following youtube tutorial as reference. Its simple.

http://www.youtube.com/watch?v=NcKSFlYEqYY

like image 20
Chamath Avatar answered Oct 13 '22 16:10

Chamath