0

I have a vertical LinearLayout with 2 TextSwitcher inside. Sometimes only the first one(@+id/ts1) will show, sometimes both of them will show on screen. The font size for ts1 is 20, for ts2 is 16.

<LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:focusable="false"
            android:layout_marginLeft="@dimen/dimen_left1"
            android:visibility="gone">

            <TextSwitcher
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:id="@+id/ts1"/>

            <TextSwitcher
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:id="@+id/ts2"/>
        </LinearLayout>

When I tested it, when both of them showed on screen, it worked fine, but when there's only ts1 shown, the text is not centered vertically, it's more like on the top vertically instead of centered. I programmatically set the visibility of these 2 TextSwitchers.

Does anyone know why this happens?

Thanks!!!

Zip
  • 809
  • 2
  • 14
  • 30
  • 1
    Just a guess from looking at your layout... the TextSwitcher height is matching the parent, so how should it be centered? – Lupinity Labs Nov 29 '16 at 23:41
  • What is your goal here? Do you just want 1 piece of text that can switch back and forth? If that is the case, you only need 1 TextSwitcher, not 2. Each TextSwitcher contains 2 TextViews and methods to switch between them. – James McCracken Nov 29 '16 at 23:58
  • @JamesMcCracken I need 2 TextSwitcher because each of them needs to switch some texts in different situations. My goal here is to do some text switching, in some cases, I only need one. I can't just do some string manipulation and combine them into one since each TextSwitcher is only different text font, size, etc. – Zip Nov 30 '16 at 00:03

3 Answers3

4

Setting the layout_gravity on TextSwitchers does not change the text android:gravity of the underlying TextViews. You can override the default TextViews that TextSwitchers use by putting them in your XML. From there, you can set the android:layout_gravity or android:gravity on them to center the text vertically. This should work:

<TextSwitcher
    android:id="@+id/ts1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
</TextSwitcher>
<TextSwitcher
    android:id="@+id/ts2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
</TextSwitcher>
James McCracken
  • 15,488
  • 5
  • 54
  • 62
  • Since I use setFactory to create textview for TextSwitcher, I add setGravity to the newly created TextView too, but still it's not showing in center. Do I need to define the TextView in xml file like above ? – Zip Nov 30 '16 at 00:25
  • The XML, to me, makes it easier to wrap my head around things but setFactory should work just fine. My next suggestion would be to debug the UI with layout boundaries on (via developer settings on the device) and see what's up. You can also use the hierarchy viewer in Android Studio to debug. – James McCracken Nov 30 '16 at 00:27
2

You need to add gravity option to TextView, not the TextSwitcher. For example, you can add layout_width, layout_height, and gravity option dynamically.

    mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);
    mSwitcher1.setFactory(new ViewSwitcher.ViewFactory() {

        public View makeView() {
            TextView myText = new TextView(MainActivity.this);
            myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            myText.setTextSize(36);
            myText.setTextColor(Color.BLUE);

            // Add this
            myText.setGravity(Gravity.CENTER);
            // Add this
            myText.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            return myText;
        }
    });
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
-1

Have you tried using the ConstraintLayout? Using it may help you out. If you give more information on what the behavior of everything needs to be, I can give you a code example.

Jon Merritt
  • 114
  • 2
  • 11