简体   繁体   中英

Dynamically setting width and height for a button in android

I have a button inside a tablerow , whose text changes between 'Buy Gift' and 'Out of Stock' depending on two situations in the database . So i want to set width and height as wrap_content dynamically in the code.

At class level , i declared the following two lines:

LinearLayout.LayoutParams lp2;
lp2=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

I used a method to dynamically set the width and height:

public void giftdetailssetting(String gift_name)
    {
        //btnbuygift.setWidth(200);
        btnbuygift.setText("Buy Gift");
        //btnbuygift.setEnabled(true);
        btnbuygift.setClickable(true);
        btnbuygift.setLayoutParams(lp2);

        // btnbuygift.setHeight(200);
        gift_src=dm.retrievegiftsrc(gift_name);
        Resources res = getResources();
        int resId=res.getIdentifier(gift_src,"drawable","com.example.shopkart");
        iv_gift.setImageResource(resId);

        List<String> giftvalues=dm.getgiftdetails(gift_name);
        giftprice=(String)giftvalues.get(1);
        txtgiftname.setText((String)giftvalues.get(0));
        txtgiftprice.setText("Rs "+giftprice);
        txtgiftquantity.setText((String)giftvalues.get(2));
        if(Integer.parseInt((String)giftvalues.get(2))==0)
        {
            btnbuygift.setText("OUT OF STOCK");
            btnbuygift.setLayoutParams(lp2);
            //btnbuygift.setEnabled(false);
            btnbuygift.setClickable(false);
        }

    }

But i get the following error in the logcat:

04-24 14:57:00.240: E/AndroidRuntime(26781): FATAL EXCEPTION: main
04-24 14:57:00.240: E/AndroidRuntime(26781): Process: com.example.shopkart, PID: 26781
04-24 14:57:00.240: E/AndroidRuntime(26781): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableRow.getColumnsWidths(TableRow.java:299)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.findLargestCells(TableLayout.java:508)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.measureVertical(TableLayout.java:473)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.TableLayout.onMeasure(TableLayout.java:439)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.View.measure(View.java:16497)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer.doFrame(Choreographer.java:544)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Handler.handleCallback(Handler.java:733)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.os.Looper.loop(Looper.java:136)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at java.lang.reflect.Method.invoke(Method.java:515)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 14:57:00.240: E/AndroidRuntime(26781):    at dalvik.system.NativeStart.main(Native Method)

How do i dynamically set width and height for the button as wrap content in both the cases??PLS HELP!!

You are getting ClassCastException because you are assigning LinearLayout.LayoutParam to a Button which is of TableRow type.

Change

LinearLayout.LayoutParams lp2;
lp2=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

to

TableRow.LayoutParams lp2;
lp2=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);

Don't forget to import the right packages for LayoutParams.WRAP_CONTENT else you will again get the same error.

I hope when you try using LayoutParams your issues will be sloved;. Using this we can change size of button dynamic in android!

For example;

button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)

Hope it will help you !

You can set the width and height using following code:

Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tblRow.addView(btnTag);

Looking at your logcat it is evident that you are setting the LayoutParams to Linear layout instead of Tablerow in your case.

But according to the docs, the children of Table row take the MATCH_PARENT for their width. So it'll be better if you nest a horizontal linearlayout with a button inside it which can then be resized with your code itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM