javaandroidandroid-layoutlayout-inflater

Android Dynamically center the buttons when inflating layout


I need to center layout dynamically, but the problem is I use inflate method to add the layout. And I want buttons to be center.

I tried adding xml attributes like gravity, layout_gravity. But it didn't work. And I tried to add parameters, but since I inflate xml I can't add parameters. Because I can't use addRule method.

Then I tried to make every element center by adding gravity attribute to root layout, it didn't work out either.

Here is the my code.

actvity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="335dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:id="@+id/buttons_layout"
        android:layout_below="@+id/surfaceView1"
        >

        <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:text="Button" />

    </RelativeLayout>



</RelativeLayout>

three_buttons_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="29dp"
        android:text="Button 1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="29dp"
        android:text="Button 2" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>

And here is my java code

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     parent = (ViewGroup)findViewById(R.id.buttons_layout);

     captureButton.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
        camera.takePicture(null, null, photoCallback);
        captureButton.setVisibility(View.GONE);
        inflater.inflate(R.layout.three_buttons_layout, parent, true);
            }
        });

I want Button1, Button2, and Button3 to be center.

screenshot


Solution

  • A horizontal LinearLayout will always left-align its subviews. You're going to have to use a RelativeLayout in order to get the positioning you want. I'd suggest giving button1 the android:layout_alignParentLeft attribute, button2 the android:layout_centerHorizontal attribute and button3 the android:layout_alignParentRight attribute, like this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="29dp"
        android:text="Button 1" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:text="Button 2" />
    
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button 3" />
    
    </RelativeLayout>
    

    (I'm just editing your code in the comment editor, so there might be a syntax error in there somewhere, but you get the idea.)

    Also, you should be able to set any attribute you want on those buttons after inflating the view. Just call findViewById(R.id.button1) and it should return you the view you want (as long as its after the inflater.inflate call). On that note, you've given all your buttons the same id, which probably isn't a good idea.

    Hope that helps! Let me know if it's still not working.

    Edit: I just realized you're going to need that RelativeLayout to have android:layout_width="match_parent". I've edited the above xml accordingly.