0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arun Kumar Nagarajan
  • 2,347
  • 3
  • 17
  • 28
  • Is this for a status bar notification? – Karakuri Dec 16 '16 at 18:44
  • It's for setting the bigContentView in notifications. Thanks – Arun Kumar Nagarajan Dec 16 '16 at 18:50
  • Then I don't understand your question. Status bar notification UI is configured using `RemoteViews`, not through normal view inflation and modification. Perhaps you should clarify what you are asking and include additional code snippets. – Karakuri Dec 16 '16 at 18:54
  • Yes, I am using RemoteViews. In that remote view, i have an imageView which i generate from generating a bitmap by inflating another layout and filling content in it. Later I set that bitmap into that image view. I just saw another question http://stackoverflow.com/questions/18565226/programmaticaly-inflate-view-with-pre-defined-measures?rq=1 and noticed that while inflating, we give new LinearLayout(context) as the rootView. I tried the same and it's not working! Thanks much for your help :) – Arun Kumar Nagarajan Dec 16 '16 at 18:57
  • Please include more relevant code snippets. There is no way to tell what (or if) you are doing anything different than the link you just posted. At the very least, it would be helpful to [edit](http://stackoverflow.com/posts/41190509/edit) your post and include that link if that is exactly the code you are using. – Karakuri Dec 16 '16 at 19:02
  • I've included more code snippets and have tried to elaborate on the question. Hope it would help someone some day. Thanks much for the help again! – Arun Kumar Nagarajan Dec 16 '16 at 19:20
  • Based on your edit, it sounds like your issue is resolved? – Karakuri Dec 16 '16 at 19:23
  • Yes, I saw the other SO thread just minutes after posting this question. I got that question in the related section of this question! Funny how I never encountered that question while Googling :| – Arun Kumar Nagarajan Dec 16 '16 at 19:31

1 Answers1

0

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

Arun Kumar Nagarajan
  • 2,347
  • 3
  • 17
  • 28