Android - change dialog button location

Is it possible to change the location of a button in a dialog box to the outside of the dialog itself? something like this (red squares are buttons):  dialog with a button outside of the frame

I know I can get a button with:

 dialog.getButton(AlertDialog.BUTTON_NEGATIVE)

but I could not find in the manual a way to change its location.

+4
source share
4 answers
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp"
android:background="#00000000">

<LinearLayout
    android:background="@drawable/border_background"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/update_app"
        android:textSize="18sp"
        android:textColor="@color/white"
        android:layout_gravity="center_horizontal"
        android:gravity="center" />

</LinearLayout>
<Button
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_marginTop="20dp"
    android:background="#123456"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="#ffffff"
    android:textSize="14sp"
    android:onClick="onUpdateClicked"
    android:text="Button" />

Instead of using the default warning dialog box, create your own layout as your layout. And perform the required action on the button.

You can call n by showing this layout without swelling in this way. EDIT: 1

 public void showUpdateLayout() {
    mParentView = (ViewGroup) findViewById(android.R.id.content);
    if (mParentView != null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        mUpdateLayout = inflater.inflate(R.layout.upadte_layout, mParentView, false);
        mParentView.addView(mUpdateLayout);
        if (mUpdateLayout != null) {
            mUpdateLayout.setVisibility(View.VISIBLE);
        }
    }

( ). , .

+1

: android:background="@android:color/transparent"

+1

DialogFragment. , , , Dialog .

CustomAlertDialog.java

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Custom DialogFragment class
*/
public class CustomAlertDialog extends DialogFragment implements
View.OnClickListener {
/**
 * Interface for receiving the wanted callbacks
 * */
public interface CallbacksListener
{
    public void onPositiveButtonClicked();

    public void onNegativeButtonClicked();
}

private CallbacksListener callbacksListener;

public void setCallbacksListener(CallbacksListener callbacksListener)
{
    this.callbacksListener = callbacksListener;
}

public CustomAlertDialog()
{
    //empty constructor
}

private String titleString;
private String messageString;
private String positiveString;
private String negativeString;

@Override
public void setArguments(Bundle bundle)
{
    titleString = bundle.getString("titleString");
    messageString = bundle.getString("messageString");
    positiveString = bundle.getString("positiveString");
    negativeString = bundle.getString("negativeString");
}

public static CustomAlertDialog newInstance(AlertDialogStrings alertDialogStrings)
{
    CustomAlertDialog customAlertDialog = new CustomAlertDialog();
    Bundle b = new Bundle();
    b.putString("titleString", alertDialogStrings.titleString);
    b.putString("messageString", alertDialogStrings.messageString);
    b.putString("negativeString", alertDialogStrings.negativeString);
    b.putString("positiveString", alertDialogStrings.positiveString);
    customAlertDialog.setArguments(b);

    return customAlertDialog;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View v = getActivity().getLayoutInflater().inflate(R.layout.custom_alert_dialog, null);
    TextView titleTV = (TextView) v.findViewById(R.id.title_customAlertDialog);
    TextView messageTV = (TextView) v.findViewById(R.id.message_customAlertDialog);
    Button positiveButton = (Button) v.findViewById(R.id.okBtn_customAlertDialog);
    Button negativeButton = (Button) v.findViewById(R.id.cancelBtn_customAlertDialog);
    titleTV.setText(titleString);
    messageTV.setText(messageString);
    positiveButton.setText(positiveString);
    negativeButton.setText(negativeString);
    positiveButton.setOnClickListener(this);
    negativeButton.setOnClickListener(this);

    builder.setView(v);
    return builder.create();
}

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.okBtn_customAlertDialog:
            callbacksListener.onPositiveButtonClicked();
            dismiss();
            break;
        case R.id.cancelBtn_customAlertDialog:
            callbacksListener.onNegativeButtonClicked();
            dismiss();
            break;
        default:
            break;
    }
}

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    try
    {
        callbacksListener = (CallbacksListener) activity;
    }
    catch (ClassCastException e)
    {
        throw new ClassCastException(activity.toString()
                + " must implement CallbacksListener");
    }
}

@Override
public void onDetach()
{
    super.onDetach();
    callbacksListener = null;
}

/**
 * Class for saving the wanted Strings we want to have on our CustomDialog implementation
 * */
public static class AlertDialogStrings
{
    public String titleString;
    public String messageString;
    public String positiveString;
    public String negativeString;

    public AlertDialogStrings(String title, String message, String positiveString, String negativeString)
    {
        this.messageString = message;
        this.titleString = title;
        this.positiveString = positiveString;
        this.negativeString = negativeString;
    }
  }
}

custom_alert_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:textSize="22sp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:text="My Title Here"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:id="@+id/title_customAlertDialog"/>

<TextView
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="7dp"
    android:id="@+id/message_customAlertDialog"
    android:layout_below="@id/title_customAlertDialog"
    android:textColor="@color/darkGray"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_marginTop="7dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:measureWithLargestChild="true"
    android:layout_below="@+id/message_customAlertDialog"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">

    <Button
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:textSize="13sp"
        android:textColor="@color/primaryColorDark"
        android:layout_width="wrap_content"
        android:layout_weight="1.0"
        android:text="@string/cancel"
        android:id="@+id/cancelBtn_customAlertDialog"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1.0"
        android:layout_marginRight="10dp"
        android:textSize="13sp"
        android:textColor="@color/primaryColorDark"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="@string/ok"
        android:id="@+id/okBtn_customAlertDialog"/>
</LinearLayout>

customAlertDialog:

private void popUpAlertDialog() 
{
    String title = "My title here?";
    String message = "My Message here";
    String positiveString = "OK";
    String negativeString = "Cancel";
    CustomAlertDialog.AlertDialogStrings customDialogStrings =
            new CustomAlertDialog.AlertDialogStrings
                    (title, message, positiveString, negativeString);
    CustomAlertDialog customAlertDialog =
            CustomAlertDialog.newInstance(alertDialogStrings);
    customAlertDialog.show(getSupportFragmentManager(), "customAlertDialog");
    customAlertDialog.setCallbacksListener(new CustomAlertDialog.CallbacksListener()
    {
        @Override
        public void onPositiveButtonClicked()
        {
           //do something 
        }

        @Override
        public void onNegativeButtonClicked()
        {
           //do something
        }
    });
}

AlertDialogStrings , , CallbacksListener , OnClick. , " " .

+1

. , , .

, LinearLayout BG , .

:

<LinearLayout android:orientation="vertical"
              android:layout_width="300"
              android:layout_height="500"
              android:background="@android:color/transparent">

    <LinearLayout android:orientation="vertical"
                  android:layout_width="270"
                  android:layout_height="200">
        ... your content here
    </LinearLayout>

    <Button android:layout_width="270"
            android:layout_height="200"
            android:margin_top="10"/>

</LinearLayout>

Dialog, :

LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.your_dialog_layout, null))

...

Dialog newDialog = new Dialog();
newDialog.setContentView(R.layout.your_dialog_layout);
0

All Articles