How to disable all the physical buttons present on a tablet device - android

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"
android:visible="false"/>
</menu>
I have tried doing this, But only the setting button gets invisible. I want to disable all the buttons present on my tab.
I have even tried doing ths
protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayHomeAsUpEnabled(false);
}
But no results obtained.

Hide elements in ActionBar:
If you want to disable the Up-Button you should to this:
getActionBar().setHomeButtonEnabled(false);
If you don't want a OptionsMenu than delete your onCreateOptionsMenu Method.
UPDATE 2013 11 22:
Hide System Navigation:
View rootView = getWindow().getDecorView().getRootView();
rootView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION );
But please read the API of this FLAG:
http://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_HIDE_NAVIGATION

Related

Handling the click on a SearchView's icon

First, I will explain to you what I want to do. Then, I will show you what I've already done. Finally, I will ask for your help about what I will implement.
My aim
My aim is to have a Toolbar like the Playstore:
When you click on the burger menu, it displays the menu.
When you click on the input, it displays changes the icon and it displays keyboard:
What I have for now
I followed the documentation (https://developer.android.com/training/appbar/ + https://developer.android.com/guide/topics/search/search-dialog + https://developer.android.com/training/search/setup). Thus, I have:
res/menu/action_bar_menu.xml containing my SearchView widget
<?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/searchView"
android:title="#string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
A Toolbar in my fragment's layout:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorRoyalRed"
android:theme="#style/ToolbarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
The style ToolbarStyle (see above):
<style name="ToolbarStyle">
<item name="android:background">#color/colorRoyalRed</item>
<item name="android:textColorPrimary">#color/colorRichYellow</item>
<item name="actionButtonStyle">#style/ToolbarActionButton</item>
</style>
<style name="ToolbarActionButton">
<item name="android:padding">0dp</item>
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
</style>
In the fragment class, I make use of the toolbar:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setTitle("");
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
Objects.requireNonNull(app_compat_activity).setSupportActionBar(toolbar);
app_compat_activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return view;
}
In the fragment class, I make use of the SearchView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menu_inflater) {
menu_inflater.inflate(R.menu.action_bar_menu, menu);
super.onCreateOptionsMenu(menu,menu_inflater);
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
SearchView search_view = (SearchView) menu.findItem(R.id.searchView).getActionView();
search_view.setIconifiedByDefault(false);
SearchManager search_manager = (SearchManager) Objects.requireNonNull(app_compat_activity).getSystemService(Context.SEARCH_SERVICE);
search_view.setSearchableInfo(Objects.requireNonNull(search_manager).getSearchableInfo(app_compat_activity.getComponentName()));
}
Well, there are other implementations (modifications concerning the manifest, etc.) but they are not important (note all the required changements to implement a simple search bar in the toolbar... more than 5 steps...).
What I would want to do next (help please!)
I don't want to use anything other than the menu items structure (thus I don't want to actually add the SearchView in the fragment's layout).
How could I change the SearchView's icon? For now it's a magnifying glass. It must be a burger menu. I tried to modify res/menu/action_bar_menu.xml by indicating a android:icon= to the SearchView but it didn't work. I also tried to use <item name="searchIcon">#drawable/ic_baseline_menu_24px</item> in the style ToolbarActionButton but it didn't work. Here the aim is to replace the glass by a burger menu icon.
How could I handle the click on the SearchView's burger menu icon (once it will be set)? Indeed, I will call my DrawerLayout::openDrawer function to open my menu.
How could I handle the click on the SearchView's content? Indeed, I will replace the burger menu by the left arrow (and, if clicked, keyboard will disapear and the burger menu will be displayed).
Am I using the wrong things?
I just followed Google's documentation. But it seems so complicated to me to realize that I may be on the wrong path? Should I stop using the menu items structure?
You may try SearchEditTextLayout available under this Google project -
https://android.googlesource.com/platform/packages/apps/Dialer/
These libraries may also help you to achieve your desired results.
SearchView and FloatingSearchView

Android: ActionBar action overflow is not shown in Android 4.3, Samsung Note 2

