0

I have created a custom action bar

ActionBar actionBar = getActivity().getActionBar();
    actionBar.setCustomView(R.layout.action_bar_draft);
    actionBar.setDisplayShowCustomEnabled(true);
    actionbarFeed = (TextView) actionBar.getCustomView().findViewById(R.id.textView_blogtest);  
    actionbarFeed.setText("My Photos and videos");

The problem is Text View is not align center

enter image description here

In my layout the text view is at center. when it come to action bar it is shifted to right side.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

    <TextView
        android:id="@+id/textView_blogtest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />
</RelativeLayout>

 <style name="AppTheme.ActionBarStyle" parent="@style/Widget.Sherlock.ActionBar.Solid">
    <item name="android:icon">@drawable/menu</item>
    <item name="android:background">#ffca36</item>
    <item name="background">#f66605</item>
    <item name="android:customNavigationLayout">@layout/action_bar</item>
</style>
DKV
  • 1,767
  • 3
  • 28
  • 49

3 Answers3

0

look for an answer here: How to center align the ActionBar title in Android?

basically you should change your xml file for the action bar.

EDIT:

post your xml file for the action bar to get a more precise answer...

EDIT2:
i don't know it. But a hint for debugging: Set background color of your textview to another color and post a screenshot of it. From this you can see if the problem is that the content of textview is placed wrong, or the whole textview is not placed correctly.

Maybe you only need the line

android:gravity="center"

in your TextView?

Community
  • 1
  • 1
Stephan Huewe
  • 124
  • 1
  • 7
0

please use below function for set actionbar

public void setMainScreenActionBar(String p_title)
{

        LayoutInflater m_inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ActionBar m_actionBar = getSupportActionBar();
        View m_viewActionBar = m_inflator.inflate(R.layout.custom_actionbar_title_layout, null);
        ActionBar.LayoutParams m_params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        TextView m_tvTitle = (TextView) m_viewActionBar.findViewById(R.id.title);
        m_tvTitle.setText(p_title);
        m_actionBar.setCustomView(m_viewActionBar, m_params);
        m_actionBar.setDisplayShowCustomEnabled(true);
        m_actionBar.setDisplayShowTitleEnabled(false);
        m_actionBar.setDisplayHomeAsUpEnabled(true);
        m_actionBar.setHomeButtonEnabled(true);
}

custom_actionbar_title_layout.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="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>

Write below code in your style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- Customize your theme here. -->
    </style>


     <style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/orange</item>
        <item name="background">@color/orange</item>
         <item name="android:homeAsUpIndicator">@drawable/ic_backarrow</item>
        <item name="homeAsUpIndicator">@drawable/ic_backarrow</item>
    </style>

</resources>
0

In your onCreate() method:

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater mInflater = LayoutInflater.from(context);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setCustomView(mInflater.inflate(R.layout.custom_layout, null),
    new ActionBar.LayoutParams(
        ActionBar.LayoutParams.WRAP_CONTENT,
        ActionBar.LayoutParams.MATCH_PARENT,
        Gravity.CENTER
));

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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Your title"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="14dp" />

</LinearLayout>

Then you will have an Actionbar with just a title.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96