I have this layout for a cell from a GridView calendar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_cell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cell_bg"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp">
<LinearLayout
android:id="@+id/shape_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
I'm setting programatically a drawable background to Linear layout identified as android:id="@+id/shape_background
. I inserted this layout just because I don't want distortions related to size of parent LinearLayout. As you noticed I have 2 textviews inside it. If the layout width and height is wrap_content my background shrinks to the size of content inside textviews. And when I set to match_parent occurs distortions on shaped background when I rotate.
Below is the simple code to create drawable and its initialization.
public CalendarAdapter( Context context, int month, int year,
Map<String, Object> caldroidData,
Map<String, Object> extraData, int cellHeight) {
super(context, month, year, caldroidData, extraData);
this.cellHeight = (int)Math.floor( ( cellHeight - ( cellHeight * 0.1 ) ) /6 );
backgroundDrawable = ContextCompat.getDrawable( context, R.drawable.ball );
}
LinearLayout shapeBackground = ( LinearLayout )cellView.findViewById( R.id.shape_background );
backgroundDrawable.setBounds(0, 0, 90, 90);
backgroundDrawable = new ScaleDrawable( backgroundDrawable, 0, 90, 90).getDrawable();
shapeBackground.setBackground( backgroundDrawable );
And this is the xml of drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FF0000"/>
<size
android:width="1dp"
android:height="1dp"/>
</shape>
Pretty simple. I read some SO posts and tried the solutions. No success.