We switching back to showing Actionbar
(source: android.com)
But [3] action overflow menu is not shown (in Android 4.3, Samsung Note 2).
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
Activity class extends android.app.Activity (not ActionBarActivity)
I played to even setting all menu item to have android:showAsAction="never" (In that case no actions are on ActionBar)
Docs
http://developer.android.com/guide/topics/resources/menu-resource.html
Similar questions when using appcompat support library
Android ActionBar compat overflow menu not showing on sdk 10
how to show menu item under overflow icon in android api 8+
EDIT common_actions.xml has all actions as android:showAsAction="never"
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_home"
android:icon="#drawable/ic_default_image"
android:showAsAction="never"
android:title="#string/recommend"
/>
<item
android:id="#+id/action_downloadmanager"
android:icon="#drawable/icon_personal_light_download_manage"
android:showAsAction="never"
android:title="#string/downloadmanage"
/>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"
android:title="#string/search"
/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="never"
android:title="#string/setting"
/>
</menu>
This is a Samsung thing that's actually a reaaaaallly stupid UI decision in my opinion. Within Touchwiz for all versions AFAIK overflow in the ActionBar isn't supported, instead you have to hit the bottom left button where a Gingerbread-esque menu pops up.
With Toolbar in AppCompat though you can override that behaviour and it shows a normal overflow in your ActionBar, I wrote a short post about moving from ActionBar to Toolbar that you can check out here, which contains links to more resources.
Looking further I have found a similar question https://stackoverflow.com/questions/23906985/action-overflow-button-not-shown (and here too)
that actually lead to several options inside Android action bar not showing overflow
I went with
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
makeOverflowMenu();
}
private void makeOverflowMenu() {
//devices with hardware menu button don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Hardware menu button on Samsung Note 2 acts as if ActionBar action overflow menu was pressed.
That is correct behavior.
If you have a physical menu button (Samsung has it), Android will automatically hide the three dots.
If you don't, it will show them.

action bar showing large space between app icon and menu items

My action bar shows the app icon on the left, then has a large space, and then shows some of my menu items on the right (the rest go into overflow). The problem is that the "large space" is big enough to hold several more icons. Why isn't it being used?
My menu resource looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_a"
android:icon="#drawable/a"
android:title="a"
android:orderInCategory="100"
android:showAsAction="ifRoom" >
<menu>
<item ... />
...
</menu>
</item>
...
<item
android:id="#+id/menu_n"
android:icon="#drawable/n"
android:title="n"
android:orderInCategory="100"
android:showAsAction="ifRoom" >
...
</item>
</menu>
In my Activity, I have:
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView (R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeButtonEnabled (true);
...
}
and
#Override
public boolean onCreateOptionsMenu (Menu menu)
{
getMenuInflater().inflate(R.menu.nav_menu, menu);
return true;
}
I have a suspicion it has something to do with the app title (setDispalyShowTitleEnabled), which I have disabled. Maybe I'm doing it wrong?
Nothing wrong with your code.
AtionBar by default allows only 2-3 menu items even when there is enough room to accommodate 4-5.
May be to maintain better UI.
So, instead of
android:showAsAction="ifRoom"
use
android:showAsAction="always"
to have the MenuItems always present on the ActionBar instead of going into the overflow menu.

ICS ActionBar overflow menu item showing up on both top ActionBar and overflow menu

On a Galaxy S3, I have an issue where a button in the ActionBar is configured as showAsAction="always" and being shown on both the hardware overflow menu and the Action buttons. I would like it to not be shown on the hardware overflow menu and only on the Action buttons. I can disable the menuitem in the onCreateOptionsMenu but it will hide the button on both places.
Something to note: if I force the "3 dots" Action Overflow menu to show, the refresh button gets properly hidden from the hardware overflow menu but still doesn't get hidden from the hardware overflow menu.
Something else to note: if I call menu.size() in either onCreateOptionsMenu or onPrepareOptionsMenu, it doesn't reflect the extra button. For example, I have four buttons and the first button is being shown in both the Action buttons and the overflow menu. menu.size() still returns 4 and doesn't seem to realize that it is showing an extra button.
I can't post a screenshot because this is an app for a client but here is my actionbar.xml file. The refresh button shows in both the overflow and the action bar at the top.
actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refreshmenuitem"
android:icon="#drawable/refreshicon"
android:title="Refresh"
android:showAsAction="ifRoom"
android:visible="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<item android:id="#+id/helpbutton"
android:title="Help"
android:showAsAction="collapseActionView"
android:icon="#drawable/ic_action_helpbutton" />
<item android:id="#+id/settingbutton"
android:title="Settings"
android:icon="#drawable/ic_action_settingbutton"
android:showAsAction="collapseActionView" />
<item android:id="#+id/importbutton"
android:title="Import file"
android:icon="#drawable/ic_action_importbutton"
android:showAsAction="collapseActionView" />
</menu>
So I figured it out but it is kind of a hack. So if you have a phone such as the Samsung Galaxy S3 that has a hardware menu button, the onCreateOptionsMenu function actually gets called twice. Once for when the Activity gets loaded to load any menu items that should display in the top right and another time when the user presses the hardware menu button. All I did was create a variable called _firstTimeOnCreateOptionsMenu that is set to false after the first time it is onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu)
{
// Populates the actionbar/Menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
boolean hardware = false;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
hardware = ViewConfiguration.get(context).hasPermanentMenuKey();
}
MenuItem b1 = menu.findItem(R.id.refreshmenuitem);
if(!hardware || _firstTimeOnCreateOptionsMenu)
{
b1.setVisible(true);
_firstTimeOnCreateOptionsMenu = false;
}
else
{
b1.setVisible(false);
_firstTimeOnCreateOptionsMenu = true;
}
}
Hopefully this helps anyone else who is having this issue.
Is your targetSdkVersion set to 14?
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
I'd be a bit surprised if this was the issue, since you'd expect unknown XML to just be ignored, but according to the documentation <item> does not support android:layout_width and android:layout_height.

Android - Remove "More actions" button from the ActionBar

I have an ActionBar that should display the action buttons in a custom way. For this I created a custom view and attached it to the ActionBar.
One thing to mention is that I am using a menu.xml resoure file to load the options menu and display them on a smartphone, but do not display them on tablet, instead use a custom view. For this I market every menu item in the xml as: android:showAsAction="never"
Everything looks fine, except one little thing that still remains on the right of the ActionBar - the "More" button.
How can I remove it?
I tried this:
ActionBar bar = activity.getActionBar();
bar.removeAllTabs();
but the "more" button still remains there.
EDIT:
This is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_username"
android:icon="#drawable/menu_username"
android:orderInCategory="0"
android:showAsAction="never"
android:title="#string/menu_username">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/menu_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/menu_search"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_search"/>
</menu>
Please note I still want to inflate this menu on a smartphone, but don't want to use it on a tablet.
Setting showAsAction="never" will force an menu item into the overflow. Why not check in onCreateOptionsMenu(...) that the device is a tablet, and if it is just not inflate the menu? Something like this:
public boolean onCreateOptionsMenu(Menu menu) {
if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
//It's a tablet, don't inflate, only create the manual view
manualMenuCreation();
} else {
getMenuInflater().inflate(R.menu.menu, menu);
}
return true;
}
Don't forget smallestScreenWidthDp is only available in 3.2 or above, so you'll have to take that into consideration.

Categories

Resources