How to implement image with text in Menu options in android - java

i am trying so hard to implement customized menu option with both image and text in menu item list but i did not get it...so please help me how to implement it...thank you in advance
here is my 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="com.example.admin.sst.Signuplogin">
<item android:id="#+id/one" android:title="one"
app:showAsAction="never"
/>
<item android:id="#+id/two" android:title="two"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/three" android:title="three"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

You can use PopupMenu on any view clicked.
Full Demo:
MainActivity:
package pk.sohail.gallerytest.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.lang.reflect.Field;
import pk.sohail.gallerytest.R;
public class MainActivity extends Activity {
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, view);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.one:
Toast.makeText(getApplicationContext(), "1",
Toast.LENGTH_SHORT).show();
return true;
case R.id.two:
Toast.makeText(getApplicationContext(), "2",
Toast.LENGTH_SHORT).show();
return true;
case R.id.three:
Toast.makeText(getApplicationContext(), "3",
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
popupMenu.inflate(R.menu.main);
// Force icons to show
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popupMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass()
.getDeclaredMethod("setForceShowIcon", argTypes)
.invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and
// NoSuchFieldError
//
// In either case, an exception indicates something is wrong
// with the reflection code, or the
// structure of the PopupMenu class or its dependencies has
// changed.
//
// These exceptions should never happen since we're shipping the
// AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to
// display, so log the error and
// show the menu normally.
Log.w("as", "error forcing menu icons to show", e);
popupMenu.show();
return;
}
popupMenu.show();
}
});
}
}
activity_main.xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pop Menu"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
R.menu.main:
<?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"
tools:context="com.example.admin.sst.Signuplogin">
<item
android:id="#+id/one"
android:icon="#mipmap/ic_launcher"
android:title="one"
app:showAsAction="never" />
<item
android:id="#+id/two"
android:icon="#mipmap/ic_launcher"
android:orderInCategory="100"
android:title="two"
app:showAsAction="never" />
<item
android:id="#+id/three"
android:icon="#mipmap/ic_launcher"
android:orderInCategory="100"
android:title="three"
app:showAsAction="never" />
</menu>

Related

Menu won't show up. I don't know why

I'm just following another tutorial but the result is different. When I'm trying to run my apk, menu icon won't show up on primary layout (activity_main.xml) Anyone knows what I missed?
menu_main_activity.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">
<!-- NEW NOTE -->
<item
android:id="#+id/action_create"
android:enabled="true"
android:icon="#android:drawable/ic_menu_add"
android:orderInCategory="0"
android:title="create"
android:visible="true"
app:showAsAction="always" />
<!-- SETTINGS -->
<item
android:id="#+id/action_settings"
android:enabled="true"
android:icon="#android:drawable/ic_menu_manage"
android:orderInCategory="10"
android:title="settings"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
MainNote.java:
package com.example.lenovo.home;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.view.View.OnClickListener;
public class MainNote extends AppCompatActivity {
private ListView mListNotes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_note);
mListNotes = (ListView) findViewById(R.id.main_listview);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
break;
case R.id.action_settings:
//TODO show settings activity
break;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
super.onResume();
//load saved notes into the listview
//first, reset the listview
mListNotes.setAdapter(null);
ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());
//sort notes from new to old
Collections.sort(notes, new Comparator<Note>() {
#Override
public int compare(Note lhs, Note rhs) {
if (lhs.getDateTime() > rhs.getDateTime()) {
return -1;
} else {
return 1;
}
}
});
if (notes != null && notes.size() > 0) { //check if we have any notes!
final NoteAdapter na = new NoteAdapter(this, R.layout.view_note_item, notes);
mListNotes.setAdapter(na);
//set click listener for items in the list, by clicking each item the note should be loaded into NoteActivity
mListNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//run the NoteActivity in view/edit mode
String fileName = ((Note) mListNotes.getItemAtPosition(position)).getDateTime()
+ Utilities.FILE_EXTENSION;
Intent viewNoteIntent = new Intent(getApplicationContext(), NoteActivity.class);
viewNoteIntent.putExtra(Utilities.EXTRAS_NOTE_FILENAME, fileName);
startActivity(viewNoteIntent);
}
});
} else { //remind user that we have no notes!
Toast.makeText(getApplicationContext(), "you have no saved notes!\ncreate some new notes :)"
, Toast.LENGTH_SHORT).show();
}
}
}
And, the last is my activity_main_note.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.lenovo.home.MainNote">
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</LinearLayout>
UPDATE
This is my styles code
<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.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
This is my theme code for application.
I have just run your code and it works fine.
I am running it with the following theme in styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
In manifest I have android:theme="#style/AppTheme" in the application tag.
And my Activity extends AppCompatActivity
Also check you have put the menu xml in res/menu folder.
The only thing I see but it is regarding the item being selected is that the return statement is wrong, but it doesn't have to do with the icons not appearing.
In onOptionsItemSelected(MenuItem item) you should return true if you handle the request or false if not.
Change it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean rtn = false;
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
rtn = true;
break;
case R.id.action_settings:
//TODO show settings activity
rtn = true;
break;
default:
rtn = super.onOptionsItemSelected(item);
}
return rtn;
}
See this question: Should "android: onOptionsItemSelected" return true or false
EDIT I
I see in your styles.xml you are extending Theme.AppCompat.Light.NoActionBar, which doesn't have an ActionBar.
In this case you can switch to a theme with an ActionBar as I am using or otherwise you need to add your own action bar to the layout using a Toolbar.

