When I click the button in the title bar it doesn't do any thing.
I use item in menu and I linked it to main activity class but it doesn't work.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return true;
}
#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);
// Inflate switch
notification_badge = (TextView) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.notification_badge);
ImageButton notification_open = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.Notificatio_open);
ImageButton addFollwes = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.my_action_search);
notification_badge.setVisibility(View.GONE);
addFollwes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchActivity.class));
}
});
notification_open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, com.account.Notification.class));
// notification_badge.setVisibility(View.GONE);
}
});
Switch mSwitchNightMode = (Switch) menu.findItem(R.id.item_switch)
.getActionView().findViewById(R.id.Switchedbtn);
// Get state from preferences
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_action:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
case R.id.my_action_search:
startActivity(new Intent(MainActivity.this, SearchActivity.class));
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
It's hard to tell without looking at the XML menu layout code. I am guessing that something went wrong such that findViewById for the menu did not register. You don't have to explicitly identify the menu items, but actually the entire menu. Then we can query which item was clicked via a switch statement.
Below I provide a simple way of handling menu
Layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_01"
android:title="1"/>
<item
android:id="#+id/menu_item_02"
android:title="2"/>
<item
android:id="#+id/menu_item_03"
android:title="3"/>
</menu>
Java
import androidx.appcompat.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
private ImageView menuButton;
private PopupMenu mainMenu;
private MenuInflater menuInflater;
menuButton = findViewById(R.id.menu_button);
menuButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mainMenu = new PopupMenu(MainActivity.this, v);
menuInflater = mainMenu.getMenuInflater();
mainMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menu_item_01:
// perform action
return true;
case R.id.menu_item_02:
// perform action
return true;
case R.id.menu_item_03:
// perform action
return true;
default:
return false;
}
}
});
menuInflater.inflate(R.menu.main_menu, mainMenu.getMenu());
mainMenu.show();
}
});
Related
I am trying to open popup menu when pressing one of the elements of the Dialog Fragment, but onOptionsItemSelected method is never called, even when pressing one of the menu items. What should I do to fix it?
public class AddSongDialogFragment extends DialogFragment implements View.OnClickListener {
private TextView genreTextview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.addsong_layout, container);
//...
genreTextview = (TextView) view.findViewById(R.id.genreTextView);
view.findViewById(R.id.ll_genre_menu).setOnClickListener(this);
genreTextview.setText(R.string.press_to_choose_genre);
return view;
}
//...
public void onClick(View v){
switch(v.getId()) {
case R.id.button2:
dismiss();
mListener.onChoose();
break;
case R.id.ll_genre_menu:
PopupMenu popup = new PopupMenu(getContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.song_genres, popup.getMenu());
popup.show();
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
genreTextview.setText(item.getTitle());
Log.d("songo", "item selected");
return true;
}
song_genres.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/pop"
android:title="#string/pop"/>
<item android:id="#+id/rock"
android:title="#string/rock" />
...
</menu>
You should set setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener listener) for popup
It will be like below
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.d("songo", "item selected");
return false;
}
});
I am doing A Level computing and I'm trying to make an app for my Comp 4 project. All the other classes i have done with buttons seem to be working fine but the buttons on my main menu are not working.
public class MainActivity extends AppCompatActivity {
Button bLogin;
Button bProducts;
EditText etName, etAge, etUsername;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id= item.getItemId();
switch (id) {
case R.id.bLogin:
break;
case R.id.bProducts:
break;
}
return super.onOptionsItemSelected(item);
}
public void ProductsOnClick(View b) {
startActivity(new Intent(".Products"));
switch (b.getId()) {
case R.id.bProducts:
}
}
public void LoginOnClick(View a) {
startActivity(new Intent(".Login"));
switch (a.getId()){
case R.id.bLogin:
}
}
}
This Activity runs but the buttons on the Options Menu don't work.
I am using Android Studio to program this.
Because there is nothing to execute when the menu item is selected, change your code like that:
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id= item.getItemId();
switch (id) {
case R.id.bLogin:
startActivity(new Intent(".Login"));
break;
case R.id.bProducts:
startActivity(new Intent(".Products"));
break;
}
return super.onOptionsItemSelected(item);
}
PS:
Make sure that you're using the right Intent constructor, because with Intent(String) you need an action as parameter not an activity name or something else.
You can use OnClickListener instead. Put this in your onCreate:
bProducts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(".Products"));
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(".Login"));
}
});
I have a menu in my ActionBar, which is checkable (in XML), but when I try, in java, to check it on press on this item, the item stay unchecked (but the other things related to this action is done)
My XML menu :
<?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"
xmlns:tools="http://schemas.android.com/tools">
[...]
<item
android:id="#+id/menu_switch_full_original"
android:title="#string/menu_switch_full_original"
android:checkable="true"/>
</menu>
And the java code :
public boolean onOptionsItemSelected(MenuItem item) {
if (mEntriesIds != null) {
Activity activity = getActivity();
switch (item.getItemId()) {
[...]
case R.id.menu_switch_full_original: {
item.setChecked(true);
[...]
}
activity.invalidateOptionsMenu();
}
return true;
}
What am I missing ?
Try this...
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.menu_switch_full_original);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.a, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_switch_full_original:
isChecked = !item.isChecked();
item.setChecked(isChecked);
// your other functionality
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I created a menu (Fack Call, Fake SMS, Statistic & About).
I want this text with an icon.
I tried, but it didn't work.
I also want to move to another screen when I click Fack Call, Fake SMS, Statistic text.
How can I do that?
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressWarnings("deprecation")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(MainActivity.this, "call", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(this, FakeCall.class);
startActivity(intent);
return true;
case R.id.sms:
Toast.makeText(MainActivity.this, "sms", Toast.LENGTH_SHORT).show();
return true;
case R.id.statistic:
Toast.makeText(MainActivity.this, "statistic", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.about:
Toast.makeText(MainActivity.this, "about", Toast.LENGTH_SHORT)
.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
FackCall.java
public class FakeCall extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fakecall);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/call"
android:icon="#drawable/fakecall"
android:title="#string/call"/>
<item
android:id="#+id/sms"
android:icon="#drawable/fakesms"
android:title="#string/sms"/>
<item
android:id="#+id/statistic"
android:icon="#drawable/statistic"
android:title="#string/statistic"/>
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="#string/about"/>
</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_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);
}'
I'm having trouble with using menu item on action bar using slidingmenu library with actionbarsherlock.
Anyone can help?
Here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
this is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/print"
android:title="#string/printItem"
android:icon="#drawable/ic_print"
android:showAsAction="always" />
<item
android:id="#+id/share"
android:title="#string/shareItem"
android:icon="#drawable/ic_action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
My logcat says that I encountered runtime error at
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Don't really understand your question.
This code works in my app.
in MainActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
break;
case R.id.print:
//your code
break;
case R.id.share:
//your code
break;
default:
return false;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
in Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_fragment,menu);
super.onCreateOptionsMenu(menu, inflater);
}