Mochamad Lucky Pradana Mochamad Lucky Pradana - 3 months ago 18
Java Question

Android - Click Button Inside alertDialog

I wonder how to click a Button inside of

AlertDialog
in Android and this is my code

activity_float_info.xml

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />


MainActivity.java

QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);

MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(Boolean.TRUE)
.setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setView(R.layout.activity_float_info)
.show();

button = (Button)QR_.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setText("TEst");
}
});
}
});


I have inflate the layout..
I think the main problem is on

button = (Button)QR_.findViewById(R.id.button);

Answer

try this create a layout for dialog box

<TextView android:id="@+id/dialogtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@android:color/black"
    android:text="Please enter the email address you used for the account"
    />
<EditText
    android:id="@+id/emailedittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:ems="10"
    android:padding="5dp"
    android:cursorVisible="true"
    android:singleLine="true"
    android:background="@android:color/white"
    android:textColor="@android:color/black"
    android:hint="Enter Mail id"
    android:textSize="20dp" >
    <requestFocus />
</EditText>

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal">

     <Button 
        android:id="@+id/cancelbtn"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="CANCEL"/>

      <Button 
        android:id="@+id/okbtn"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Ok"/>
</LinearLayout>

Then create a dialog box using this layout and handle button clicks

final Dialog dialog = new Dialog(MainActivity.this);
      // Include dialog.xml file
      dialog.setContentView(R.layout.forgotpassword);
      // Set dialog title
      dialog.setTitle("ALERT!!");
      // set values for custom dialog components - text, image and button
      Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
      Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
      final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
      dialog.show();
      dialog.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
      // if decline button is clicked, close the custom dialog
      cancelbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // Close dialog
              dialog.dismiss();
          }
      });
      okbtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String email=emailedittext.getText().toString();
              //do something more here
          }
      });

Refer: http://coderzpassion.com/android-show-alertdialog/

Comments