Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of buttons at bottom of the screen, use TabHost and ActivityGroup to display multiple activities under the selected tab.</p> <pre><code>&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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>Main activity:</p> <pre><code>/** * Activity that displays the main tabs and manages separate activity for the * selected tab. */ public class MainActivity extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load main view setContentView(R.layout.main_activity); // get the TabHost TabHost tabHost = getTabHost(); // resusable TabSpec for each tab TabHost.TabSpec spec; // reusable Intent for each tab Intent intent; // first tab intent = new Intent(this, TabGroupActivity.class); spec = tabHost.newTabSpec("first") .setIndicator(createTabIndicatorView(tabHost, "First", R.drawable.ic_tab_first)) .setContent(intent); tabHost.addTab(spec); // second tab intent = new Intent(this, TabGroupActivity.class); spec = tabHost.newTabSpec("second") .setIndicator(createTabIndicatorView(tabHost, "Second", R.drawable.ic_tab_second)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(0); } /** * Creates tabs with custom layout. * * @param tabHost the tab host * @param tabTitle the title of the tab * @param icon the icon of the tab * @return the view representing single tab */ private View createTabIndicatorView(TabHost tabHost, CharSequence tabTitle, int icon) { LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost, false); final TextView tv = (TextView) tabIndicator.findViewById(R.id.title); tv.setText(tabTitle); final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); iconView.setImageDrawable(getResources().getDrawable(icon)); return tabIndicator; } } </code></pre> <p>The activty group:</p> <pre><code>/** * The purpose of this Activity is to manage the activities in a tab. Note: * Child Activities can handle Key Presses before they are seen here. */ public class TabGroupActivity extends ActivityGroup { private ArrayList&lt;String&gt; mIdList; public TabGroupActivity() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (mIdList == null) { mIdList = new ArrayList&lt;String&gt;(); } startChildActivity("firstChildActivity", new Intent(this, FirstChildActivity.class)); } /** * This is called when a child activity of this one calls its finish method. * This implementation calls {@link LocalActivityManager#destroyActivity} on * the child activity and starts the previous activity. If the last child * activity just called finish(),this activity (the parent), calls finish to * finish the entire group. */ @Override public void finishFromChild(Activity child) { LocalActivityManager manager = getLocalActivityManager(); int index = mIdList.size() - 1; if (index &lt; 1) { finish(); return; } manager.destroyActivity(mIdList.get(index), true); mIdList.remove(index); index--; String lastId = mIdList.get(index); Intent lastIntent = manager.getActivity(lastId).getIntent(); Window newWindow = manager.startActivity(lastId, lastIntent); setContentView(newWindow.getDecorView()); } /** * Starts an Activity as a child Activity to this. * * @param Id Unique identifier of the activity to be started. * @param intent The Intent describing the activity to be started. * @throws android.content.ActivityNotFoundException. */ public void startChildActivity(String Id, Intent intent) { Window window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); if (window != null) { mIdList.add(Id); setContentView(window.getDecorView()); } } /** * The primary purpose is to prevent systems before * android.os.Build.VERSION_CODES.ECLAIR from calling their default * KeyEvent.KEYCODE_BACK during onKeyDown. */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // preventing default implementation previous to // android.os.Build.VERSION_CODES.ECLAIR return true; } return super.onKeyDown(keyCode, event); } /** * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that all * systems call onBackPressed(). */ @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { onBackPressed(); return true; } return super.onKeyUp(keyCode, event); } /** * If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and add * this method. */ @Override public void onBackPressed() { int length = mIdList.size(); if (length &gt; 1) { Activity current = getLocalActivityManager().getActivity(mIdList.get(length - 1)); current.finish(); } else { super.onBackPressed(); } } } </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. 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.
    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