Android overflow menu overlapping actionbar

I'm trying to make a menu with two items, "Exit" and "Settings", where the "Exit" item is located on the actionbar, while the "Settings" item is located in the overflow menu.
However when I click on the menu icon, the overflow menu overlaps the "Exit" item. Is there anyway I can prevent this behaviour?
This is what's currently happening
This is what I'm trying to achive
Menue.XML
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:title="#string/setting"
android:id="#+id/setting"
android:orderInCategory="2"
app:showAsAction="never"
/>
<item android:title="#string/exit"
android:id="#+id/exit"
android:orderInCategory="1"
app:showAsAction="withText|ifRoom"
android:icon="#drawable/exit"
/>
</group>
</menu>
Activity_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"
tools:context="com.example.meunueexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
Main_Activity.Java
package com.example.meunueexample;
import android.app.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) { // it wil inflate the menue in the action bar
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) { // here we will get the selected item form group option
int id = item.getItemId(); // will get id of selected item these IDs are not user defined
if (id == R.id.setting) {
android.app.DialogFragment myfragment= new DialogFragment(); // made object of dialog fragment
myfragment.show(getFragmentManager() , "thedialog"); // thedialog is object created in DialogFragment class
return true;
} else if (id == R.id.exit) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Dialog_Fragment.Java
package com.example.meunueexample;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by AQ on 9/10/2017.
*/
public class DialogFragment extends android.app.DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) { // override this method to ake dialog box
AlertDialog.Builder thedialog = new AlertDialog.Builder(getActivity()); // jiss activity ka oper is na show hona hy
thedialog.setTitle("Sample Dialog");
thedialog.setMessage("Hlw AQ");
thedialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // on click listener on button clicked
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();
}
}); // place semicolon there
thedialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // on click listener on button clicked
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel", Toast.LENGTH_SHORT).show();
}
}); // place semicolon there
return thedialog.create();
} // may change abndroid version from android manifest and from build.gradle
}
You can change this behaviour by creating a style with a vertical offset, like so:
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<item name="android:dropDownVerticalOffset">-4.0dip</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
<item name="android:dropDownVerticalOffset">4.0dip</item>
</style>
You can then apply this style in your theme:
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>

How to make a fixed BottomNavigationView with title below, no shifted navigation

