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 ?