Foobar
Foobar

Reputation: 8467

Android button.setWidth() not doing anything

I have a inputMethodService that controls a soft keyboard. Here is the relevant part of the

@Override
public View onCreateInputView() {
    LayoutInflater lInflate = getLayoutInflater();
    Resources res = getResources();


    LinearLayout inputView = (LinearLayout) lInflate.inflate(R.layout.copypasta, null);

    Button tab_navySeal = (Button) inputView.findViewById(R.id.tab_navySeal);
    Button tab_thatsome = (Button) inputView.findViewById(R.id.tab_thatsome);
    Button tab_thethingis = (Button) inputView.findViewById(R.id.tab_thethinggis);
    Button tab_identify = (Button) inputView.findViewById(R.id.tab_identify);
    Button tab_othercopypasta = (Button) inputView.findViewById(R.id.tab_otherpasta); 


    int sW = res.getDisplayMetrics().widthPixels;

    int bWidth = sW/5;
    Log.i("DEBUG_TAG", Integer.toString(bWidth));
    if(bWidth > 100) {
        tab_navySeal.setWidth(bWidth);
        tab_identify.setWidth(bWidth);
        tab_thatsome.setWidth(bWidth);
        tab_thethingis.setWidth(bWidth);
        tab_othercopypasta.setWidth(bWidth);

    }

    return inputView;
}

The purpose of this code is so that if the screen is large enough the buttons will each be 1/5 of the screen's width. Otherwise if the screen is small, the buttons will just be 100dp (which is fine since they are wrapped in a HorizontalScrollView).

However nothing is actually happening when I open the softkeyboard.

Here is an image of the problem:

enter image description here

The buttons at the top should each be 1/5 of the screen instead of being 100dp wide. Incase this matters, the width of each button is set to 100dp in XML (even though I don't think it matters, because programmatically setting the widths should override the XML).

Upvotes: 1

Views: 529

Answers (2)

Paul McCarthy
Paul McCarthy

Reputation: 886

Unfortunately the people who designed Android decided that programmers are too stupid to know where they want to put things in their application to stupid to know what size they want things to be. Therefore methods like setWidth DON'T set the width. You have to read through pages and pages of crap about Layouts to get even remotely close to something that works. Sorry, I don't know enough about layouts to help but I am glad you have highlighted how badly Android is designed in this area.

Upvotes: 1

Shadab Ansari
Shadab Ansari

Reputation: 7070

You can try setting LayoutParams with the new width like this -

tab_navySeal.setLayoutParams (new LayoutParams(bWidth, LayoutParams.WRAP_CONTENT);

Upvotes: 1

Related Questions