I have an ImageView inside a xml layout. The ImageView has an onClick method.
android:onClick="onHomeClicked"
When the user clicks my image, I want that to in turn call onOptionsItemSelected inside the activity. How would I do that?
The following code gives null for homeMenuItem:
MenuItem homeMenuItem;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
homeMenuItem = menu.findItem(android.R.id.home);
super.onPrepareOptionsMenu(menu);
return true;
}
public void onHomeClicked(View view) {
onOptionsItemSelected(homeMenuItem);
}
You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).
Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().
Eg
private void mySharedMethod (){
//implementation
}
public void onHomeClicked (View v){
mySharedMethod();
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId ()){
case android.R.id.home:
mySharedMethod();
return true;
default:
return super.onOptionsItemSelected (item);
}
}
Possible you have to declare method and call it from onOptionsItemSelected and from onHomeClicked
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.btn:
youNewMethod();
break;
}
}
public void onHomeClicked() {
youNewMethod();
}
private void youNewMethod(){
// write your code here
}
Related
I have a view that has an onClick property:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
android:onClick="showCity" />
Which corresponds to this method:
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
However, I have a menu item that I want to have open the CityActivity as well:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, this doesn't work because it's missing the view parameter in the call to showCity() and I'm not sure what it should be in this case.
How do I modify it to work in both cases?
Replace your onOptionsItemSelected code with following following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity(null);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You showCity must have the View parameter instead of that you can pass null as parameter to your onClick() method.
Since you don't use the view parameter, you can just pass null to it and it will work
Simply pass null i.e. showCity(null)
Write an id attribute to your textview in xml as shown below, you can also remove the onClick attribute of the TextView:
<TextView
android:id="#+id/tShowCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
/>
Now in your onCreate() method of the activity initize your TextView as shown below and write the click listener for the TextView as shown below:
TextView tv = (TextView)findViewById(R.id.tShowCity);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCity();
}
});
now define your show city method as shown below:
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Now the above method can also be used in your Menu and will function without any issue as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
IF you are not using view , so why are you add in your Method Parameter...
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Look...
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Simple..... :)
If you do not want to use View property ..as answer already you can pass null value to your showCity()
showCity(null);
But if you want to use specific view properties in your showCity() you can pass view a with id by findViewById() method..like.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showCity(this.findViewById(R.id.textview1));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm using the library ActionBarSherlock, and put a item with the property android:showAsAction="ifRoom|collapseActionView". How to check if the back button of the ActionBarSherlock was clicked? thanks!
you have to override
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
}
return true;
}
from the doc:
his hook is called whenever an item in your options menu is selected.
The default implementation simply returns false to have the normal
processing happen (calling the item's Runnable or sending a message to
its Handler as appropriate). You can use this method for any items for
which you would like to do processing without those other facilities.
the answer above works (Thanks). But for my code, this solution works best ...
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
item.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// running changes ...
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// running changes ...
return true;
}
});
return super.onOptionsItemSelected(item);
};
I'd like to have only one menu for all my activities. I don't want to repeat my menu code (below) in all my activities.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.referent, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
System.out.println("set");
return true;
case R.id.action_alert:
System.out.println("alert");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I read some topics, but I found just one solution witch is to extends a parent class who declare the menu. I can't use this solution because all my activities are not extending Activity, I have also have FragmentActivity and ListActivity.
Is there a solution to have the same menu on each activity writing a minimum of code on each activity?
Depending on what the menu handling code needs access to from the current activity, you could create a class whose only responsability is to handle the selected menu items. Possibly even with just a static method that receives the MenuItem.
For example, modify activities such that the onOptionsItemSelected is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = MenuHandler.onOptionsItemSelected(item);
if (!handled) {
handled = super.onOptionsItemSelected(item);
}
return handled;
}
and create the MenuHandler class:
public class MenuHandler {
public static boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case 1: //R.id.action_settings:
System.out.println("set");
return true;
case 2: //R.id.action_alert:
System.out.println("alert");
return true;
default:
return false; //allow default processing
}
}
}
all what you need is to extend from a main class
public abstract class main extends activity(){
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// put your common menu code
super.onOptionsItemSelected(item);
}
}
public class HelloActivity extends main{
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
}
}
I use the ActionBarShellock 4.2. I think the OnCloseListener() would be called when I click the closebutton but no response when i did it.
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(mContext, "OnCloseListener", 1000).show();
return false;
}
});
I've tried to call the getChildAt(index) to get the closebutton. Then I think it's unsafe cause the SearchView is not my own code. So, how can I capture the close event? Did I do in the wrong way?
thanks in advance.
Just get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do something on close
// you must return true
return true;
}
Hack done
good luck
I have MenuItems in the ActionBar and I am using Fragments inside ViewPager. Now I would like to handle onMenuItemClickListener event inside my fragment. It works fine inside Main Activity. But not inside Fragments. And also it doesn't fetch any error.
Here is the methods that I tried. Both works fine inside Activity.
First method:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.grid_view);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.v("test","dfsfdsfasd");
return true;
}
});
return true;
}
Second Method:
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.grid_view:
{
Log.v("Log:","grid_view item pressed");
return true;
}
case R.id.list_view:
{
Log.v("Log:","list_view item pressed");
return true;
}
default:
return true;
}
}
Any help on how to achieve this will be appreciated.
Solved by using onPrepareOptionsMenu method.