Zoker Zoker - 6 months ago 38
Android Question

Android get gravity value of textView programmically

I'm trying to get the current gravity value of a textView in Android

I searched a lot on stackoverflow, but only found solutions how to set gravity or how to get things like margin, padding etc.

So how can I get the current gravity of a textView?

Edit: It's simply

getGravity()

Answer

This is my TextView :

 <TextView
    android:id="@+id/textView"
    android:gravity="center"
    android:text="Hello World!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

This is the method how i am getting the gravity.

textView = (TextView)findViewById(R.id.textView);

switch (textView.getGravity())
{
        case Gravity.CENTER:

            Toast.makeText(MainActivity.this, "CENTER"
                    , Toast.LENGTH_SHORT).show();

            break;

        case Gravity.TOP:

            Toast.makeText(MainActivity.this, "TOP"
                    , Toast.LENGTH_SHORT).show();

            break;

        case Gravity.BOTTOM:

            Toast.makeText(MainActivity.this, "BOTTOM"
                    , Toast.LENGTH_SHORT).show();

            break;

        case Gravity.CENTER_HORIZONTAL:

            Toast.makeText(MainActivity.this, "CENTER_HORIZONTAL"
                    , Toast.LENGTH_SHORT).show();

            break;

        case Gravity.CENTER_VERTICAL:

            Toast.makeText(MainActivity.this, "CENTER_VERTICAL"
                    , Toast.LENGTH_SHORT).show();

            break;
    }

I am getting Toast displaying CENTER.

Comments