Carlos Porta
Carlos Porta

Reputation: 1234

How to centralize buttons on android?

I'm having troubles to create a layout like that, the Dialog on left side of image. But I want to create it using horizontal orientation. I want that my layout have this appearance, but only with two buttons horizontally.

My problem is the layout xml file, I don't know how to start this layout... =(

The real problem is how to edit the xml file to the buttons get centralized, I tried a lot of thinks, padding, orientation, align etc... But I cant align that. My current layout is that. How can I centralize the buttons?

Can someone help me?

And here is my xml file code...

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:id="@+id/textView6"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:id="@+id/textView7"
/>

Can someone help me?

Upvotes: 0

Views: 190

Answers (4)

ifeomaro
ifeomaro

Reputation: 2764

First, you need to create your desired layout with the buttons that you mentioned (I figured you can do this yourself). Then, you would use that layout to inflate an AlertDialog as I have done below:

View alertView = getLayoutInflater().inflate(
                R.layout.your_custom_layout,
                (ViewGroup) findViewById(R.id.alertViewLayout));

new AlertDialog.Builder(this)
        .setTitle(title)
        .setView(alertView)
        .setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // handle click here

                        // dismiss AlertDialog
                        dialog.dismiss();
                    }
                })
        .setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // handle click here

                        // dismiss AlertDialog
                        dialog.dismiss();
                    }
                }).show();

You could access the views in the AlertDialog's layout by referencing the parent view (alertView);

ImageButton button1 = (ImageButton) alertView.findViewById(R.id.button1);
ImageButton button2 = (ImageButton) alertView.findViewById(R.id.button2);

Edit

This is a sample layout file with 2 ImageButtons placed side-by-side.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/alertViewLayout"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:layout_weight=".5"
        android:background="@+drawable/imageButton1"
        android:text="Button 1" />

    <ImageButton
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center"
        android:layout_weight=".5"
        android:background="@+drawable/imageButton2"
        android:text="Button 2" />

</LinearLayout>

Edit 2

Since you want your buttons to have custom backgrounds, I suggest you use an ImageButton and set the background property to your choice background.

If you want more details on how to create a layout file, you can check Android Developer documentation.

Let me know if this helps.

Upvotes: 1

Brahim Belghmi
Brahim Belghmi

Reputation: 151

you can do somethnig like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:id="@+id/button2" />
    </LinearLayout>
</RelativeLayout>

Upvotes: 1

T D Nguyen
T D Nguyen

Reputation: 7613

Simplest answer:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:gravity="center_vertical"
android:orientation = "horizontal">

 <Button
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="Large Text"
 android:id="@+id/textView6"
 />

 <Button
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="Large Text"
 android:id="@+id/textView7"/>

</LinearLayout>

Upvotes: 0

Gabriel R.
Gabriel R.

Reputation: 328

Could you be more clear in your question please? I don't know if I got it right but I will try to explain it.

First, if you want to create a custom layout for you dialog, you should create a .xml file and then inflate it with this specific layout.

But I think that is not your case, what you want to do it's to simply create a custom dialog, right? The screen orientation doesn't matter (I don't know if you meant it).

I don't think I should explain you the whole process or just paste the code, it's better to you to get used with android official documentation, so take a look at this link: Dialogs | Android Development

Hope it can help you.

Upvotes: 0

Related Questions