I am testing toolbar in Android 5.0 lollipop to show menu where my configurations are min sdk 21 and target sdk 23 I am using default toolbar from sdk 21 i.e Android 5.0 lollipop not using Appcompact v7 to support earlier version than Android 5.0 lollipop but I am stuck at menu which is not showing always I tried lots of but no luck.please get me rid of that.
following is my code.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.text);
mTitle.setTextColor(Color.parseColor("#636161"));
mTitle.setTextSize(25);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(Color.parseColor("#eea9b8"));
toolbar.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
}
@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 super.onCreateOptionsMenu(menu);
}
@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_refresh) {
Toast.makeText(MainActivity.this, "Refresh App", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.action_new) {
Toast.makeText(MainActivity.this, "Create Text", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
You got to pass your custom toolbar to setActionBar(toolbar);
method.
Try adding the above code statement at the end of OnCreate
function. If that doesn't work then make the following changes.
styles.xml
:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in AndroidManifest.xml
:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
I've made a sample code in github look at it -> Link