Nazmul Hasan
Nazmul Hasan

Reputation: 113

Android Custom Dialog with Dismiss Button

I have an action bar containing "About" and "An Image" and "Settings" when "About is touched" custom dialog button open and I have used a Button in the xml file id "dialogButton"

Basically, I want the dialog to dismiss when it's touched.

Actionbar.java:

package com.example.actionbar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class ActionBar extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.testmenu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()){
    case R.id.about:
        aboutMenuItem();
        break;
    case R.id.settings:
        settingsMenuItem();
        break;
    case R.id.item3:
        item3MenuItem();
        break;
    }
    return true;
}   
private void aboutMenuItem(){
    final Dialog d = new Dialog(this);
    d.setContentView(R.layout.dialog_custom);
    d.setTitle("Custom Dialog");        
    d.findViewById(R.id.dialogButton);
    d.show();
}

private void settingsMenuItem() { }

private void item3MenuItem() { }

}

and my xml file, dialog_custom.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:id="@+id/dialog_webview"
    android:layout_width="match_parent"
    android:layout_height="68dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello Dialog" />

<Button
    android:id="@+id/dialogButton"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:text="OK" />

Upvotes: 0

Views: 1625

Answers (3)

Mukesh Rana
Mukesh Rana

Reputation: 4091

You can do something like this in your aboutMenuItem() function.

      final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        View view = getLayoutInflater().inflate(R.layout.yourCustomDialogLayout,
                null);

        Button dialogButton = (Button) view.findViewById(R.id.dialogButton);

        dialogButton .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.show();

Upvotes: 1

raja
raja

Reputation: 388

Try This

private void aboutMenuItem(){
    final Dialog d = new Dialog(this);
    d.setContentView(R.layout.dialog_custom);
    d.setTitle("Custom Dialog");
    Button connect = (Button) d.findViewById(R.id.dialogButton);

    connect.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            d.dismiss();
      }
    });
  d.show();
 }

Upvotes: 1

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

you can try this..

final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); // find button with dialog's preference..

        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

Upvotes: 0

Related Questions