I am having difficulties in making fixed BottomNavigationView like in the Photo below: OriginalPhoto.png
Here is my own project's BottomNavigationView: MyPhoto.png
Also, I want the title below icons visible all the time, not just when clicked. How can I do this??
Note: I have read all stackoverflow articles and followed other external links but no tangible results have gained. Please help me to figure this subtle problem out. Would be better if only with XML to solve this problem, rather than java code.
Here is my source code for activity_menu.xml, btw, it is not activity_main.xml because i have used main activity for my login page. This activity is after login page: `
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="datasite.com.konnex.Menu"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3fc0ea">
<ImageButton
android:layout_width="120dp"
android:layout_height="38dp"
android:background="#drawable/lg1"
android:layout_marginLeft="130dp"
android:layout_marginStart="130dp"/>
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"/>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<FrameLayout
android:layout_width="191dp"
android:layout_height="150dp"
android:id="#+id/fm_cases"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/cases"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="35dp"
android:id="#+id/img_cases" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cases"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:layout_marginTop="128dp"
android:textSize="18sp"
android:textColor="#424242"/>
<TextView
android:layout_width="22dp"
android:layout_height="wrap_content"
android:text="#string/string_1"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:layout_marginLeft="122dp"
android:layout_marginStart="122dp"
android:layout_marginTop="35dp"
android:background="#E91E63"
android:id="#+id/notif_cases"/>
</FrameLayout>
</GridLayout>
</RelativeLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fffafa"
app:menu="#menu/navigation"
app:itemIconTint="#color/dark"
app:itemTextColor="#color/dark"
android:animateLayoutChanges="false"
android:splitMotionEvents="false"
android:fitsSystemWindows="true"/>
</LinearLayout>`
Here is the source code for menu.java. it is also after main.java activity which is for login used.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
}
Thank you in advance!!! I really need your help Guys)
My Navigation.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/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
/>
</menu>
Try using AHBottomNavigation library, I suggest this because is has simple implementation considering your case and reduces your work to create another menu.xml file and handling menu option methods in your code. Just add this line in your gradle file.
compile 'com.aurelhubert:ahbottomnavigation:2.0.6'
It will take care of the layout issue you have moreover, it has lots of customization options. You can read further here, but for your case the simple use to keep titles is..
public class MyActivity extends AppCompatActivity{
#BindView(R.id.bottom_navigationbar)
AHBottomNavigation bottomNavigation;
onCreate(){
bottomNavigation.addItem(new AHBottomNavigationItem("Title1", iconID1);
bottomNavigation.addItem(new AHBottomNavigationItem("Title2", iconID2);
bottomNavigation.addItem(new AHBottomNavigationItem("Title3", iconID3);
bottomNavigation.addItem(new AHBottomNavigationItem("Title4", iconID4);
bottomNavigation.setAccentColor(ContextCompat.getColor(this, themeColor));
//will always show titles
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
}}
setUpClickListener.
bottomNavigation.setOnTabSelectedListener((position, wasSelected) -> {
if (position == 0 && !wasSelected) {
} else if (position == 1 && !wasSelected) {
} else if (position == 2 && !wasSelected) {
} else if (position == 3 && !wasSelected) {
} else if (position == 4 && !wasSelected) {
}
return true;
}
);
Ok then try to add app:showAsAction in menu item
<?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/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_about" />
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
app:showAsAction="ifRoom"
android:title="#string/title_location"
/>
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
app:showAsAction="ifRoom"
android:title="#string/title_phone"
/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
app:showAsAction="ifRoom"
android:title="#string/title_home"
/>
</menu>
If your problem not resolve then replace this one showAsAction="always|withText"
after then you can use this method for disabling shifted menu
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
Updated Menu.java: `package datasite.com.konnex;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Field;
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//Add This
disableShiftMode((BottomNavigationView)findViewById(R.id.navigation))
FrameLayout fml = (FrameLayout) findViewById(R.id.fm_cases);
fml.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Cases.class);
startActivity(i);
}
});
}
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
Updated Navigation.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/nav_about"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_about"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_location"
android:icon="#drawable/nav_location"
android:title="#string/title_location"
app:showAsAction="always|withText" />
<item
android:id="#+id/nav_phone"
android:icon="#drawable/nav_call"
android:title="#string/title_phone"
app:showAsAction="always|withText"/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home"
app:showAsAction="always|withText" />
</menu>
`

How to make option menu appear on bottom of the screen?

Here is what I am trying to implement: when people click on the menu on top right corner of a toolbar, an options menu appears on bottom of the screen. See picture below:
I am not sure what method should I call for the item at the bottom. Can somebody give me some hint on how to implement this?
I implement successfully the icon in the top right menu bar with the code below. But I don't know how to show the options at the bottom of the screen, with width match_parent, and height wrap_content
onClick on the right top corner
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_profile_image_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.more:
//How to show the 2 option in the bottom of screen here
return true;
}
return super.onOptionsItemSelected(item);
}
Update
After implement the code of Nikesh, the popup shown like this:
UPDATED ANSWER
You can now use Material Design BottomAppBar
A bottom app bar displays navigation and key actions at the bottom of mobile screens
SAMPLE CODE
Add below dependencies in your build.gradle
implementation 'com.google.android.material:material:1.0.0'
Now create 3 menu file in res/menu directory
bottom_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/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search"
android:title="Search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/app_bar_fav"
android:icon="#drawable/ic_favorite"
android:title="Favorite"
app:showAsAction="ifRoom"/>
<item
android:icon="#drawable/ic_favorite"
android:title="Favorite"
app:showAsAction=""/>
</menu>
nav_drawer_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/apk/res-auto">
<item
android:id="#+id/navItemOne"
android:title="Nav Item 1"
app:showAsAction="never"/>
<item
android:id="#+id/navItemTwo"
android:title="Nav Item 2"
app:showAsAction="never"/>
<item
android:id="#+id/navItemThree"
android:title="Nav Item 3"
app:showAsAction="never"/>
</menu>
toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/app_bar_fav"
android:icon="#drawable/ic_favorite"
android:title="Favorite"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search"
android:title="Search"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/app_bar_settings"
android:title="Settings"
app:showAsAction="never"/>
<item
android:title="Menu Item 1"
app:showAsAction="never"/>
<item
android:title="Menu Item 2"
app:showAsAction="never"/>
<item
android:title="Menu Item 3"
app:showAsAction="never"/>
</menu>
Now Create a class name BottomNavigationDrawerFragment to open navigation drawer from bottom
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.android.synthetic.main.bottom_nav_layout.*
class BottomNavigationDrawerFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottom_nav_layout, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
navigation_view.setNavigationItemSelectedListener { menuItem ->
// Bottom Navigation Drawer menu item clicks
when (menuItem!!.itemId) {
R.id.navItemOne -> context!!.toast(" Nav item one is Clicked ")
R.id.navItemTwo -> context!!.toast(" Nav item Two is Clicked ")
R.id.navItemThree -> context!!.toast(" Nav item Three is Clicked ")
}
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
true
}
}
// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
toast.setGravity(Gravity.BOTTOM, 0, 600)
toast.show()
}
}
Code of MainActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// attach menu to your BottomAppBar
bottomBar.replaceMenu(R.menu.bottom_menu)
// handle click event of navigationIcon
bottomBar.setNavigationOnClickListener {
toast("Navigation Icon Clicked")
val bottomNavDrawerFragment = BottomNavigationDrawerFragment()
bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
}
// handle click event of FloatingActionButton
fab.setOnClickListener {
toast("Fab Icon Clicked")
}
//handle click event of menu of BottomAppBar
bottomBar.setOnMenuItemClickListener { menuItem ->
when (menuItem!!.itemId) {
R.id.app_bar_search -> toast("Search menu of bottomBar is clicked!")
}
true
}
}
// Overriding Actionbar menu
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.toolbar_menu, menu)
return true
}
//handle click event of menu of Actionbar
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) {
R.id.app_bar_fav -> toast("Fav menu item of toolbar is clicked!")
R.id.app_bar_search -> toast("Search menu item of toolbar is clicked!")
R.id.app_bar_settings -> toast("Settings item of toolbar is clicked!")
else -> toast("Menu item of toolbar is clicked!")
}
return true
}
// method to display toast
private fun toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fitsSystemWindows="true">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomBar"
style="#style/Widget.MaterialComponents.BottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleVerticalOffset="4dp"
android:backgroundTint="#color/colorPrimary"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/ic_drawer"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bottomBar"
app:srcCompat="#drawable/ic_apps" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
bottom_nav_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/nav_drawer_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>
SAMPLE CODE for JAVA Lover
BottomNavigationDrawerFragment
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import neel.com.bottomappbar.R;
public class BottomNavigationDrawerFragment extends BottomSheetDialogFragment {
private Context mContext;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext=context;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.bottom_nav_layout, container, false);
NavigationView navigationView=rootView.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.navItemOne:
Toast.makeText(mContext, "Nav item One is Clicked ", Toast.LENGTH_SHORT).show();
return true;
case R.id.navItemTwo:
Toast.makeText(mContext, "Nav item Two is Clicked ", Toast.LENGTH_SHORT).show();
return true;
case R.id.navItemThree:
Toast.makeText(mContext, "Nav item Three is Clicked ", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
MainActivity
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import neel.com.bottomappbar.R;
public class MainActivity extends AppCompatActivity {
BottomAppBar bottomAppBar;
FloatingActionButton floatingActionButton;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomAppBar = findViewById(R.id.bottomBar);
// attach menu to your BottomAppBar
bottomAppBar.replaceMenu(R.menu.bottom_menu);
// handle click event of navigationIcon of bottomAppBar
bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BottomNavigationDrawerFragment bottomNavigationDrawerFragment = new BottomNavigationDrawerFragment();
bottomNavigationDrawerFragment.show(getSupportFragmentManager(), bottomNavigationDrawerFragment.getTag());
}
});
//handle click event of menu of BottomAppBar
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.app_bar_search:
Toast.makeText(MainActivity.this, "Search menu of bottomBar is clicked!", Toast.LENGTH_SHORT).show();
return true;
case R.id.app_bar_fav:
Toast.makeText(MainActivity.this, "Favorite menu of bottomBar is clicked!", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Fab Is clicked ", Toast.LENGTH_SHORT).show();
}
});
}
// Overriding Actionbar menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
//handle click event of menu of Actionbar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.app_bar_fav:
Toast.makeText(MainActivity.this, "Fav menu item of toolbar is clicked!", Toast.LENGTH_SHORT).show();
return true;
case R.id.app_bar_search:
Toast.makeText(MainActivity.this, "Search menu item of toolbar is clicked!", Toast.LENGTH_SHORT).show();
return true;
case R.id.app_bar_settings:
Toast.makeText(MainActivity.this, "app_bar_settings", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
for more information please check below articles
Bottom App Bar. How to start
Implementing Google’s newly launched Bottom AppBar
Implementing BottomAppBar I: Material Components for Android
Bottom App Bars
OUTPUT
https://www.youtube.com/watch?v=k145bGLrleo
OLD ANSWER
try this attach this PopupMenu in your bottom view click event something like button click event
step 1
create a view at bootom of your layout like this
<View
android:layout_gravity="center"
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
step 2 create a new_menu.xml file like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/temp"
android:title="#string/str_menu_account_logout"
android:icon="#drawable/next"
app:showAsAction="ifRoom"></item>
</menu>
now add this code to create option menu in your java file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.temp) {
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.myView),Gravity.CENTER);
popupMenu.inflate(R.menu.home_menu);
popupMenu.show();
return true;
}
return false;
}
I will suggest to use the Material design App Bar(Bottom), with few modifications you can get the result.
Try this https://material.io/design/components/app-bars-bottom.html
hope this will help.
Simple.
Make a dialog with its own contents which will makeup the bottom menu.
When user clicks there above then the dialog should show but its position would be below i.e. anchored with the bottom. You can find many codes regarding how to position dialog at the bottom of the scree anchored!! SO not adding any.Hope it helps.!
Answer by #Nilesh is very elaborate and bit complicated.
Simple Solution
Create FrameLayout and arrange your buttons inside frame layout depending on your requirements. Set id -- I use id = "menu"
Set visibility to gone android:visibility = "gone"
On menubutton click
findViewById(R.id.menu).setVisibility(View.VISIBLE);
Now to make menu disappear when you click outside the menu, add an empty view or layout behind the menu. set ontouch listner. Set visibility GONE when the background view is touched.
there is no need to reinvent the wheel; use a BottomNavigationView, which is being provided by:
dependencies {
api "com.google.android.material:material:1.0.0"
}
it can be added into the layout alike this (set gone by default):
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="start"
android:visiblity="gone"
app:menu="#menu/bottom" />
the menu/bottom.xml:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_search" />
<item android:id="#+id/action_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_add" />
</menu>
then hook into the onMenuOpened() event:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
this.mBottomNavigationView.setVisibility(View.VISIBLE);
return super.onMenuOpened(featureId, menu);
}
and also implement method onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
...
this.hideBottomNavigation();
return true;
case R.id.action_settings:
...
this.hideBottomNavigation();
return true;
default:
this.hideBottomNavigation();
return super.onOptionsItemSelected(item);
}
}
private void hideBottomNavigation() {
this.mBottomNavigationView.setVisibility(View.GONE);
}
BottomSheetDialogFragment would indeed be the other option available, while it does not support inflating a menu, but a fragment ...nevertheless it would merely be the same approach.
You can now use Material Design BottomAppBar
1st add this in build.gradle where all are being implemented
implementation 'com.google.android.material:material:1.0.0'
create menu
anyname.xml
2nd add coordinator layout and inside it bottomAppBaradd menu and navigation as given here
3rd Add code by initialising
private var mBottomAppBar: BottomAppBar ?= null
Then add them into the method
//bottom bar
mBottomAppBar = findViewById(R.id.bar)
mBottomAppBar?.setOnMenuItemClickListener{
when(it.itemId){
R.id.itemConfig->showConfigBottomSheetDialogFragment()
}
true
}
mBottomAppBar?.setNavigationOnClickListener{
startContentHighlightActivity()
}
}
The navigation will directly work and the menu will work according to the items you will add.
//This is click event of button
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.buttonSort:
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.myView),Gravity.CENTER);
popupMenu.inflate(R.menu.bottom_menu);
popupMenu.show();
break;
}
This is my home_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/apk/res-auto">
<item
android:id="#+id/bottom_menu_price"
android:title="Price low to high"
android:icon="#drawable/ic_price_low"
/>
<item
android:id="#+id/bottom_menu_price_high"
android:title="Price high to low"
android:icon="#drawable/ic_price_high"
/>
<item
android:id="#+id/bottom_menu_date"
android:title="New Added"
android:icon="#drawable/ic_date_sort"
/>
<item
android:id="#+id/bottom_menu_rate"
android:title="Best Rated"
android:icon="#drawable/ic_rate_sort"
/>

Android setting custom actionbar

In my project i would to set a custom actionBar (with a custom layout) after a specific action.
I have this simple Activity:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
#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_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//apriImpostazioni ();
return true;
case R.id.information_item:
//apriInformazioni ();
return true;
case R.id.search_item:
apriBarraRicerca ();
System.out.println ("IL BOTTONE RICERCA E' PREMUTO");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void apriBarraRicerca () {
getActionBar ().setCustomView(getLayoutInflater().inflate(R.layout.search_layout, null));
}
}
Where "menu_main_activity2" is this 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="zipangulu.myapplication.MainActivity2">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/information_item"
android:title="Info"
android:icon="#drawable/info"
android:orderInCategory="100"
app:showAsAction="always"/>
<item android:id="#+id/search_item"
android:title="Search"
android:icon="#drawable/search_icon"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
I would, pressing the search_item on the main actionbar, set a custom actionBar whith this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#4584d3">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:background="#null"
android:src="#drawable/back_icon"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:id="#+id/campo_ricerca"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:spinnerMode="dropdown"
android:id="#+id/spinner_anno"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/search_icon"
android:id="#+id/avvia_ricerca"/>
</LinearLayout>
But at runtime I have a NullPointerException in the body of "apriBarraRicerca" method..why?How can I fix this problem?
EDIT: as suggested I replaced "getActionBar ()" with "getSupportActionBar ()", now I don't have any exception but nothing happens.
EDIT2: I added getSupportActionBar().setDisplayShowCustomEnabled(true); and now my custom actionbar is visible but not as i want, look at the following image:
http://bit.ly/1Dc2kGg
The bar is visible but cutted, and are visible also the items of the previous actionBar..
You can try do the same thing with the android.support.v7.widget.ToolBar
Your Layout which you can populate as you desire:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#688A08"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
You have to modify the values->styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
And the in the onCreate() method you do the following:
Toolbar tb = (Toolbar)findViewById(R.id.top_bar);
if(tb != null){
setSupportActionBar(tb);
}
Hope it helps!!!

Categories