Android ActionBar custom layout styling

Msmit1993

I'm just starting android development coming from IOS and again stubbled onto a problem. And after 1 day of trying i decided that i will ask the people of stack overflow.

So my problem:

I have an app and in the action bar (default no sherlock) i want 1 logo and 2 lines of text. And trough the internet i fount the setCustomView for the action bar. And the custom xml is loaded but i can't get the layout right.

So first i setup the action bar:

private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button.

    actionBar.setCustomView(customNav, lp1);
}

Than the xml:

<?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:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:background="@color/Blue">

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_menu"
        android:textColor="@color/White"
        android:paddingLeft="5dp"
        android:layout_gravity="left"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/Icon"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_help"
        android:layout_gravity="right"
        android:textColor="@color/White"
        android:paddingRight="5dp"/>

</LinearLayout>

But the result is something what i'm not looking for:

Problem in an image

So what am i forgetting/doing wrong/or should i do?

Fahriyal Afif

try change the root layout into relative layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">

<TextView
    android:text="left text"
    android:layout_toLeftOf="@+id/icon"
    android:layout_alignParentLeft="true"
    android:id="@+id/text_left"
    android:gravity="left"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"/>

<ImageView
    android:layout_centerHorizontal="true"
    android:id="@+id/icon"
    android:layout_width="10dp"
    android:layout_height="wrap_content"
    android:src="@drawable/bg_sunrise"
    android:layout_gravity="center"/>

<TextView
    android:text="Right txt"
    android:layout_toRightOf="@+id/icon"
    android:id="@+id/text_right"
    android:layout_width="match_parent"
    android:gravity="right"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:paddingRight="5dp"/> </RelativeLayout>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Android: remove left margin from actionbar's custom layout

From Dev

Custom actionbar layout with overflow menu

From Dev

Styling android search view and drop down list in actionbar

From Dev

android layout causes crash on actionbar addtab

From Dev

AppCompat Actionbar styling

From Dev

ActionBar with custom layout with custom menu item

From Dev

Android ActionBar custom layout styling

From Dev

Android Custom Dialog Styling

From Dev

Styling dropdown menu that Android creates from tabs in the actionbar

From Dev

Styling ActionBar Tabs

From Dev

custom actionbar layout using appcompat

From Dev

ActionBar with custom layout does not occupy full screen width on Android 4.4.2 (KitKat)

From Dev

Custom font in Android app and ActionBar

From Dev

Custom big logo in actionbar android

From Dev

Add custom layout to ActionBar/Toolbar with no margins

From Dev

issue Android layout on top of actionBar

From Dev

Styling the actionbar in android

From Dev

Android ActionBar custom view and NavigationDrawer

From Dev

Android ActionBar with custom layout does not occupy whole width of parent

From Dev

AppCompat Actionbar styling

From Dev

Android - styling custom Dialog

From Dev

Android Custom Dialog Styling

From Dev

custom actionbar layout using appcompat

From Dev

Custom font in Android app and ActionBar

From Dev

Styling actionBar affects styling of Preferences

From Dev

Styling Actionbar Appcompat devider

From Dev

Android Custom ActionBar with Search View

From Dev

Android ActionBar on Lollipop not styling correctly

From Dev

Custom Actionbar in fragment android