Skip to content Skip to sidebar Skip to footer

Android, Make Text Switcher Central?

How can I centralise my text switcher? I've tried set gravity but it doesnt seem to work. ts.setFactory(new ViewFactory() { public View makeView() { TextView t = ne

Solution 1:

That code is OK, you need to set the parent textSwitcher width to fill_parent

either in XML with

<TextSwitcher android:layout_width="fill_parent" ...

or in code

import android.widget.LinearLayout.LayoutParams;
//...
textSwitcher=...
textSwitcher.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Solution 2:

If you are using match_parent or a fixed value for the TextSwitcher size, do the following. In the .xml:

<TextSwitcher
    android:id="@+id/switcher"
    android:layout_width="60dp"
    android:layout_height="60dp"/>   

In code:

    import android.widget.FrameLayout.LayoutParams;

    TextSwitcher mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(new ViewFactory() {
        @Override
        public View makeView() {
            TextView t = new TextView(MainActivity.this);
            t.setGravity(Gravity.CENTER);
            t.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            return t;
        }
    });

It is quite important to set LayoutParams to MATCH_PARENT to get the TextSwitcher centered vertically.


Post a Comment for "Android, Make Text Switcher Central?"