Ronak Mehta
Ronak Mehta

Reputation: 5979

How to create a pop up menu in Android

i tried the following code

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListAdapter;

public class Custom_dialogActivity extends Activity {
/** Called when the activity is first created. */
String test[] = {"Security","Data Backup","Missing Device"};
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle("My Title");
    builder.setInverseBackgroundForced(true);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setItems(test, new OnClickListener() {  
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            System.out.println("onClick " + which);

        }
    });
    builder.setAdapter(adapter, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            System.out.println("DialogInterface : " + which);
        }
    });
    EditText ed = new EditText(this);
    builder.setView(ed);
    builder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                dialog.dismiss();
                             }
                       });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,int which)
                            {
                                dialog.dismiss();
                            }
                        });

                AlertDialog alert = builder.create();
                alert.show();
          }
}

**but it looks like but i tried to set color and icon but failed to setIcon and Color Green on Top of Dialog box solve my problem and i didint created any xml and didnt made any changes **

enter image description here

but i want like

enter image description here

Upvotes: 2

Views: 6645

Answers (3)

waqaslam
waqaslam

Reputation: 68187

You can achieve this by using AlertDialog.Builder, but instead of using standard dialog, you need to inflate your own xml to override the view.

update

or create an activity with dialog style:

your custom style:

<style name="styleActivityDialog" parent="@android:style/Theme.Light.NoTitleBar">
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowFrame">@null</item>
 <item name="android:windowIsFloating">true</item>
 <item name="android:windowContentOverlay">@null</item>
 <item name="android:colorBackgroundCacheHint">@android:color/transparent</item>
 <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
 <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>

 <item name="android:windowBackground">@drawable/bg_activity_dialog</item>
</style>

drawable bg_activity_dialog:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">

      <stroke android:width="1dp" android:color="#C0C0C0" />
      <solid android:color="#FFFFFF" />
      <corners android:radius="5dp" />
      <padding android:top="5dp" android:bottom="5dp" android:left="5dp" 
         android:right="5dp"/>
    </shape>

your activity in AndroidManifest.xml:

<activity android:name=".ActivityDialog"
     android:theme="@style/styleActivityDialog">
</activity>

now create any layout for your activity ActivityDialog and it should appear as a dialog window

Upvotes: 5

Matt M
Matt M

Reputation: 804

You could create an Activity with the layout you desire and add the following theme to the Activity's tag in the AndroidManifest.xml to simulate a dialog look.

android:theme="@android:style/Theme.Dialog"

If that is not enough, you can use inheritance and modify this specific theme to use a translucent, non-dim background.

<style name="MyTheme" parent="@android:style/Theme.Dialog">
     <item name="android:windowIsTranslucent">true</item>
     <item name="android:windowBackground">@android:color/transparent</item>
     <item name="android:backgroundDimEnabled">false</item>
</style>

Upvotes: 2

Deepthi G
Deepthi G

Reputation: 79

You can use AlertDialog or custom dialog. I've discussed about this in my personal blog.You can just visit my humble blog if you desire.. http://androiddesk.wordpress.com/tag/dialog-in-android/page/2/

Upvotes: 2

Related Questions