Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Your Manifest.xml file</p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.dynamictab" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name=".MainActivity"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".BlankActivity"/&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>here is your activity_main.xml file for Tab activity</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none" android:layout_weight="1"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="0dip" android:layout_marginRight="0dip" /&gt; &lt;/HorizontalScrollView&gt; &lt;Button android:id="@+id/add_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.1" android:text="Add"/&gt; &lt;/LinearLayout&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="2dp" /&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>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.</p> <p>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.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" /&gt; </code></pre> <p>Here is your MainActivity.java file</p> <pre><code>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); } } </code></pre> <p>And here is the BlankActivity.java file</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload