ClickListener on custom ActionBar Item (ActionBarSherlock) - android

I'm putting some custom items in my ActionBarSherlock AB like this in my SherlockFragmentActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_builtin, menu);
MenuItem selectAll = menu.findItem(R.id.selectall);
selectAll.setActionView(R.layout.selectalllayout);
return super.onCreateOptionsMenu(menu);
}
Neither onMenuItemSelected nor onOptionsItemSelected are called when a custom item is clicked, they are when I add a 'standard item' with menu.add(String).
I also tried:
selectAll.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
and
selectAll.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
they aren't called either. My layout components are made clickable and everthing that has to do with the menu is imported from ABS, not android.
Any ideas on what's wrong here?

You can do it two ways
First:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View view = LayoutInflater.from(this).inflate(R.layout.your_layout, null);
actionBar.setCustomView(view);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Do your click stuff
}
});
}
Second:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/view_id"
android:title="#string/name"
android:actionLayout="#layout/your_layout"
android:showAsAction="always" />
</menu>
In your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.main_menu, menu);
View view = (View) menu.findItem(R.id.view_id).getActionView();
// to get child view - example:
//ImageView image = (ImageView)view.findViewById(R.id.my_item);
//image.setOnClickListener....
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do stuff here
}
});
return true;
}
Dont forget to import these.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

Related

How to disable items in menu after button is clicked?

public class MainActivity extends AppCompatActivity {
private final int item_4 = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onPrepareOptionsMenu();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
menu.add(Menu.NONE, item_4, Menu.NONE, "Show");
menu.add(Menu.NONE, item_4, Menu.NONE, "Hide");
menu.add("Reset");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "You Pressed Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "You Pressed Create", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "You Pressed Delete", Toast.LENGTH_SHORT).show();
return true;
case item_4:
Toast.makeText(this, "You Pressed Hide", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mii = menu.findItem(R.id.item2);
mii.setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
}
In the above code I have created a menu item list for the app bar and I have created a button in my app and i want some of my items to be disabled after button click but I am not able to do so as I am not gettig how to call onPrepareOptionsMenu() on button click? Can anyone suggest a solution for how to disable the menu items after button click?
I will show short example:
Only one note for this method: don’t forget to clear menu items (menu.clear();) on every method start.
Example:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Class which shows how to change dynamically options menu items
*/
public class Main extends Activity {
private Button clickBtn;
private boolean isChangedStat = false;
private static final int MENUITEM = Menu.FIRST;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clickBtn = (Button) findViewById(R.id.click);
clickBtn.setText("Click me");
clickBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isChangedStat) {
isChangedStat = false;
} else {
isChangedStat = true;
}
}
});
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(isChangedStat) {
menu.add(0, MENUITEM, 0, "True");
} else {
menu.add(0, MENUITEM, 0, "False");
}
return super.onPrepareOptionsMenu(menu);
}
}
Reference from:
Android: How to enable/disable option menu item on button click?
Once the activity is created, the onCreateOptionsMenu() method is
called only once, as described above. The system keeps and re-uses the
Menu you define in this method until your activity is destroyed. If
you want to change the Options Menu any time after it's first created,
you must override the onPrepareOptionsMenu() method. This passes you
the Menu object as it currently exists. This is useful if you'd like
to remove, add, disable, or enable menu items depending on the current
state of your application.
you could call the method invalidateOptionsMenu(); in your button's click listener. It will call onPrepareOptionsMenu() again, all your logic to disable could be written here.
Anyway, documents covers all.

