Reputation: 41
I am making an application in which I have to change the UI programmaticaly.
I needed to change the layout_height
and layout_width
XML attributes.
But when I use the setLayoutParams
method, the app runs and then I give a message that it has unexpectedly stopped working and I have to force close.
I have tried setting the LayoutParams as ViewGroup.LayoutParams
as well.
But nothing works. Kindly check the attached code and guide.
This is the Java Code.
private void fixLayoutVmain(){
ListView lv;
ImageButton rev,stop,play,forw;
lv=(ListView)findViewById(R.id.ListMain);
rev=(ImageButton)findViewById(R.id.rev);
stop=(ImageButton)findViewById(R.id.stop);
play=(ImageButton)findViewById(R.id.plpa);
forw=(ImageButton)findViewById(R.id.forw);
Log.d("DEV1","ID's have been assigned");
LayoutParams lp=new LayoutParams(W, ((75*H)/100));
Log.d("DEV1","param object created");
lv.setLayoutParams(lp);
Log.d("DEV1","ListView param set");
lp.height=(int)(10*H)/100;
lp.width=(int)(10*H)/100;
Log.d("DEV1","Changes to param made");
rev.setLayoutParams(lp);
Log.d("DEV1","Reverse Button param applied");
stop.setLayoutParams(lp);
Log.d("DEV1","Stop button param applied");
play.setLayoutParams(lp);
Log.d("DEV1","forward button param applied");
forw.setLayoutParams(lp);
Log.d("DEV1","All imagebutton changes have been made");
}
This is the XML File
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListVieW
android:id="@+id/ListMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ImageButton
android:id="@+id/rev"
android:src="@drawable/reverseimg"
android:background="@null"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/stop"
android:src="@drawable/stopimg"
android:background="@null"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/plpa"
android:src="@drawable/playimg"
android:background="@null"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/forw"
android:src="@drawable/forwardimg"
android:background="@null"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
Upvotes: 4
Views: 13078
Reputation: 1
I checked again and made both the changes as told in the above answers. It still didn't work.
However i found out that i was executing the function before i used setContentView() method. Since the view hadn't been created, alterations could not be made.
So putting my function after the setContentView()
method made it work fine.
Thanks anyways.
Upvotes: 0
Reputation: 14746
It looks like your application crashes because you have not supplied android:layout_width and android:layout_height to your ImageButtons.
Even if you are going to set the width and height in code, you need to add these parameters to your xml-file for each ImageButton:
<ImageButton
android:id="@+id/rev"
android:src="@drawable/reverseimg"
android:background="@null"
android:scaleType="fitXY"
android:layout_height="0dp"
android:layout_width="0dp"
/>
Upvotes: 2
Reputation: 134664
Any time you set LayoutParams, check the parent container of the View. In all your instances, the parent is a LinearLayout. You need to use LinearLayout.LayoutParams, like so:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
lv.setLayoutParams(lp);
Whether that's your particular force close issue or not, we have no way of knowing. As stated in the comments, you should check LogCat and post the exception details here. Give this a try though, and see if it fixes the problem.
Upvotes: 11