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.
Related
I am trying to implement a search Filter in my Fragment, but it does not let me. I used the same code in Activity before and it worked. Whenever I type something, the onCreateOptionsMenu() does not get called. The App looks like this : [![app Picture][1]][1]
As you can see I type something in the top and it won't get filtered.
My Fragment code: `
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setMenuVisibility(false);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_tab1, container, false);
if(getArguments() != null){
String yourText = getArguments().getString("interessen");
System.out.println(yourText);
}`
.
.
.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuevents, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem searchitem = menu.findItem(R.id.searchEvent);
//searchitem.setVisible(false);
final SearchView searchView = (SearchView) searchitem.getActionView();
final List<Event> allEvents = new ArrayList<>();
allEvents.addAll(eventList);
searchitem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
eventList.clear();
eventList.addAll(allEvents);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
eventList.clear();
eventList.addAll(allEvents);
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
eventListAdapter.getFilter().filter(newText);
return false;
}
});
I did not copy the EventListAdapter in, becuase this should work just fine. Its just that the Fragment does not get Access to the SearchItem.
Anybody? :)
[1]: https://i.stack.imgur.com/JkLcL.png
You've called setMenuVisibility(), which means your Fragment is specifically opting out of having its menu being shown. It is expected that when your menu isn't shown, onCreateOptionsMenu() is not triggered.
Remove that line of code to have your menu actually be visible.
I'm having trouble implementing a backbutton in the ActionBar in a Fragment. Since this is a Fragment I don't have access to getSupportActionBar(); and each time I use this, or similar code:
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
only results in NullPointerExceptions.
I've looked at numerous similar questions on StackOverflow but most of them are specified to Activities or AppCompatActivities, which do not work in Fragments. Using
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
only results in NullPointerExceptions. The other similar questions and answers have not helped me with this problem so I had to create a new topic.
This is where I get the error:
public class ExampleFragment extends Fragment{
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); //This results in NullPointerException
inflater.inflate(R.menu.example_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
getActivity().getSupportFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
}
You need to receive supportActionBar.
public class ExampleFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
ActionBar actionBar = ((AppCompatActivity)requireActivity()).getSupportActionBar();
if (actionBar!=null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
requireActivity().getSupportFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
}
I did not get this to work so I changed the structure of the View and made it to an Activity which has more support in dealing with this issue.
#Override
protected void onCreate(#Nullable Bundle savedInstance){
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
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
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 was having some problem when trying to make my navigation drawer in Android accessible from all Activity. I have a NavigationDrawer.java:
public class NavigationDrawer extends Activity {
static Context context;
private DrawerLayout mDrawerLayout;
private ExpandableListView mDrawerList;
private LinearLayout navDrawerView;
CustomExpandAdapter customAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mEventSelection;
private String[] mProfileSelection;
private int selectedPosition;
List<SampleTO> listParent;
HashMap<String, List<String>> listDataChild;
ArrayList<Event> newsFeedList = new ArrayList<Event>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
mTitle = mDrawerTitle = getTitle();
navDrawerView = (LinearLayout) findViewById(R.id.navDrawerView);
mEventSelection = getResources().getStringArray(R.array.event_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.nav_left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
listParent = new ArrayList<SampleTO>();
listDataChild = new HashMap<String, List<String>>();
listParent.add(new SampleTO(getString(R.string.eventDrawer),
R.drawable.event));
listDataChild.put(getString(R.string.eventDrawer),
Arrays.asList(mEventSelection));
customAdapter = new CustomExpandAdapter(this, listParent, listDataChild);
mDrawerList.setAdapter(customAdapter);
mDrawerList.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
// Navigation drawer with sub menu goes here
public void selectItem(int groupPosition, int position) {
selectedPosition = position;
mDrawerLayout.closeDrawer(navDrawerView);
if (groupPosition == 0) {
switch (selectedPosition) {
case 0:
break;
case 1:
break;
}
// Navigation item for event
else if (groupPosition == 3) {
switch (selectedPosition) {
case 0:
Intent eventMain = new Intent(context, EventMain.class);
context.startActivity(eventMain);
break;
case 1:
Toast.makeText(NavigationDrawer.this, "Analyze Event",
Toast.LENGTH_LONG).show();
break;
}
}
setTitle(mEventSelection[selectedPosition]);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
public void onResume() {
super.onResume();
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
int index = parent.getFlatListPosition(ExpandableListView
.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
String parentTitle = ((SampleTO) customAdapter
.getGroup(groupPosition)).getTitle();
if (parentTitle.equals(getString(R.string.amenityDrawer))) {
Toast.makeText(NavigationDrawer.this, "Amenity",
Toast.LENGTH_LONG).show();
setTitle(parentTitle);
mDrawerLayout.closeDrawer(navDrawerView);
}
return false;
}
});
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
int index = parent.getFlatListPosition(ExpandableListView
.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
selectItem(groupPosition, childPosition);
return false;
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(navDrawerView);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
And then I navigate to my second class which is EventMain.java when the sub item from navigation drawer is selected:
public class EventMain extends NavigationDrawer {
public static MapView mMapView = null;
ArcGISTiledMapServiceLayer tileLayer;
LocationManager locationManager;
public static GraphicsLayer graphicsLayer = null;
public static Callout callout;
private int mYear, mMonth, mDay, mHour, mMinute;
private Calendar c;
static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 2;
private LinearLayout legendDiv, llNewsFeed, llSearch;
private ListView listview;
private Button btnNewsFeed, btnSearchAddr, btnLegends;
private EditText searchAddrET;
private ImageView ivEventGuide;
public static LinearLayout directionDiv;
public static TextView tvDirection, tvDirectionTitle, tvSearchTitle;
static EventController eventCtrl = new EventController();
static Event eventModel = new Event();
private ListAdapter mAdapter;
ArrayList<Event> newsFeedList = new ArrayList<Event>();
#Override
public void onCreate(Bundle savedInstanceState) {
ArcGISRuntime.setClientId("UkIVSWzquykoxCMG");
super.onCreate(savedInstanceState);
setContentView(R.layout.event_main);
context = this;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event_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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
It did shows the navigation drawer icon at the EventMain.java. However, when I try to expand it, it does not work. Any ideas?
Thanks in advance.
The xml layout of my EventMain:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/event_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
initExtent="21801.3, 25801.0, 33218.7, 44830.0" >
</com.esri.android.map.MapView>
//Other linear and relative layouts
</FrameLayout>
</LinearLayout>
In EventMain's onCreate() method, setContentView() is being called after the call to the super.onCreate() method. This is causing the DrawerLayout set in the base class, NavigationDrawer, to be replaced, effectively removing the Drawer. To prevent this, we ensure the DrawerLayout has the standard container FrameLayout for main content, and inflate EventMain's layout into that.
The base Activity's layout, main.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/event_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
</android.support.v4.widget.DrawerLayout>
And in the Activity subclass, EventMain:
#Override
public void onCreate(Bundle savedInstanceState) {
...
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.event_frame);
getLayoutInflater().inflate(R.layout.event_main, content, true);
...
}