How to click a button to open a customized menu (it's not context menu of the button) in android?

I hope to display a customized menu deleterecord.xml by clicking btnDelete button. It's not a context menu of btnDelete, opening a context menu need long click, so you can't use registerForContextMenu do it.
I have used openOptionsMenu() to open Options Menu while I click btnMore button, I hope to click btnDelete button to open another customized menu (The menu file is deleterecord.xml), how to do? Thanks!
private void SetButtons() {
findViewById(R.id.btnMore).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
findViewById(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to open deleterecord.xml 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.menu_more, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.MoreShare:
return true;
case R.id.MoreSettings:
return true;
case R.id.MoreAbout:
return true;
}
return super.onOptionsItemSelected(item);
}
This is option menu menu_more.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="info.dodata.unlock.UnlockMain" >
<item android:id="#+id/MoreShare"
android:title="#string/MoreShare" />
<item android:id="#+id/MoreUninstall"
android:title="#string/MoreUninstall" />
<item android:id="#+id/MoreAbout"
android:title="#string/MoreAbout" />
</menu>
This is my customized menu deleterecord.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/ContextDeleteLess"
android:title="Delete 10 records" />
<item android:id="#+id/ContextDeleteMore"
android:title="Delete 20 records"/>
</menu>
You can set a buttons onClick by android:onClick="methodName" in your xml layout under the button. This method should be in your views class and should have a single View argument. You can use this to determine if the view opens one menu or another. If it isn't a button view, make sure to set android:clickable="true".
You can create a menu layout and try opening it as popup.
findViewById(R.id.btnAnoter).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent popup=new Intent(getBaseContext(),Islemler.class);
popup.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popup);
}
});
This is how I managed to create custom menu window.
What is this other menu?Is it a Dialog with menu options?
However if you want to show Dialog with your options, you can do it like below:
findViewById(R.id.btnAnoter).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.menus);//This is your menu view.
dialog.show();
}
});
You need to store Menu reference in global variable and then inflate the menu again on button click. Below is the code for the same:
public class MainActivity extends Activity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openMenu1();
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openMenu2();
}
});
}
private void openMenu1() {
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
openOptionsMenu();
}
private void openMenu2() {
menu.clear();
getMenuInflater().inflate(R.menu.main1, menu);
openOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Android: Fragment's onOptionsItemSelected doesn't get called

I have an ActionBarActivity and one Fragment. The Activity has no menu inflated, while the Fragment has a menu with two buttons. Fragment menu is visible, but the buttons don't react at all when tapped. At debugging I can see that both onCreateOptionsMenu() for Fragment and Activity get called, but when tapping buttons no onOptionsItemSelected() gets called, neither from Activity nor from Fragment.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity = (NavigationActivity)getActivity();
setHasOptionsMenu(true);
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState){
return (ScrollView) inflater.inflate(R.layout.tutoring_detail, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.query_details_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.accept_query:
respondToQuery(true);
return true;
case R.id.decline_query:
respondToQuery(false);
return true;
default:
break;
}
return false;
}
Menu to be displayed in Fragment
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<item
android:id="#+id/accept_query"
android:orderInCategory="100"
app:showAsAction="always"
android:checkable="true"
style="?android:attr/borderlessButtonStyle"
app:actionViewClass="android.widget.ImageButton"/>
<item
android:id="#+id/decline_query"
android:orderInCategory="101"
app:showAsAction="always"
android:checkable="true"
style="?android:attr/borderlessButtonStyle"
app:actionViewClass="android.widget.ImageButton"/>
</menu>
In the Activity class,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
return false;
}
In the Fragment,
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "called " + item.getItemId(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
You must use super.onOptionsItemSelected(item) in the parent activity's onOptionsItemSelected(...) method.
From Activity | Android Developers:
Derived classes should call through to the base class for it to perform the default menu handling.
Try moving setHasOptionsMenu(true) inside of the onCreateView() method in your Fragment instead of onCreate().
I had a similar issue after making special layout for my action button (I made an Image Button and needed to pad it and change some other things so I had to use layout for it).
Then onOptionsItemSelected lost connection with this imageButton so I just use clickListener for it inside onCreateOptionsMenu. It might not be the best practice, maybe there is a better solution, but this is what solve my problem.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.picker_list_menu, menu);
MenuItem itemDone = menu.findItem(R.id.menu_done);
MenuItemCompat.setActionView(itemDone, R.layout.menu_layout_done);
menuDoneIB = (ImageButton) MenuItemCompat.getActionView(itemDone);
itemDone.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code..
}
});
}
Try using oncreateview
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
If you have dynamically changed menu item, for instance, a badge menu (https://stackoverflow.com/a/16648170/2914140 or https://stackoverflow.com/a/26017587/2914140), you should initialize the item in onCreateOptionsMenu and re-set setOnClickListeners after every change of the item.
In my case:
private MenuItem menuItem;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_catalog, menu);
menuItem = menu.findItem(R.id.action_badge);
writeBadge(0);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_badge) {
// Your code.
return true;
}
return super.onOptionsItemSelected(item);
}
private void writeBadge(int count) {
MenuItemCompat.setActionView(menuItem, R.layout.item_badge);
RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(menuItem);
// A TextView with number.
TextView tv = (TextView) layout.findViewById(R.id.badge);
if (count == 0) {
tv.setVisibility(View.INVISIBLE);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(String.valueOf(count));
}
// An icon, it also must be clicked.
ImageView imageView = (ImageView) layout.findViewById(R.id.image);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
};
menuItem.getActionView().setOnClickListener(onClickListener);
imageView.setOnClickListener(onClickListener);
}
I am passing the click event to fragment from the activity. This worked for me.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
return getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getFragments().size() - 1)
.onOptionsItemSelected(item);
}

