Menu - Activity empty - java

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

Related

Contextual Action Bar (CAB) does not overlay Toolbar

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.

Searchview setQuery in Activity

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

How to make options menu after drawer button?

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>

Toolbar icons not appearing in AppCompat Activity

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.

Toolbar cannot be customized as expected in android

I'm learning Material Design from video tutorial provided by slidenerd.In that they put one next icon on toolbar,everything is working in that tutorial but when i'm implemented it the icon is not appearing.
Here,i'm attaching my files please have a look and help me out at my learning stage.Please!!!
MainActivity.java
package materialtest.example.ritzipsy.mytoolbar5;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_settings) {
Toast.makeText(this, "You have Clicked Setting menu", Toast.LENGTH_SHORT).show();
}
if (id == R.id.id_navigate) {
Toast.makeText(this, "You have Clicked Next menu", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="materialtest.example.ritzipsy.mytoolbar5.MainActivity">
<include android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
</RelativeLayout>
my_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/id_settings"
android:title="Settings"
app:showAsAction="never">></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.co0m/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Got Output as
Expected output to be
Just change your menu xml as below. The problem is with this line
xmlns:app="http://schemas.android.com/apk/tools"
<?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/id_settings"
android:title="Settings"
app:showAsAction="never"></item>
<item
android:id="#+id/id_navigate"
android:icon="#drawable/ic_next_arrow"
android:orderInCategory="200"
android:title="Next"
app:showAsAction="always"></item>
</menu>

Categories