i have problem with my android code. i want to create button icon at android toolbar. and the icon is showing at preview. see my picture below menu.xml
but when i run this project, the button icon is not showing up. run project
and this is my activity code
public class Main2Activity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout dr;
private Toolbar toolbar;
private View navHeader;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
dr = (DrawerLayout) findViewById(R.id.drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupDrawerContent(navigationView);
actionBarDrawerToggle = setupDrawerToggle();
dr.setDrawerListener(actionBarDrawerToggle);
selectDrawerItem(navigationView.getMenu().getItem(0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
whats the problem ? any suggestion would be appreciated. thank you
create a resource file in menu named it main_menu_tool.xml & paste bellow code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/miCompose"
balloonberry:showAsAction="always"
android:icon="@drawable/menu"/>
<item
android:id="@+id/miProfile"
balloonberry:showAsAction="always"
android:icon="@drawable/week_view"/>
</menu>
In Main2Activity just write like ...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu_tool, menu);
return true;
}
Here's the code of Menu.XML I wrote for a location icon. Image
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/Location1"
app:showAsAction="always"
android:icon="@drawable/location"
android:title="@string/location"
/>
</menu>
And part of MainActivity.java representing the icon
@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.Location1) {
Intent intent = new Intent(MainActivity.this,Location.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.