android - Inflating ImageButton programatically changes width & height params to wrap_content. Why? -


in app i'm using android.support.v7.widget.toolbar.

left half of toolbar acts button.

right half different different activities. it's text, button. inflate right half views programmatically.

toolbar

<android.support.v7.widget.toolbar> ... ... <linearlayout         android:id="@+id/left_ll"/> ... <linearlayout         android:id="@+id/right_ll"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentright="true"         android:layout_centervertical="true"         android:orientation="horizontal" />  </android.support.v7.widget.toolbar> 

i'm inflating view in right_ll:

child_button.xml

<?xml version="1.0" encoding="utf-8"?> <imagebutton xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/right_ib" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/save" android:background="@null" android:onclick="onrightheaderbutton" android:scaletype="fitxy" /> 

i'm adding programatically this:

right_ll.addview(inflater.inflate(r.layout.child_button,                     null, false)); 

so the problem is button occupies half screen. it's original dimensions 512x512.

i thought maybe layoutparams somehow getting reset, tried

final float scale = getresources().getdisplaymetrics().density; int pixels = (int) (30 * scale + 0.5f);  right_ib = (imagebutton) right_ll.findviewbyid(r.id.right_ib); right_ib.setlayoutparams(new viewgroup.layoutparams(                     pixels,pixels)); 

it gave exception:

java.lang.classcastexception: android.view.viewgroup$layoutparams cannot cast android.widget.linearlayout$layoutparams

any appreciated.

when call inflater.inflate(r.layout.child_button, null, false), passing in null parent view - causes layout_ attributes ignored (as attributes used parent view) - see this google+ post more details.

instead, should use

inflater.inflate(r.layout.child_button, right_ll, true)); 

this causes child_button layout inflated parent layout, right_ll, , automatically added (that's true does).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -