The main XML
Need to access the Item click via id (nav_camera)
<?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:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Camera" />
</group>
This s Xml file
Try to access via following code.
public class Camera extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Menu nav_camera =(Menu)findViewById(R.menu.nav_camera);
}
}
need to access Item(nav_camera) via id;
Try this:
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_landing_page, menu);
super.onCreateOptionsMenu(menu, menuInflater);
MenuItem pinMenuItem = menu.findItem(R.id.menuItemID);
...
...
...
}
or please add more description on what do you want to do so I can provide more specific answer
Related
I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.
Here is the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the settings_menu.xml that should show the 3-dot menu.
<?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/item2"
android:title="Item 2"
app:showAsAction="never" />
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never">
<menu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</menu>
</item>
</menu>
Here is the onCreate() method of HomeActivity, where the menu DOES appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);
buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);
buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createClient();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
you have to override the onCreateOptionsMenu in every activity you need menu to be shown within
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
// return true so that the menu pop up is opened
return true;
}
check this thread for full details
You will have to forgive me, because I am still pretty new to android development. For my android app, I am staying to create a custom menu and place on the tool bar with the java code in a base activity that is extended by the other activities. But for some reason, the menu buttons are hiding in the overflow menu even though there is more than enough room for them to be showing. I feel like my code is correct, but I need to add a reference to something somewhere else that I am missing. Thanks in advance!
Current toolbar appearance
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolBar = (Toolbar) this.findViewById(R.id.toolbar);
if (toolBar != null) {
setSupportActionBar(toolBar);
if (getSupportActionBar() != null) {
setDisplayHomeEnabled(true);
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
protected abstract int getLayoutResource();
public void setDisplayHomeEnabled(boolean b) {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(b);
}
}
#Override
public void setTitle(CharSequence title) {
toolBar.setTitle(title);
}
#Override
public void setTitle(int titleId) {
toolBar.setTitle(titleId);
}
public Toolbar getToolBar() {
return toolBar;
}
}
toolbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
In your main_menu.xml file, add an 'app' tag for showAsAction as I have done below. It worked for me. See if that works for you too.
<?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/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
app:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"
I've been trying to get an Action Button working on my Android app, and been failing miserably. I've read so many questions and answers and nothing seems to work. The video link always just appears in the menu which I do not want
Here is the menu Xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:puertogaleradivesites="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/youtube"
android:icon="#drawable/ic_play_arrow_black_24dp"
android:title="YouTube"
puertogaleradivesites:showAsAction="ifRoom"/>
<item android:id="#+id/action_settings"
puertogaleradivesites:showAsAction="never"
android:title="Settings"/>
</menu>
And here is the main Java Fragment
public class DivesiteFragment extends Fragment {
private Divesite mDivesite;
private TextView mDepthField;
private TextView mSiteDescription;
private TextView mDistance;
private ProportionalImageView mImageView;
public static final String EXTRA_SITE_ID = "com.blueribbondivers.extraSiteID";
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UUID crimeId = (UUID)getActivity().getIntent()
.getSerializableExtra(EXTRA_SITE_ID);
mDivesite = DiveSites.get(getActivity()).getDiveSite(crimeId);
mContext = getActivity().getApplicationContext();
//getActivity().getActionBar().setTitle(Html.fromHtml("<font color=\"#1c3565\">" + mDivesite.getName() + "</font>"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_divesite, parent, false);
Resources resources = mContext.getResources();
//setHasOptionsMenu(true);
mDepthField = (TextView)v.findViewById(R.id.divesite_display_depth);
mDepthField.setText(mDivesite.getMaxDepth());
mSiteDescription = (TextView)v.findViewById(R.id.divesite_display_description);
mSiteDescription.setText(mDivesite.getSiteDescription());
mSiteDescription.setMovementMethod(new ScrollingMovementMethod());
mDistance = (TextView)v.findViewById(R.id.divesite_display_distance);
mDistance.setText(mDivesite.getTravelTime());
mImageView = (ProportionalImageView)v.findViewById(R.id.divesite_display_imageView);
String imageName = mDivesite.getPhoto();
final int resourceID = resources.getIdentifier(imageName,"drawable",mContext.getPackageName());
mImageView.setImageResource(resourceID);
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.divesitemenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
I have the full project on Github also https://github.com/Jonnyblue/PuertoGaleraDiveSites
Any help would be greatly appreciated.
For displaying menus like Youtbe, gmail:
You should use yourapp:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
yourapp:showAsAction="always"
android:title="#string/search"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
yourapp:showAsAction="always"
android:title="#string/refresh"/>
</menu>
Hope it will help you.
Your activity extends FragmentActivity. If you'd like a backward compatible action bar that works the same on all API levels (things changed significantly in API 21), you should instead extend AppCompatActivity
If you want to show an ActioBar item a as a button but not as an overflow menu, You need to define an android:orderInCategory for it. So that it doesn't comes inside the overflow menu.
<?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/action_search"
android:icon="#drawable/ic_magnify"
android:orderInCategory="100"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/noticiation_count"/>
</menu>
Also as you are using fragments its safe to use this line inside the onCreate() method,
setHasOptionsMenu(true);
Tested and working perfectly with Fragments.
I have 3 activities in my project and I would like to remove/hide the searchView in the actionbar on only one activity.
I tried searchView.setVisibility(View.GONE); but there is still the clickable icon.
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/item_clear_memory_cache"
android:title="#string/menu_item_clear_memory_cache"/>
<item
android:id="#+id/item_clear_disc_cache"
android:title="#string/menu_item_clear_disc_cache"/>
</menu>
menu.findItem(R.id.menu_search).setVisible(false); should be the way.
You can hide the searchview and the searchicon by doing this:
searchItem.setVisible(false);
searchView.setVisibility(View.GONE);
Then you can bring it back by:
searchItem.setVisible(true);
searchView.setVisibility(View.VISIBLE);
Well, after some researches here you can find my complete example. Don't forget to set visibility and visible back in activity or fragment where you need to show your search view back.
I was looking a way to hide search view in preferences.
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this means that we're telling the Activity that is hosting the Fragment that we want to display menus
setHasOptionsMenu(true);
}
/**
* ********************* SEARCH ********************
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) item.getActionView();
item.setVisible(false);
searchView.setVisibility(View.GONE);
}
}
And menu_main.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
/>