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;
}
});
Related
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();
}
});
This is my first try with Bottom Sheets.
I have a menu button in Toolbar that shows Bottom Sheets with 3 item, one of them is Switch widget that can change theme to Dark Mode. When app starts it checkes is the Dark Mode on or off.
Bottom Sheet layout
When I try to set value onCreate Method in Switch I'm getting an error:
Attempt to invoke virtual method 'void androidx.appcompat.widget.SwitchCompat.setChecked(boolean)' on a null object reference
I understand the error that Switch Widget is initialized in Bottom Sheet layout not Main and appears only when user clicks the button in Toolbar but what can I change in code?
MainActivity on Create
switchCompat = findViewById(R.id.switchMode);
sharedPreferences = getSharedPreferences("night",0);
Boolean booleanValue = sharedPreferences.getBoolean("night_mode", true);
if (booleanValue){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
}
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
switchCompat.setChecked(true);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("night_mode", true);
editor.commit();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
switchCompat.setChecked(false);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("night_mode", false);
editor.commit();
}
}
});
Methods after onCreate:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_lang, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_more_action) {
showBottomSheetDialog();
}
return super.onOptionsItemSelected(item);
}
public void showBottomSheetDialog() {
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.bottom_sheet);
LinearLayout lang = bottomSheetDialog.findViewById(R.id.languageBottom);
LinearLayout about = bottomSheetDialog.findViewById(R.id.aboutBottom);
LinearLayout darkMode = bottomSheetDialog.findViewById(R.id.darkModeBottom);
lang.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showChangeLanguageDialog();
bottomSheetDialog.dismiss();
}
});
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openAboutAppActivity();
bottomSheetDialog.dismiss();
}
});
bottomSheetDialog.show();
}
I'm trying to have a different menu show up when a fullscreen dialog is shown but it won't appear. Instead, a blank menu appears.
I've tried calling it conditionally from the original activity and I've tried calling it from the dialog view.
Here's the fullscreen Dialog View:
public class MyMainView extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.layout.layout_full_screen_dialog);
setHasOptionsMenu(true);
}
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
new MenuInflater(this.getActivity()).inflate(R.menu.add_to_user_library, menu);
//inflater.inflate(R.menu.add_to_user_library, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.add_to_user_library, container, false);
// view code here
return view;
}
}
Menu XML:
<?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=".result.ResultActivity">
<item
android:id="#+id/save"
android:icon="#drawable/ic_check"
android:showAsAction="ifRoom"
android:title="#string/menu_save_library"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/go_back"
android:icon="#drawable/ic_clear"
android:showAsAction="ifRoom"
android:title="#string/menu_save_library"
tools:ignore="AppCompatResource" />
</menu>
From the activity
public final class ResultActivity extends CoreActivity {
public static void startActivityForResult(Activity a, Result r, int requestCode) {
Intent i = new Intent(a, ResultActivity.class);
i.putExtra(EXTRA_RESULT, r);
a.startActivityForResult(i, requestCode);
a.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.activity_result, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.case1:
{
Bundle b = new Bundle();
String myString = (String.valueOf(_result.myString));
b.putString("myString", myString);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);
String timeStamp = sdf.format(new Date(_result._timestamp));
b.putString("timeStamp", timeStamp);
String myString1 = _result.myString1;
b.putString("myString1", myString1);
//show the dialog
DialogFragment newFragment = new MyMainView();
newFragment.setArguments(b);
newFragment.show(getFragmentManager(),"SaveIt");
return true;
}
case case2:
{
//etc...
Right now, when the dialog shows up but the menu appears right before it loads then becomes blank.
What I see is the following.
When I click list item from search result, return to 0 position. How do I get original position from string?
I have a list with personalized rows, the situation is that when I click on an item I get a good result, the problem is that when I search and click it, I do not get the result I expected.
My code is:
String.xml
<resources>
<string name="app_name">SearchViewList</string>
<string-array name="array_country">
<item>Alemania</item>
<item>Mexico</item>
<item>EUA</item>
<item>Argentina</item>
<item>Rusia</item>
<item>EspaƱa</item>
</string-array>
</resources>
My code is:
MenuSearch.xml
<?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/menuSearch"
android:title="Buscar"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always"></item>
</menu>
My code is:
MainActivity.java
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lista);
ArrayList<String> arrayCountry = new ArrayList<>(); arrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country)));
adapter = new ArrayAdapter<>(
MainActivityRespaldo2.this, android.R.layout.simple_list_item_1, arrayCountry);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView adapterView, View view, int posicion, long l) {
switch (posicion) {
case 0 :
Intent i0 = new Intent(getApplicationContext(), Alemania.class);
startActivity(i0);
break;
case 1 :
Intent i1 = new Intent(getApplicationContext(), Mexico.class);
startActivity(i1);
break;
case 2 :
Intent i2 = new Intent(getApplicationContext(), EUA.class);
startActivity(i2);
break;
case 3 :
Intent i3 = new Intent(getApplicationContext(), Argentina.class);
startActivity(i3);
break;
default:
Toast.makeText(getApplicationContext(), "Developing", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_buscar, menu);
MenuItem item = menu.findItem(R.id.menuBuscar);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
}
Please help me
I get NPE on SearchView.OnQueryTextListener.
/res/menu/order_toolbar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/order_menu_search"
android:icon="#drawable/ic_search"
android:title="#string/order_menu_search"
app:actionViewClass="android.support.widget.v7.SearchView"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/order_menu_checkmark"
android:icon="#drawable/ic_checkmark"
android:title="#string/order_toolbar"
app:showAsAction="ifRoom|withText" />
ActivityOrder.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order_toolbar, menu);
mMenuCheckmark = menu.findItem(R.id.order_menu_checkmark);
mMenuCheckmark.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
FragmentOrder.java
#Override
public void onPrepareOptionsMenu(Menu menu){
MenuItem menuItem = menu.findItem(R.id.order_menu_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getActivity(), "" +newText, Toast.LENGTH_SHORT).show();
return false;
}
});
}
I'm inflating menu in Activity (i need to hide checkmark and enable it later after the user made some input, all the logic is in the activity), and in Fragment, I have RecyclerView and I need to implement search (in the toolbar). So basically, I need to access that SearchView INSIDE FRAGMENT only.
I have been trying to find anything but nothing helps, if I inflate menu in fragment then I get doubled icons in the toolbar.
setHasOptionsMenu(true);
is already added in onCreate
Also i tried this:
1. inflate the menu in fragment (removed onCreateOptionsMenu in activity)
2. passed menuItem (checkmark) back to activity trough interface so i can access checkmark
3. still getting NPE if i set listener on searchview
I make option menu in fragment. Like this,
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
...
setHasOptionsMenu(true);
...
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
menu.findItem(R.id.menu_1).setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_bar_search));
searchView = (SearchView) menu.findItem(R.id.menu_1).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
return false;
}
});
....
}
Not sure, you call "searchView.setOnQueryTextListener" from onCreateOptionsMenu.
Solved:
mMenuSearch = (SearchView) menu.findItem(R.id.order_menu_search).getActionView();
mMenuSearch.setOnQueryTextListener(this);
added implements SearchView.OnQueryTextListener on Fragment and 2 new Override methods onQueryTextSubmit and onQueryTextChanged