Search code examples
androiduser-interfacebuttonlayoutandroid-linearlayout

Button wrap_content not working in LinearLayout


So I've been trying to make a project that creates a new Button(with numerical text by order) whenever I press that LayoutInflate Button. (I wrote a question and a problem on the bottom of this post)

What I have made so far is these:

1.MainActivity java file

->java file that inflates button in buttonlayout.xml to Linearlayout in mission.xml file

package com.example.a13_1_mission1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

Button btn_inflation;
LinearLayout container;
static int num=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mission1);

    btn_inflation= (Button)findViewById(R.id.inflation);
    container = (LinearLayout)findViewById(R.id.container);

    btn_inflation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LayoutInflater inflater = getLayoutInflater();
            Button inflatedButton = 
            (Button)inflater.inflate(R.layout.buttonlayout,null);
            inflatedButton.setText("버튼"+(num++));
            container.addView(inflatedButton);
        }
    });



  }

}

2.mission.xml

->file that offers a Linearlayout(that contains new Buttons) and the inflating Button UI.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/inflation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="#6999"
    android:gravity="center"
    android:padding="5dp"
    android:text="LayoutInflate"
    android:textAllCaps="false"
    android:textSize="10sp" />




    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:background="#6999"
        android:orientation="vertical">


    </LinearLayout>

3.buttonlayout.xml

-->layout of Button to be inflatedenter image description here(textsize and stuff).

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp">
</Button>

PROBLEM & QUESTION: Although I set width and height in buttonlayout.xml to be wrap_content, new manufactured buttons is not correctly adjusted. here's a photo of my situation to help understand :

As you see in the photo, the left one is the objective and the right one is mine.


Solution

  • Please update your xml with below code

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/inflation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#6999"
            android:gravity="center"
            android:padding="5dp"
            android:text="LayoutInflate"
            android:textAllCaps="false"
            android:textSize="10sp" />
    
        <RelativeLayout
            android:id="@+id/containermain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:background="#6999">
    
            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="20dp"
                android:orientation="vertical"/>
    
        </RelativeLayout>
    </LinearLayout>