I have a layout resource like this and a I want to inflate it with the layout width and height:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75px"
android:layout_height="25px">
<TextView
android:id="@+id/drawable_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Name" />
<TextView
android:id="@+id/drawable_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/drawable_name"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_below="@+id/drawable_name"
android:text="Some Text" />
</RelativeLayout>
The View could be anything, and I'm converting it to Bitmap.
private Bitmap getBitmap(View v){
v.measure(MeasureSpec.makeMeasureSpec(mDrawableWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mDrawableHeight, MeasureSpec.EXACTLY));
v.layout(0, 0, mDrawableWidth, mDrawableHeight);
Bitmap returnedBitmap = Bitmap.createBitmap(mDrawableWidth, mDrawableHeight,Bitmap.Config.ARGB_8888);
Canvas c=new Canvas(returnedBitmap);
v.draw(c);
return returnedBitmap;
}
Until now, I have the Width and Height hardcoded and I want to be able to do it programatically, accessing the layout_width
and layout_height
.
Is there any way to achieve this?
If there's another way of inflating the view with this values without specifying them in the measure, please let me know.
If I create a Custom View, is there any chance of specifying the fixed width and height?
LayoutParameter
from the View is null at that point. and the view will not be attached to a parent. I just want to create a Bitmap from thatLayout
. – Diverticulosis