To keep this simple: I have tabs in my actionbar, but the action bar take up too much space. I want that extra space. I need a way to hide the action bar, yet keep my tabs. Is there anyway to do this? Or is there a way I can get the tabs built into the action bar like it is in landscape mode? Thanks!
You can have an empty Actionbar, then the tabs will occupy the space:
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Try the below code:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Also remove the below in code which is added default when project is created:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
This did the trick for me
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
I also commented the line
getMenuInflater().inflate(R.menu.main, menu);
Ahmad's answer is correct but it requires API 11. For supporting lower APIs, use this code -
setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
To enable it, use -
setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
Related
I'd like to hide the Action Bar of my Fragment.
I searched on Google for 1-2h. But I didn't find any proper anwser...
So if you could help me that would be nice.
You can try using getActivity().getActionBar().hide();
It is called ActionBar You can hide Action Bar using getSupportActionBar().hide();
I recently tried something different for my on development app but don't know how it works. I'm trying to add different actions (eg. Search)to each Tabs in more than one toolbar. I don't know how to explain my view well but the images below can make you understand what I'm trying to.
In the Contacts tab there is a add user icon in toolbar and in the calls tab you can see add calls icon in toolbar and in the Chats tab you can see chats icon in toolbar. My question is how to give different ifRoom settings to different tabs like the above app. Hope it is well explained to get the right answer :)
Thanks in Advance :)
Follow this code.
Add toolbar in your Activity, like you did.
Create menu for each fragment res/menu/menu_one.xml and ...
Add onCreateOptionsMenu and onOptionsItemSelected in each fragment, like this:
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);// add menu which you created in step 2
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// do stuff, like showing settings fragment
return true;
}
return super.onOptionsItemSelected(item); // important line
}
And in onCreate add this like: setHasOptionsMenu(true);
And it is it! :)
I know it has been asked several times before. But I guess I have to ask the question myself because none of the existing ones fits my problem:
I am developing for Kitkat(4.4) and try to use a action bar with the theme: holo.light.darkActionBar.
I created some xml files with the items in menu and linked it in the java class to the activity:
menu/main:
<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=".MainActivity">
<item android:id="#+id/action_filter"
android:title="#string/action_filter"
android:orderInCategory="100"
android:icon="#drawable/ic_action_filter"
app:showAsAction="ifRoom" />
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
The Problem is the following:
When I click on the menu/main.xml I can see that a item is added to the actionbar. But it never appears in the activity_main.xml.
I searched for many hours and considered the following:
-set minSDK to 19
-i know that some time it does not show up because the device has a menu button. But I use a icon and say it should be displayed. Therefore it should show up. And I also use a Nexus Emulation which has not menu button.
-I use Android studio and inserted the activities with the assistentce of android studio.
-The mainActivity extends FragmentActivity(because I chose to have a google maps activity)
-I tried other themes but then the actionbar disappears totally(oc did not use .noActionBar!)
-I set a style for the actionbar and there I specify a red color. This color is propagated to the activity but not the items.
my imports are:
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
How it should look:
How it looks rightnow:
I really really hope some of you can help me, it drives me nuts.
I think, your code to inflate the menu should look like this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
And remove menu.clear() line.
try to use a action bar with the theme: holo.light.darkActionBar
That means that you are trying to use the native action bar.
I created some xml files with the items in menu and linked it in the java class to the activity
Get rid of the app namespace, and rename app:showAsAction to be android:showAsAction. The app convention is used for the appcompat-v7 action bar backport, which you are not using.
MainActivity:
Get rid of menu.clear().
But it never appears in the activity_main.xml.
It is not supposed to. It should show up when you run the app, not necessarily in editors for other resources.
And I also use a Nexus Emulation which has not menu button
There certainly were Nexus devices with MENU buttons. Try pressing PgUp; if you get the overflow menu rising from the bottom of the screen, your emulator has a MENU button.
To create a navigation bar, I have dynamically create an action bar:
public void onCreate(Bundle savedInstanceState) {
ActionBar actionBar = getActionBar();
}
And add tabs one by one:
// Create first Tab
tab = actionBar.newTab().setTabListener(new FragmentsTab1());
// Create your own custom icon
tab.setIcon(R.drawable.tab1_hdpi);
actionBar.addTab(tab);
The problem is , the action bar is at the top by default. How can I placed it at the bottom? I found layout_below may be suitable for this case . However , as it generates dynamically, how can I know its ID? Thanks a lot.
Edited:
After adding split option, it doesn't make any changes, is it due to my action bar is dynamically generated?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:uiOptions="splitActionBarWhenNarrow"
package="com.example.tabtest"
android:versionCode="1"
android:versionName="1.0" >
And
<activity
android:name="com.example.tabtest.MainActivity"
android:uiOptions="splitActionBarWhenNarrow"
android:label="#string/app_name" >
Add this to your activity tag in the Minifest
uiOptions="splitActionBarWhenNarrow"
You haven't really answered the OP's question.. I'm searching for an answer as well since it looks like the custom bar always gets placed top and default menu bar on bottom if I use this code to bring the custom view:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
// displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.custom,
null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
:(
With Android 5.0 API, uiOptions="splitActionBarWhenNarrow" is deprecated. Unfortunately, this is not very well documented as yet. For details, you can read up this article.
http://commonsware.com/blog/2014/11/18/android-5p0-deprecation-splitactionbarwhennarrow.html
I've got an app that I've written targeting Android 2.1 which makes use of the menu button to expose some options. I'd like to have this menu available through the action bar overflow button in ICS, but I'm having trouble with getting it to show up.
If I change my target API to 15, the legacy menu button in the bottom bar disappears in ICS, but the icon in the top right of the action bar doesn't replace it, so there is no way to access the menu. I've tried adding the showAsAction attribute to the menu items, which did nothing. I'm definitely targeting 4.0.3 in my Eclipse build options.
All I want is for that menu to be accessible somewhere whilst using the ICS Holo theme, but still backwards compatible to older devices. How do I go about that?
Usually you add those Action Items simply by implementing the onCreateOptionsMenu(Menu menu) and adding the android:showAsAction="ifRoom" for the desired items in the menu's XML file.
e.g.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:icon="#drawable/ic_menu_save"
android:title="#string/menu_save"
android:showAsAction="ifRoom|withText" />
</menu>
Did you consider this fact?
Edit:
Here is a simple implemantation which worked for me a while ago:
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
createMenu(menu);
return true;
}
private void createMenu(Menu menu){
MenuItem mnu1 = menu.add(0, 0, 0, "Logout");
{
mnu1.setAlphabeticShortcut('a');
mnu1.setIcon(R.drawable.icon);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
I was having this issue 5 minutes ago. Dont know if this can be still helpful, but this is how I got it solved:
Add super.setBooleanProperty("showTitle", true); to your onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
//more code...
This will force the action bar to show and the menu button replacement will be there.
I am targeting android 4 in eclipse. Older devices show the bar as title only and of course are listening to default menu button action.