Android: I can't make collapseActionView

I want to make something like that:
I try to make the popup menu whit the collapseview, y the action bar. but i try all the thing that i found on internet and no solution found. i didnt need suppoart android api less than 15.
this the main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_dialog_email"
android:title="#string/hello_world"
android:showAsAction="always|collapseActionView"
/>
<item android:id="#+id/action_compose"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
<item android:id="#+id/action_compose2"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
</menu>
This my activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar a = getActionBar();
a.setTitle("Mariano");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}
If I understood correctly you want to open an Android Popup Menu by click on a Button. Then all you have to do is to add this to your MainActivity:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.id1) {
//do something
return true;
}
else if (i == R.id.id2){
//do something
}
else if (i == R.id.id3) {
//do something
return true;
}
else if (i == R.id.id4) {
//do something
return true;
}else {
return onMenuItemClick(item);
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}

Android app is closing on press of back button

I have a menu and when clicked on a menu, it goes inside another menu.
for example, mainmenu-->click on airport guide --> menu2
but when i am in menu2 and press on the back button, the app is closing instead of going back to mainmenu . i am not able to figure out the problem here. i am new to android development
package com.shashank.sharjahinternationalairport;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
saaDcaButton = (ImageButton) findViewById(R.id.saaDca);
cargoButton = (ImageButton) findViewById(R.id.cargo);
airportGuideButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.airport_guide);
}
});
visitorInfoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.visitor_info);
}
});
saaDcaButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.saa_dca);
}
});
}
#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;
}
You are using setContentView, so you are just changing the view layout. When you press the backbutton, the Android calls onBackPressed, which the default implementation is to call finnish() method, that closes the activity.
You can override the onBackPressed method to set the other view.
#Override
public void onBackPressed()
{
//setContentView the previous view
}
Hope it helps!
you can always override the onBackPressed method of Activity, you'll have something like this
public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;
private enum VIEWS {
VIEW_1, VIEW_2, VIEW_3
};
private VIEWS mSelectedView = VIEWS.VIEW_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
saaDcaButton = (ImageButton) findViewById(R.id.saaDca);
cargoButton = (ImageButton) findViewById(R.id.cargo);
airportGuideButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView1();
}
});
visitorInfoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView2();
}
});
saaDcaButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView3();
}
});
}
private void showView1() {
setContentView(R.layout.airport_guide);
mSelectedView = VIEWS.VIEW_1;
}
private void showView2() {
setContentView(R.layout.visitor_info);
mSelectedView = VIEWS.VIEW_2;
}
private void showView3() {
setContentView(R.layout.saa_dca);
mSelectedView = VIEWS.VIEW_3;
}
#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 void onBackPressed() {
switch (mSelectedView) {
case VIEW_1:
super.onBackPressed();
break;
case VIEW_2:
showView1();
break;
case VIEW_3:
showView2();
break;
}
}
}
All you is doing that you are changing the Content View of the class MainActivity you need to create sperate classes for your difreent layouts and you can call it by INTENT
This is happennig because you are using only one activity, and on click just changing the content view. Even though this appears like a multi page activity, it is only one screen. Hence onBackPressed() it closes. Yo will have to create different activiies for each of your menu options and start them using Intents on Button Click.
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
You are using only one activity, if it is many activity mean you can track back to previously loaded activity.
else you must needed only one activity mean please store the layouts in some array, then in the onBackPressed() you can it get LIFO manner
#Override
public void onBackPressed() {
}

Categories

Resources