Implementation of Action Bar in Android version 4 (only above)
1. MyActivity.java
public class MyActivity extends Activity { public static Context con; public MyActivity() { con = MyActivity.this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); ActionBar actionbar = getActionBar(); actionbar.setHomeButtonEnabled(true); actionbar.setDisplayShowTitleEnabled(false); actionbar.setLogo(R.drawable.logout); Drawable d = getResources().getDrawable(R.drawable.top_bar_logo); actionbar.setBackgroundDrawable(d); actionbar.setIcon(R.drawable.login_logo); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_option, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuitem_search: Toast.makeText(con, "search", Toast.LENGTH_SHORT).show(); return true; case android.R.id.home: Toast.makeText(con, "Home", Toast.LENGTH_SHORT).show(); return true; } return false; } }
2.menu_option.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menuitem_search"
android:icon="@drawable/bubble"
android:showAsAction="ifRoom"
android:title="search">
</item>
</menu>
No comments:
Post a Comment