content_main.xml
activity_main.xml
I was trying to following an online youtube android beginner class tutorial, but when I create a new blank activity, the 3 dot on the right top overflow menu does not show and this rendering problem happened. I've tried to change the API to 22, 21, 19 and also tried changing the style.xml method, both doesn't work for me. Please help. Thanks.
For Menu u have to override the below functions.
#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 true;
}
#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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Even though menu is not available in preview,just test it in emulator or real device.It works perfectly.
Related
I want to use a standalone toolbar and for that I defined a android.support.v7.widget.Toolbar in my layout.
I want to add an item in the menu and using inflateMenu the toolbar seems to work fine but the inflateMenu was added in API-21.
How can I add a menu for earlier SDKs?
If you wanna add/show menu buttons, why don't you use
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu_layout, menu);
return true;
}
#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_button1) {
return true;
}
return super.onOptionsItemSelected(item);
}
To use the SupportToolbar like the default one, you need to tell your Activity to use it as a normal Toolbar by using
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
After that you can use the Toolbar like a default one with Framework functionalities like onCreateOptionMenu or onOptionsItemSelected.
I need to create ActionBar with left side button item looks like below mentioned Image. I tried by using huge of codes, but still I didnt get anything. Please help me to create android ActionBar with button.
I need to create like below
Image
NOTE: I don't have sample code, Moreover It's not proper because I am new developer for android.
To Add Action Buttons add this in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//and this to handle actions
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and this in your main.xml under menu directory
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/icon"
android:orderInCategory="100"
app:showAsAction="always" />
To get the icon to the left see this
Is there a way I can add a button to the top right of my ActionBar, like where the default settings Button is? I removed the settings Button but I'd like to add a custom Button in it's place.
You can add a button by editing/create the menu xml file:
<?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/action_name"
android:icon="#drawable/you_resource_here"
android:title="Text to be seen by user"
app:showAsAction="always"
android:orderInCategory="0"/>
</menu>
Then in your activity, if you created a new file your need to edit onCreateOptionsMenu
#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 true;
}
and you can edit what the actions do in the following method:
#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_name) {
return true;
}
return super.onOptionsItemSelected(item);
}
This might be easier, however I use a toolbar instead:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_name:
//your code
break;
}
return super.onOptionsItemSelected(item);
}
I want to make a simple settings page, I went to Google's documentation below:
http://developer.android.com/guide/topics/ui/settings.html
created new android testing project, then I created the PreferenceScreen xml, and PreferenceFragment class inside my mainActivity class.
I want the settings screen to appear when I press the settings icon, but instead it only shows one unclickable item named settings.
I know my question is basic, but I tried a lot to make it work with no results.
any help please?
If you want to open the settings from the Menu inside the action bar, as shown below :
First, you need to define a Menu item inside the menu.xml file for that activity :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
Second, you need to infate the menu, and then the events for the menu item click can be handles using the methods shown below :
#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 true;
}
#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_settings) {
// write code to handle the click for the settings menu item!
return true;
}
return super.onOptionsItemSelected(item);
}
In my app, I have an icon in the action bar. When I click on it, I get an option called Settings. However, when I click on this option, nothing happens. I have searched, but I can't find out how to utilize this option. I would like for a dialog box to open when Settingsclicked (or if another Activity opened that would be cool too), which I could then add content to. Does anyone know how I would go about this?
The solution Dave S provided works but I want to share another way you can handle this functionality.
In your Activity you can have the following code to interact with your menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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();
if (id == R.id.action_settings) {
displaySettingsDialog();
}
return super.onOptionsItemSelected(item);
}
The key is to make sure you are checking for the right ID you assigned to your menu item.
If you wanted to launch a new activity (SettingsActivity) then you can replace the displaySettingsDialog() function with the following:
Intent intent = new Intent(this, SettingsActivity.class)
startActivity(intent);
Look for main.xml in your res/menu/ folder. (at least for eclipse ADT) There you can specify an onClick attribute which will call a function in your code. from there you can do whatever you want with it.
EDIT
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/fb_login"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/facebook_login_label"
android:onClick="showFacebookDialog"/>
</menu>
This would define the item for my MainActivity, inside which you define the onClick like so
public void showFacebookDialog(final MenuItem item)
{
...
}