I am using the ActionBar.Tab, and they always appear at the left of screen. I want to put the activity icon to the left of tabs, but it always appear at the right of tabs. How can I move their position to where I want?
here is what works for me like a dream:
in the Activity I have this:
//hiding default app icon
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
my_action_bar.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/turquoise">
<ImageButton
android:id="#+id/btn_slide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#null"
android:scaleType="centerInside"
android:src="#drawable/btn_slide"
android:paddingRight="50dp"
android:onClick="toggleMenu"
android:paddingTop="4dp"/>
</RelativeLayout>
//use your custom xml view to show your icon at left
View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
on your onCreate() method if you are on the activity
actionBar.addTab(actionBar.newTab()
.setIcon(<the icon you want>)
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
Related
I want to insert a centered icon in Actionbar, but i have no idea what code that i need. anyone can help me?
This is my code :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
getSupportActionBar().setTitle("");
Thank you
step 1: create a custom layout in R.layout. directory, as you desire and then set the view
to action bar
step 2: Set the custom layout.
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View customView = getLayoutInflater().inflate(R.layout.customActionBar, null);
actionBar.setCustomView(customView );
From your xml add ToolBar as below and set Icon to ImageView inside it.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"/>
</android.support.v7.widget.Toolbar>
Set toolbar as support action bar as you are doing.
I'm trying to style the action bar by adding a custom back button on the left and an icon in the center. The rendering in Android Studio is exactly as I want it, but when I test the layout on my phone, it just shows both back button and icon on the left, with the icon on top of the back button. This is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton android:id="#+id/back_button"
android:contentDescription="Image"
android:src="#drawable/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#color/action_bar_bg_color"
android:layout_alignParentLeft="true"/>
<ImageView android:id="#+id/cow_icon"
android:contentDescription="Image"
android:src="#drawable/cowhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
And this is what I do to style the action bar:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
View mActionBar = getLayoutInflater().inflate(R.layout.action_bar_layout, null);
actionBar.setCustomView(mActionBar);
actionBar.setDisplayShowCustomEnabled(true);
Anyone see what could be causing the flawed styling of the action bar?
You could try using the new Toolbar : https://developer.android.com/reference/android/widget/Toolbar.html
which is great for making custom actionBar.
Otherwise, you don't have to make your back button custom, actionbar supports it with : https://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled(boolean)
I'm trying to make an ActionBar that has:
Home button as up enabled with title (to the left)
TextView (centered)
Overflow Menu items (to the right)
I was following this answer to an almost identical question. The difference is that I would like to show a title. I tried actionBar.setTitle("Title"), but it gets cut off by the LinearLayout and isn't visible.
How can I create a custom View in the center of my ActionBar with the above elements?
Is it possible to do without overriding the entire ActionBar?
Edit:
I set up the ActionBar here:
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setTitle("Title");
actionBar.setCustomView(R.layout.some_layout);
Here is the some_layout.xml file:
<?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:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text" />
</LinearLayout>
Here is the resulting ActionBar I get from this (I set the LinearLayout background to green and text color to white):
Here is what I'm trying to get (without the green background):
Try this:
ActionBar ab = getSupportActionBar();
ab.setCustomView(R.layout.registration_actionbar);
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
I'm trying to change the layout of the actionBar in my app. I managed to display the layout, but there's a problem - the layout doesn't stretch to entire width of the bar and I don't see the title and the icon. Here's how I see it right now:
This is the XML of my custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/app_bg"
android:layout_gravity="fill_horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
And this is how I tried to display it in my main FragmentActivity, along with the title:
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setSubtitle("welcome to my app");
actionBar.setTitle("My App");
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I want the layout to stretch to entire width of the bar. Also, I'd like to have the options button on the right of the menu later (to the left of the button).
What am I doing wrong and how do I fix it?
I am new to android.I am trying to implement one actionbar which has three tabs,each tab contains one icon and name of tab.I am succeeded to place icon and text on each tab,but unfortunately icon is coming on left side of text(name of the tab) in tab. I want place icon on the top of the text instead of left side. Please find snippet of my code, and please help me to find solution. Thanks in advance,
private void setActionBar()
{
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tabA = bar.newTab().setText("TabA");
tabA.setIcon(R.drawable.iconA);
ActionBar.Tab tabB = bar.newTab().setText("TabB");
tabB.setIcon(R.drawable.iconB);
ActionBar.Tab tabC = bar.newTab().setText("TabC");
tabC.setIcon(R.drawable.iconC);
}
You can use custom view to define how you want to have your tabs displayed.
define custom layout where you have an image above your text
in your activity, inflate the view and set the values for your image and your text
set custom view for the tab
Here is a rough example:
CUSTOM 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="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/tabIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="2dp" />
<TextView
android:id="#+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
INFLATE VIEW
View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null);
TextView tabText = (TextView) tabView.findViewById(R.id.tabText);
tabText.setText(R.String.sometext);
ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage));
SET CUSTOM VIEW FOR THE GIVEN TAB
Tab tab = actionBar.newTab().setCustomView(tabView)