I'm trying to generate a bitmap
to show in the notification. The layout is a bit complex, however, I need to set the width according to the screen size. In the XML, I have a LinearLayout
in the root and I inflate the view with
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
This layout has quite a bunch of elements which I need to fill. And their width depends on the width of the layout itself and the width of the layout varies with phone. I set the width for the inflated layout with
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
However, when I try to generate a bitmap
from this, it appears that the size set with the setLayoutParams
is not affecting the child views of the inflated layout. There are many questions around i in SO, however, all of them deals with inflating a layout that already has a rootView
and nothing worked for me. A couple of answers suggested that I call the measure()
method on the view. However, after calling the measure method, the getMeasuredWidth
returns a width lesser that what I had set with the setLayoutParams
call.
Update
A little more context about the problem. I was trying to show a notification that had a placeholder ImageView which I would later fill in with a bitmap generated from a view inflated from an XML. The remote view layout is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliderViewBottom"
android:layout_width="match_parent"
android:layout_height="256dp"
android:background="@color/detail_page_blue"
android:clickable="true"
android:focusable="false"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
>
<ImageView
android:id="@+id/progressImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D05757"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
>
<TextView
android:id="@+id/overAllDelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delayed by "
android:textColor="@color/white"
/>
<TextView
android:id="@+id/overAllDelayVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/overAllDelay"
android:text="-"
android:textColor="@color/white"
/>
</RelativeLayout>
</LinearLayout>
The layout that I was trying to inflate and generate a bitmap from to set in the progressImage
was
<RelativeLayout
android:id="@+id/swipe_page_track_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="@dimen/swipe_layout_info_card_track_margin"
android:paddingRight="@dimen/swipe_layout_info_card_track_margin"
>
<ImageView
android:id="@+id/fromGreenCircle"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="1dp"
android:src="@drawable/from_green_circle"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp"
android:src="@drawable/station_end"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4.5dp"
android:background="@drawable/swipe_page_empty_track"
/>
<ImageView
android:id="@+id/fromGreenTrack"
android:layout_width="105dp"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="4.5dp"
android:src="@drawable/from_green_track"
/>
<ImageView
android:id="@+id/trackNavigationIcon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginLeft="100dp"
android:src="@drawable/ic_track_detail_card_current_station_pointer"
/>
<TextView
android:id="@+id/currentStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/trackNavigationIcon"
android:layout_marginTop="10dp"
android:textColor="@color/black"
/>
</RelativeLayout>
I was trying to inflate this second layout with
LinearLayout v = (LinearLayout) i inflater.inflate(R.layout.notification_expanded_detail, null, false);
And then was using
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) available_width, LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
This available_width was computed dynamically. However, when I later tried to generate a Bitmap from this inflated view by using v.layout()
, it kep t generating an image which was not honouring the width that I set.
Later, I came across this SO thread where I noticed that while inflating, instead of specifying null as the parentView, new LinearLayoutParams(contex)
was specified. I tried that and now, the dynamically set width is being honoured.