Tabhost Buatlah 2 Activity Baru Untuk 2 Layout
Tabhost Buatlah 2 Activity Baru Untuk 2 Layout
TabHost
Buatlah 2 activity baru untuk 2 layout
Koding :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.master.pab13.MainActivity">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/layout1"
layout= "@layout/layout_satu"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/layout2"
layout= "@layout/layout_dua"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Mainactiviry.java
Koding :
package com.example.master.pab13;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TabHost;
public class MainActivity extends ActionBarActivity {
//Deklarasi Variabel
TabHost tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = (TabHost) findViewById(R.id.tabHost);
tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("TAB 1");
spec1.setIndicator("TAB 1");
spec1.setContent(R.id.layout1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("TAB 2");
spec1.setIndicator("TAB 2");
spec1.setContent(R.id.layout2);
tab.addTab(spec2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/layout1"
layout= "@layout/layout_satu"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/layout2"
layout= "@layout/layout_dua"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/layout3"
layout= "@layout/activity_layout_tiga"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/layout4"
layout= "@layout/activity_layout_empat"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
MainActivity.Java
Koding :
package com.example.master.pab13;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TabHost;
public class MainActivity extends ActionBarActivity {
//Deklarasi Variabel
TabHost tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = (TabHost) findViewById(R.id.tabHost);
tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("TAB 1");
spec1.setIndicator("TAB 1");
spec1.setContent(R.id.layout1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("TAB 2");
spec2.setIndicator("TAB 2");
spec2.setContent(R.id.layout2);
tab.addTab(spec2);
TabHost.TabSpec spec3 = tab.newTabSpec("TAB 3");
spec3.setIndicator("TAB 3");
spec3.setContent(R.id.layout3);
tab.addTab(spec3);
TabHost.TabSpec spec4 = tab.newTabSpec("TAB 4");
spec4.setIndicator("TAB 4");
spec4.setContent(R.id.layout4);
tab.addTab(spec4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sesudah