0

I have to add a background shape to a TextView but i don't know the dimension of this beacause I need to set width to match parent. I can't set dimension dp beacause it cause me more problem on resize ,on keyboard show and other. I would get effective width on create and set the same on height but when i wrote:

TextView tCircle = findViewById(R.id.tCircle);
tCircle.getLayoutParams().width

it give me -1. How can draw this circle ?? the button_shape is:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <corners android:radius="10dp"/>

    <stroke android:color="@color/colorPrimaryDark"
        android:width="2dp"/>
    <solid android:color="@color/colorAccent"/>

</shape>

layout fragment

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="6.5"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tCircle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:autoSizeTextType="uniform"
                    android:background="@drawable/button_shape"
                    android:text="TextView"
                    android:textAlignment="center" />
            </LinearLayout>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

EDIT: After Mohamed Mohaideen AH's suggestions i wrote in onCreate:

tCircle.post(new Runnable() {
      @Override
      public void run() {

          int width = tCircle.getWidth();
          int heigth = tCircle.getHeight();
          Log.d(TAG_LOG, "Dim cerchio: W" + String.valueOf(width)+" H:"+String.valueOf(heigth));

          if(width>=heigth){
              Log.d(TAG_LOG, "A");
              tCircle.setWidth(heigth);
              tCircle.setHeight(heigth);
          }
          else{
              Log.d(TAG_LOG, "B");
              tCircle.setWidth(width);
              tCircle.setHeight(width);
          }

      }
});

And the TextView is :

<TextView
android:id="@+id/tCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeTextType="uniform"
android:background="@drawable/button_shape"
android:text="TextView"
android:textAlignment="center" />

But the set don't change the dimension. Why ?

rgv
  • 1,186
  • 1
  • 17
  • 39
Pab
  • 3
  • 9

2 Answers2

0

You can dynamically set the height according to the width of the Screen. Use this method to get the width of the screen:

public static int getScreenWidth(Context mContext) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics.widthPixels;
}

Then you can use the layout params as you used to set the height same as the width.

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • Thanks for response but my text view is inside a layout that is inside another ecc.. So i need the dimension of the father. – Pab Jul 16 '18 at 13:07
0

Problem you are getting -1 is your view not intialized. When OnCreatemethod you can't get layoutparameter value until view loaded. So try below code to get width of view.

textview.post(new Runnable() {
            @Override
            public void run() {

                int width = textview.getWidth();
}
}

EDITED
To create a circle background for your textview.

enter image description here

tv.post(new Runnable() {
        @Override
        public void run() {
            int value  = tv.getWidth();              
            tv.setWidth(value);
            tv.setHeight(value);         
      }
});

Hope it helps.!

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24