Search code examples
androidbuttonprogrammatically-created

Why does it look like my androidbutton is compressed in width?


I want to add multiple Buttons in my activity programmatically. That works fine so far, but it looks like my button is compressed in width. The button has a background image that is 20*20px and im using WRAP_CONTENT for width and height. Here is the code:

for(int i = 0; i < 5; i++){

        LinearLayout layout;
        Button buttonDel;

        layout = (LinearLayout)findViewById(R.id.linLayout);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);

        buttonDel = new Button(this);
        buttonDel.setId(i);
        buttonDel.setBackgroundResource(R.drawable.buttondelete);
        buttonDel.setLayoutParams(params);

        layout.addView(buttonDel);

    }

And the xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.developer.cardz.ListOfAllWordsActivity">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linLayout">

    </LinearLayout>
</ScrollView>

Do you have any suggestions?


Solution

  • Does it has any text inside them? If your button had any text on them, then those would be considered as "content" so it may compress image regarding to text.