0

I customized tab layout for tab activity in my Android app. The problem is how to center it vertically. The text is centered horizontally automatically. I tried writing gravity:center, layout_gravity:center. Even more: I tried writing exact height in my TextView, but nothing works (actually exact pixels work great, but when I change device orientation - text is not centrally oriented.

That's my code for tab 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">

    <TextView
        android:id="@+id/tab_name"
        android:paddingTop="11dp"
        android:paddingBottom="11dp"
        android:layout_width="match_parent"
        android:textSize="14dp"
        android:textStyle="bold"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:layout_gravity="center" />

</LinearLayout>

Here's screenshot of my problem: Screenshot Screenshot-2

Stepan Mazokha
  • 459
  • 1
  • 5
  • 22
  • 1
    I m not exactly sure what is going wrong. Could you post some images that could help. Also what would be the height and width of the textview in dp? – ngoa Apr 28 '15 at 16:10
  • What do you want to center vertically the text or the widget? – Josef Apr 28 '15 at 16:21
  • @ngoa I updated the post with link on picture with my problem – Stepan Mazokha Apr 30 '15 at 14:15
  • @StepanMazokha Did u try using actionBar and see it that works? just a suggestion! – ngoa Apr 30 '15 at 15:31
  • @ngoa what do you mean by saying "using actionBar"? By the way, I am adding the tabs programmatically in this way: mCustomView = (LinearLayout) getLayoutInflater().inflate(R.layout.custom_tab, null); textView = (TextView)mCustomView.findViewById(R.id.tab_name); textView.setText(mSectionsPagerAdapter.getPageTitle(i)); actionBar.addTab(actionBar.newTab().setCustomView(mCustomView).setTabListener(mActivity)); – Stepan Mazokha Apr 30 '15 at 16:15
  • @StepanMazokha I'm not sure what exactly is going wrong since I need my workstation to debug this. But you try activating "Show Layout Boundaries" from the developer options to see how the layout is affected. – ngoa Apr 30 '15 at 17:10
  • @ngoa layout is not affected at all. there is no difference before and after applying gravity attributes. I could add paddings programmatically, but I need to have tab height - can't find the way how to get it. (using action bar height is not appropriate because tab height and actionbar height are different on multiple screens) – Stepan Mazokha Apr 30 '15 at 21:56
  • @StepanMazokha Very weird... I might get to work on this immediately but when I get time, I will test this manually and see what might be going wrong. – ngoa Apr 30 '15 at 22:16
  • @ngoa ok, thank you. will be waiting for your response – Stepan Mazokha Apr 30 '15 at 22:28
  • @ngoa Actually the thing that helped me is posted here: http://stackoverflow.com/a/22144844/3649567 – Stepan Mazokha May 01 '15 at 21:02
  • @StepanMazokha Sorry I never got back to you. Totally forgot to open my workstation over the weekend. – ngoa May 06 '15 at 15:06

1 Answers1

1

You just need to add android:gravity="center" to the LinearLayout and
use wrap_content for layout_width and layout_height of the TextView.

<?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">

    <TextView
        android:id="@+id/tab_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="11dp"
        android:paddingBottom="11dp"
        android:textSize="14dp"
        android:textStyle="bold"
        android:maxLines="1" />

</LinearLayout>
ByteHamster
  • 4,884
  • 9
  • 38
  • 53