I want a options menu after the drawer toggle button like this app. And I want options menu in left, write both sides (don't wanna compromise any). Can anyone tell me how to do that?
-: My tool bar :-
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
-: My options menu :-
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.option_menu, menu);
}
-: In 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/0"
android:icon="#drawable/ic_0"
android:title=""
app:showAsAction="always"
android:orderInCategory="0"/>
<item
android:icon="#drawable/ic_1"
android:orderInCategory="1"
android:title=""
app:showAsAction="always">
<menu>
<item
android:id="#+id/2"
android:icon="#drawable/ic_2"
android:orderInCategory="2"
android:title="2" />
<item
android:id="#+id/3"
android:icon="#drawable/ic_3"
android:orderInCategory="3"
android:title="3" />
<item
android:id="#+id/4"
android:icon="#drawable/ic_4"
android:orderInCategory="4"
android:title="4" />
<item
android:id="#+id/5"
android:icon="#drawable/ic_5"
android:orderInCategory="5"
android:title="5" />
<item
android:id="#+id/6"
android:icon="#drawable/ic_6"
android:orderInCategory="6"
android:title="6" />
</menu>
</item>
</menu>
Related
I am really stuck with this.
I want to use a CAB in a fragment, so I'm using
actionMode = ((MainActivity)getActivity()).startActionMode(actionModeCallbacks);
to call startActionMode
I've been changing themes and adding and testing several lines of code (together and separated) to xml styles like:
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
Relevant information:
I have my toolbar defined inside and <include/>in my main activity.
Toolbar is included just before the frame of MainActivity, between that frame and the DrawerLayout, so I can use the toolbar in the fragments.
I have tested the CAB inside Main Activity, but it also gets above
the Toolbar.
I also tried a dirty workoaround toggling Toolbar visibility when CAB
is created/destroyed, but it also doesn't work!
I'm using android.view.ActionMode instead of
android.support.v7.view.ActionMode because I can't call
startActionMode if I use Support Library (?).
My toolbar (*android.support.v7.widget.Toolbar):
toolbar = findViewById(R.id.toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
toolbar.setTitle(R.string.addWordLabel_Act);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
XML (toolbar)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FAFAFA"
android:theme="#style/AppTheme.NoActionBar"
app:collapseIcon="#drawable/ic_arrow_back_white_24dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/addWordLabel_Act"
app:titleTextColor="#color/colorAccent"
/>
</android.support.constraint.ConstraintLayout>
ActionMode.Callback
private ActionMode actionMode;
private ActionMode.Callback actionModeCallbacks = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_mode_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share_actionMode:
shareWord();
mode.finish();
return true;
case R.id.deleteWords_actionMode:
deleteWords();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
XML (CAB)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Share"
android:icon="#drawable/ic_share_white_24dp"
android:id="#+id/share_actionMode"/>
<item android:id="#+id/deleteWords_actionMode"
android:title="Delete"
android:icon="#drawable/ic_round_delete_outline_white_24px"/>
</menu>
And styles...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/lightAccent</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="android:fontFamily">#font/sans_serif_roboto</item>
<item name="alertDialogTheme">#style/AlertDialogStyle</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
<style name="AppTheme.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
</style>
I don't know what else I can do.
Solved
I completely forgot to check Manifest, and that was the problem!
The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I completely forgot to check Manifest, and that was the problem! The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I have got Activity extends AppCompatActivity. I want to set searchview query after change screen orientation. I got NPE error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference
If i use setQuery in Fragment it works right. In Activity it does not work.
How can I change searchview query from activity ?
MyActivity.java
#Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bankmvp_menu, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchManager = (SearchManager) this.getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
getObservableSearchViewText();
return true;
}
#Override public void setQueryToSearch(String querystring) {
searchView.setQuery(querystring, true); //NPE error
}
bankmvp_menu.xml
<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"
tools:context=".MyActivity">
<item
android:id="#+id/action_setmonth"
android:orderInCategory="100"
android:title="#string/action_setmonth"
app:showAsAction="never" />
<item
android:id="#+id/action_setaccount"
android:orderInCategory="100"
android:title="#string/action_setaccount"
app:showAsAction="never" />
<item
android:id="#+id/action_nosaveddoc"
android:orderInCategory="100"
android:title="#string/action_nosaveddoc"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
To set query string from the Presenter to Searchview
Change menu.xml
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:iconifiedByDefault="true" //important
app:showAsAction="always" //important
Change activity, from Presenter set only string.
#Override public void setQueryToSearch(String querystringx) {
if( querystringx.equals("")){
}else {
querystring = querystringx;
}
}
Change activity setQuery in onResume of activity
if( querystring.equals("")){
}else {
searchView.setIconified(false);
searchView.setQuery(querystring, false);
menuItem.setVisible(true);
}
Screen after change orientation
I try to do a tuto about Menu.
My JAVA code :
public class MenuActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.myfirstmenu, menu);
return true;
}
}
My XML "R.layout.activity_menu" code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- nothing because I just want display a menu -->
</LinearLayout>
My XML "R.menu.myfirstmenu" code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Item 1"></item>
<item android:id="#+id/item2" android:titleCondensed="Item 2" android:title="Item 2 mais avec un nom assez long quand même">
<menu>
<item android:id="#+id/item3" android:title="Item 2.1" android:checkable="true"/>
<item android:id="#+id/item4" android:title="Item 2.2"/>
</menu>
</item>
<item android:id="#+id/item5" android:title="Item 3" android:checkable="true"/>
<item android:id="#+id/item6" android:title="Item 4">
<group android:id="#+id/group1" android:checkableBehavior="all">
<item android:id="#+id/item7" android:title="Item 4.1"></item>
<item android:id="#+id/item8" android:title="Item 4.2"></item>
</group>
</item>
<group android:id="#+id/group2" android:enabled="false">
<item android:id="#+id/item9" android:title="Item 5.1"></item>
<item android:id="#+id/item10" android:title="Item 5.2"></item>
</group>
</menu>
My problem is that when I run my app, nothing appear !
What I must do in order to display the three vertical dots and the app name ?
Thank for your answers ;)
Max
I have MainActivity.java which contains DrawerLayout / Navigation. I used the following code in the MainActivity to launch About.java activity:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent = new Intent(Intent.ACTION_VIEW);
if (id == R.id.nav_fav) {
} else if (id == R.id.nav_themes) {
} else if (id == R.id.nav_about) {
intent = new Intent(getApplicationContext(), About.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
The About activity launches fine and the icon/text colour of the item in the DrawerLayout/ Navigation highlights/ active but it remains highlighted/active even the About activity is finished by pressing back button.
About.java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
default:
}
return true;
}
MainActivity.xml:
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_main_drawer.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_menu_favorite"
android:title="#string/favorite" />
<item
android:id="#+id/nav_themes"
android:icon="#drawable/ic_menu_theme"
android:title="#string/themes" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="#string/about_us" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/share" />
<item
android:id="#+id/nav_rate"
android:icon="#drawable/ic_menu_rate"
android:title="#string/rate_us" />
<item
android:id="#+id/nav_more"
android:icon="#drawable/ic_menu_more"
android:title="#string/more_apps" />
</menu>
</item>
</menu>
Graphical representation of the problem:
Thank you #MikeM for providing solution to the problem.
I had <group android:checkableBehavior="single"> in the layout of menu items which caused the problem. I removed the code and this fixed the problem.
<group>
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_menu_favorite"
android:title="#string/favorite" />
<item
android:id="#+id/nav_themes"
android:icon="#drawable/ic_menu_theme"
android:title="#string/themes" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="#string/about_us" />
</group>
According to android the code does the following:
When a checkable item is selected, the system calls your respective
item-selected callback method (such as onOptionsItemSelected()). It is
here that you must set the state of the checkbox, because a checkbox
or radio button does not change its state automatically. You can query
the current state of the item (as it was before the user selected it)
with isChecked() and then set the checked state with setChecked().
Android Menu Documentation
I am trying to add toolbar to my app but somehow it is not working. Neither title not action icons are appearing. Even " : " menu is also not appearing. Here are my codes:
Style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Menu_main.xml:
<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"
tools:context="com.example.vaibhav.thirdeye.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/next"
android:title="#string/next"
android:orderInCategory="200"
android:icon="#drawable/next_arrow"
app:showAsAction="always"/>
</menu>
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back_splash"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.vaibhav.thirdeye.MainActivity"
tools:showIn="#layout/activity_main"
app:theme="#style/AppTheme.NoActionBar">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding Custom toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.next_arrow);
toolbar.setTitle("ThirdEye");
}
#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.action_settings:
Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
settings.show();
case R.id.next:
Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
next.show();
}
return true;
}
}
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/icon_size"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Output Screenshot:
Any helps????
Remove extra toolbars in your MainActivity's activity_main.xml layout, it will solve your problems.