Advertisement
zingga

Tabhost on Click Button

Oct 25th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.27 KB | None | 0 0
  1. #ini bagian activitynya
  2.  
  3. package com.catcode.sipintar;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.design.widget.NavigationView;
  8. import android.support.v4.app.FragmentTransaction;
  9. import android.support.v4.view.GravityCompat;
  10. import android.support.v4.widget.DrawerLayout;
  11. import android.support.v7.app.ActionBarDrawerToggle;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.support.v7.widget.Toolbar;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.TabHost;
  19.  
  20. public class MainActivity extends AppCompatActivity
  21.         implements NavigationView.OnNavigationItemSelectedListener {
  22.  
  23.     private Toolbar toolbar;
  24.     private TabHost tabHost;
  25.     private Button btn_tindak;
  26.     private TabHost host;
  27.     private TabHost.TabSpec spec;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.  
  34.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  35.         setSupportActionBar(toolbar);
  36.  
  37.         host = (TabHost)findViewById(R.id.tabHost);
  38.         host.setup();
  39.  
  40.         //Tab 1
  41.         host.addTab(host.newTabSpec("Hari ini").setIndicator("Hari ini").setContent(R.id.tab1));
  42.  
  43.         //Tab 2
  44.         host.addTab(host.newTabSpec("Osis").setIndicator("Osis").setContent(R.id.tab2));
  45.  
  46.  
  47. //        spec = host.newTabSpec("Osis");
  48. //        spec.setContent(R.id.tab2);
  49. //        spec.setIndicator("Osis");
  50. //        host.addTab(spec);
  51.  
  52.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  53.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  54.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  55.         drawer.setDrawerListener(toggle);
  56.         toggle.syncState();
  57.  
  58.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  59.         navigationView.setNavigationItemSelectedListener(this);
  60.  
  61.         btn_tindak = (Button) findViewById(R.id.btn_tindakan);
  62.         btn_tindak.setOnClickListener(new View.OnClickListener() {
  63.             @Override
  64.             public void onClick(View v) {
  65. //                Intent intent = new Intent(getApplicationContext(), QRActivity.class);
  66.             }
  67.         });
  68.     }
  69.  
  70.  
  71.     @Override
  72.     public void onBackPressed() {
  73.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  74.         if (drawer.isDrawerOpen(GravityCompat.START)) {
  75.             drawer.closeDrawer(GravityCompat.START);
  76.         } else {
  77.             super.onBackPressed();
  78.         }
  79.     }
  80.  
  81.     @Override
  82.     public boolean onCreateOptionsMenu(Menu menu) {
  83.         // Inflate the menu; this adds items to the action bar if it is present.
  84.         getMenuInflater().inflate(R.menu.main, menu);
  85.         return true;
  86.     }
  87.  
  88.     @Override
  89.     public boolean onOptionsItemSelected(MenuItem item) {
  90.         // Handle action bar item clicks here. The action bar will
  91.         // automatically handle clicks on the Home/Up button, so long
  92.         // as you specify a parent activity in AndroidManifest.xml.
  93.         int id = item.getItemId();
  94.  
  95.         //noinspection SimplifiableIfStatement
  96.         if (id == R.id.action_settings) {
  97.             return true;
  98.         }
  99.  
  100.         return super.onOptionsItemSelected(item);
  101.     }
  102.  
  103.     @SuppressWarnings("StatementWithEmptyBody")
  104.     @Override
  105.     public boolean onNavigationItemSelected(MenuItem item) {
  106.         // Handle navigation view item clicks here.
  107.         int id = item.getItemId();
  108.  
  109.         if (id == R.id.nav_camera) {
  110.             // Handle the camera action
  111.         } else if (id == R.id.nav_gallery) {
  112.  
  113.         } else if (id == R.id.nav_slideshow) {
  114.  
  115.         } else if (id == R.id.nav_manage) {
  116.  
  117.         }
  118.  
  119.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  120.         drawer.closeDrawer(GravityCompat.START);
  121.         return true;
  122.     }
  123. }
  124.  
  125.  
  126.  
  127. #ini bagian xmlnya
  128.  
  129. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  130.     xmlns:app="http://schemas.android.com/apk/res-auto"
  131.     android:layout_width="match_parent"
  132.     android:layout_height="match_parent"
  133.     android:orientation="vertical">
  134.  
  135.     <TabHost
  136.         android:id="@+id/tabHost"
  137.         android:layout_width="match_parent"
  138.         android:layout_height="match_parent"
  139.         android:layout_alignParentTop="true"
  140.         android:layout_centerHorizontal="true">
  141.  
  142.         <LinearLayout
  143.             android:layout_width="match_parent"
  144.             android:layout_height="match_parent"
  145.             android:orientation="vertical">
  146.  
  147.             <TabWidget
  148.                 android:id="@android:id/tabs"
  149.                 android:layout_width="match_parent"
  150.                 android:layout_height="wrap_content">
  151.             </TabWidget>
  152.  
  153.             <FrameLayout
  154.                 android:id="@android:id/tabcontent"
  155.                 android:layout_width="match_parent"
  156.                 android:layout_height="wrap_content"
  157.                 android:background="@color/white">
  158.  
  159.                 <LinearLayout
  160.                     android:id="@+id/tab2"
  161.                     android:layout_width="match_parent"
  162.                     android:layout_height="match_parent"
  163.                     android:orientation="vertical">
  164.  
  165.                     <RelativeLayout
  166.                         android:layout_width="match_parent"
  167.                         android:layout_height="match_parent"
  168.                         android:padding="20dp">
  169.  
  170.                         <Button
  171.                             android:layout_width="wrap_content"
  172.                             android:layout_height="wrap_content"
  173.                             android:id="@+id/btn_tindakan"
  174.                             android:text="Tindak siswa"
  175.                             android:background="@drawable/radius_button_asphalt"
  176.                             android:padding="15dp"
  177.                             android:layout_marginRight="20dp"
  178.                             android:layout_marginBottom="20dp"
  179.                             android:textColor="@color/white"
  180.                             android:drawableTop="@drawable/peluit" />
  181.  
  182.                         <Button
  183.                             android:layout_width="wrap_content"
  184.                             android:layout_height="wrap_content"
  185.                             android:id="@+id/btn_pasal"
  186.                             android:layout_toRightOf="@+id/btn_tindakan"
  187.                             android:text="Pasal-Pasal"
  188.                             android:padding="15dp"
  189.                             android:layout_marginBottom="20dp"
  190.                             android:background="@drawable/radius_button_orange"
  191.                             android:textColor="@color/white"
  192.                             android:drawableTop="@drawable/buku_kebijakan"/>
  193.  
  194.                         <Button
  195.                             android:layout_width="wrap_content"
  196.                             android:layout_height="wrap_content"
  197.                             android:id="@+id/btn_list_point"
  198.                             android:layout_marginRight="20dp"
  199.                             android:layout_below="@+id/btn_tindakan"
  200.                             android:text="point siswa"
  201.                             android:padding="15dp"
  202.                             android:background="@drawable/radius_button_turquoise"
  203.                             android:textColor="@color/white"
  204.                             android:drawableTop="@drawable/list_point"/>
  205.  
  206.                         <Button
  207.                             android:layout_width="wrap_content"
  208.                             android:layout_height="wrap_content"
  209.                             android:id="@+id/btn_jadwal_osis"
  210.                             android:layout_below="@+id/btn_pasal"
  211.                             android:layout_toRightOf="@id/btn_list_point"
  212.                             android:text="jadwal osis"
  213.                             android:padding="15dp"
  214.                             android:background="@drawable/radius_button"
  215.                             android:textColor="@color/white"
  216.                             android:drawableTop="@drawable/jadwal_osis"/>
  217.  
  218.                     </RelativeLayout>
  219.  
  220.                 </LinearLayout>
  221.  
  222.                 <LinearLayout
  223.                     android:id="@+id/tab1"
  224.                     android:layout_width="match_parent"
  225.                     android:layout_height="match_parent"
  226.                     android:orientation="vertical">
  227.  
  228.                     <RelativeLayout
  229.                         android:layout_width="match_parent"
  230.                         android:layout_height="match_parent"
  231.                         android:background="@drawable/abc"
  232.                         android:padding="15dp">
  233.  
  234.                         <TextView
  235.                             android:layout_width="match_parent"
  236.                             android:layout_height="wrap_content"
  237.                             android:text="Hello World"
  238.                             android:textStyle="bold"/>
  239.  
  240.                     </RelativeLayout>
  241.  
  242.                 </LinearLayout>
  243.  
  244.  
  245.             </FrameLayout>
  246.         </LinearLayout>
  247.     </TabHost>
  248.  
  249. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement