Baiklah tanpa memperpanjang mukadimah lagi langsung saja kita mulai. Pertama-tama anda harus membuat project baru dengan nama terserah anda. Pada folder res/layout buatlah file xml layout seperti koding dibawah ini.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | android:id = "@+id/drawer_layout" android:layout_width = "match_parent" android:layout_height = "match_parent" > < FrameLayout android:id = "@+id/frame_container" android:layout_width = "wrap_content" android:layout_height = "match_parent" > </ FrameLayout > < ListView android:id = "@+id/slider_list" android:layout_width = "240dp" android:layout_height = "wrap_content" android:layout_gravity = "start" android:choiceMode = "singleChoice" android:divider = "@android:color/transparent" android:dividerHeight = "0dp" /> </ android.support.v4.widget.DrawerLayout > |
list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "69dp" android:background = "#1f2021" > < ImageView android:id = "@+id/icon" android:layout_width = "50dp" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" android:layout_centerVertical = "true" android:layout_marginLeft = "2dp" android:layout_marginRight = "2dp" android:contentDescription = "desc" /> < TextView android:id = "@+id/title" android:layout_width = "wrap_content" android:layout_height = "match_parent" android:layout_alignParentRight = "true" android:layout_centerVertical = "true" android:layout_toRightOf = "@+id/icon" android:gravity = "center_vertical" android:height = "40dp" /> </ RelativeLayout > |
home.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:layout_centerVertical = "true" android:text = "home" android:textAppearance = "?android:attr/textAppearanceLarge" /> </ RelativeLayout > |
profil.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:layout_centerVertical = "true" android:text = "Profil" android:textAppearance = "?android:attr/textAppearanceLarge" /> </ RelativeLayout > |
visimisi.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:layout_centerVertical = "true" android:text = "Visi Misi" android:textAppearance = "?android:attr/textAppearanceLarge" /> </ RelativeLayout > |
Setelah selesai membuat layout, saatnya membuat file java di src/namapackage. Buatlah file java seperti dibawah ini.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | package com.stmik.promo; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.content.res.Configuration; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.stmik.promo.cont.Home; import com.stmik.promo.cont.Profil; import com.stmik.promo.cont.Visi; public class MainActivity extends Activity { String [] menutitles; TypedArray menuIcons; // nav drawer title private CharSequence mDrawerTitle; private CharSequence mTitle; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private List<RowItem> rowItems; private CustomAdapter adapter; @SuppressLint( "NewApi" ) @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); menutitles = getResources().getStringArray(R.array.titles); menuIcons = getResources().obtainTypedArray(R.array.icons); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.slider_list); rowItems = new ArrayList<RowItem>(); for (int i = 0 ; i < menutitles.length; i++) { RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId( i, -1 )); rowItems.add(items); } menuIcons.recycle(); adapter = new CustomAdapter(getApplicationContext(), rowItems); mDrawerList.setAdapter(adapter); mDrawerList.setOnItemClickListener( new SlideitemListener()); // enabling action bar app icon and behaving it as toggle button getActionBar().setDisplayHomeAsUpEnabled( true ); getActionBar().setHomeButtonEnabled( true ); // getActionBar().setBackgroundDrawable(new // ColorDrawable(R.color.lime)); // getActionBar().setBackgroundDrawable(new // ColorDrawable(Color.parseColor("#ffffff"))); getActionBar().setBackgroundDrawable( getResources().getDrawable(R.drawable.header)); mDrawerToggle = new ActionBarDrawerToggle( this , mDrawerLayout, R.drawable.ic_drawer, // nav menu toggle icon R.string.app_name, // nav drawer open - description for R.string.app_name // nav drawer close - description for // accessibility ) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); // calling onPrepareOptionsMenu() to show action bar icons invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); // calling onPrepareOptionsMenu() to hide action bar icons invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null ) { // on first time display view for first nav item updateDisplay( 0 ); } } class SlideitemListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { updateDisplay(position); } } private void updateDisplay(int position) { Fragment fragment = null ; switch (position) { case 0 : fragment = new Home(); break ; case 1 : fragment = new Profil(); break ; case 2 : fragment = new Visi(); break ; default: break ; } if (fragment != null ) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() . replace (R.id.frame_container, fragment).commit(); // update selected item and title, then close the drawer setTitle(menutitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e( "MainActivity" , "Error in creating fragment" ); } } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(mTitle); } @Override public boolean onCreateOptionsMenu(Menu menu) { return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { // toggle nav drawer on selecting action bar app icon/title if (mDrawerToggle.onOptionsItemSelected(item)) { return true ; } return super .onOptionsItemSelected(item); } /*** * Called when invalidateOptionsMenu() is triggered */ @Override public boolean onPrepareOptionsMenu(Menu menu) { // if nav drawer is opened, hide the action items boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); // menu.findItem(R.id.action_settings).setVisible(!drawerOpen); return super .onPrepareOptionsMenu(menu); } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super .onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super .onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); } } |
CustonAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com.stmik.promo; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class CustomAdapter extends BaseAdapter { Context context; List<RowItem> rowItem; CustomAdapter(Context context, List<RowItem> rowItem) { this .context = context; this .rowItem = rowItem; } private class ViewHolder { ImageView icon; TextView title; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null ) { convertView = mInflater.inflate(R.layout.list_item, null ); holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.icon); holder.title = (TextView) convertView.findViewById(R.id.title); RowItem row_pos = rowItem.get(position); holder.icon.setImageResource(row_pos.getIcon()); holder.title.setText(row_pos.getTitle()); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } return convertView; } @Override public int getCount() { return rowItem.size(); } @Override public Object getItem(int position) { return rowItem.get(position); } @Override public long getItemId(int position) { return rowItem.indexOf(getItem(position)); } } |
RowItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.stmik.promo; public class RowItem { private String title; private int icon; public RowItem( String title, int icon) { this .title = title; this .icon = icon; } public String getTitle() { return title; } public void setTitle( String title) { this .title = title; } public int getIcon() { return icon; } public void setIcon(int icon) { this .icon = icon; } } |
Home.java, Profil.java, dan Visi.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.stmik.promo.cont; import com.stmik.promo.R; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @SuppressLint( "NewApi" ) public class Home extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.home, container, false ); return rootView; } } |
No comments:
Post a Comment