I trying to personalize the Android tutorial about Layout Changes (http://developer.android.com/training/animation/layout.html), but in the code there is this code
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Navigate "up" the demo structure to the launchpad activity.
// See http://developer.android.com/design/patterns/navigation.html
// for more.
NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
return true;
case R.id.action_add_item:
// Hide the "empty" view since there is now at least one item in the
// list.
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
return true;
}
return super.onOptionsItemSelected(item);
}
In my case I don't have a button in the menu, but I woud use an external button. I trying to use this button:
<Button
android:id="#+id/buttonPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/editTextComment"
android:text="Post"
android:onClick="add" />
With the method
public void add(View view) {
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
}
But this solution doesn't work. Any ideas?
onOptionsItemSelected is for an options menu used with onCreateOptionsMenu(Menu menu). Simply move this code to your onClick if you want it to work for the Buttons and switch on the Button id.
public void add(View v) {
switch (v.getId()) {
case R.id.buttonPost:
findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
break;
case R.id.another_button_id:
// do something else
break;
}
}
Put an OnClickListener on your button instead:
findViewById(R.id.buttonPost).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do something here
}
});
Related
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#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 == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...
I want to open the menu, the problem is my game uses full screen so there is no toolbar, no room for a FAB and no menu button. I have tried to use
openOptionsMenu();
in an onClickListener from a button on the screen but it does nothing. If you have any suggestions please reply here.
The answer I found was to use the PopupMenu, see the example and comments below.
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public void onClick(View view) {
switch (view.getId()) {
case R.id.new_game_button:
//start new game
break;
case R.id.menu_button:
/** Instantiating PopupMenu class */
PopupMenu popup = new PopupMenu(getBaseContext(), view);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.game_menu, popup.getMenu());
/** Defining menu item click listener for the popup menu */
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_more_apps:
//do something
return true;
case R.id.menu_about:
// do something
return true;
case R.id.menu_like_us:
//do something
return true;
break;
I'm working on an android app and recently, I discovered that the default back button onBackPressed() produces a back behaviour, while the getSupportActionBar().setDisplayHomeAsUpEnabled(true) produces an up behaviour. And the two have significant differences.
I was wondering if I can simulate the up behaviour when I press the hardware back button, so that it navigates up instead of back.
Thanks.
You can do this.
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
}
You can do this
Overide onOptionsItems Selected
Create a switch case of android.R.id.home
Call method onBackPressed();
here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
Right now I have a couple of buttons that do different things when clicked, but now I want one of them to display a menu when clicked, but I am not sure how to do this.
My code is something like this for the main buttons:
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.button1:
//do stuff
break;
case R.id.button2:
//display menu
break;
}
If button2 is pressed, I want to display a list of options and see what menu item the user selects, but how can I do this?
Displaying icon in menu
XML
<item
android:id="#+id/item_1"
android:icon="#drawable/settings"
android:showAsAction="always"
android:title="Add item1" />
You could use a PopupMenu In your onOptionsItemSelected() which will then show a different menu when one of your menu buttons is clicked. Modify this piece of code according to your needs:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.button1:
// DO SOMETHING HERE
break;
case R.id.button2:
// THE R.id.button2 has to be the same as the item that will trigger the popup menu.
View v = findViewById(R.id.button2);
PopupMenu pm = new PopupMenu(LoginActivity.this, v);
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), String.valueOf(item.getTitle()), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.menuEdit:
break;
case R.id.menuDetails:
break;
case R.id.menuDelete:
break;
default:
break;
}
return true;
}
}); pm.show();
break;
default:
break;
}
return false;
}
You will notice that a new menu XML has been inflated at this line:
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
You will have to create a second menu XML with the list of options that you need to display when one of the buttons is clicked. This is similar to your current menu XML with the difference being, a different set of options.
IMPORTANT!
Do not forget to include this View v = findViewById(R.id.button2); before the PopupMenu pm..... The PopupMenu requires a View to anchor itself to. But the onOptionsItemSelected() method does not provide this at all. Hence the extra statement.
The above example illustrates the example in an Activity. To use this in a Fragment, change the View v = findViewById(R.id.button2); to View v = getActivity().findViewById(R.id.button2);
This is the final result:
If you are talking about Opening option menu, Then there is a method openOptionMenu() in Activity class.
case R.id.button2:
openOptionsMenu();
break;
If you are talking about opening contextMenu
You have to call openContextMenu() method, But don't forget to add registerForContextMenu(view) and other methods for taking action
I want to add a new button to the ActionBar. When I click it, it does a specific Action. So I don't want a button that, after being pressed, opens a sub-menu (like the classic 3-dot menu).
I created a new button, this:
<item android:id="#+id/action_refresh"
android:icon="#drawable/refresh"
android:title="#string/refresh_string"
android:showAsAction="always"/>
and it's shown on the ActionBar, but if I click it, naturally, it doesn't do anything.
How can I manage to get an Action just pressing it?
Thanks!
You Need to override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_refresh:
//do your stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Same as Homo sapiens's answer, but with an if-structure.
Add this method to your Activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_refresh)
{
// do your stuff
return true;
}
else if (item.getItemId() == R.id.otherItem)
{
// do other stuff
return true;
}
// ...
else
{
return super.onOptionsItemSelected(item);
}
}