2

I have a custom alert dialog, in which i set a list view and its background is white.

But i am getting a view like this.

image

This is the screenshot which is fits entire screen.

 Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);       
        CustomDialogListAdapter customDialogAdapter;
        ListView customList = new ListView(context);
        customList.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        customDialogAdapter = new CustomDialogListAdapter(context,PaymentInfo.creditCardTypes, type);
        customList.setAdapter(customDialogAdapter);
        customList.setBackgroundColor(Color.WHITE);
        d.setContentView(customList);
        d.show();

Thanks in advance..!

Noby
  • 6,562
  • 9
  • 40
  • 63

5 Answers5

7
Context mContext = getApplicationContext(); 
Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog); 
dialog.show();

the above code will generate a transparent dialog box. So your listviews background will be the only background for the dialog box.

Permita
  • 5,503
  • 1
  • 16
  • 21
  • 1
    Hi, thank you for your useful answer, still i am getting one problem in this. The dialog is coming full screen. how to avoid that. I have updated the code. – Noby Oct 18 '11 at 09:05
  • Here, size of the dialog is depending on the size of listview. We can try limiting the height of listview. I haven't tried this, but here's an idea to limit the size of listview: `ViewGroup.LayoutParams params = customList.getLayoutParams();` `params.height = 300;` `customList.requestLayout();` here the height is specified in "px" – Permita Oct 18 '11 at 10:05
  • hi, can u please add a screenshot – Permita Oct 20 '11 at 05:24
  • I could get dis work only by having a xml layout for the dialog. Here is the code: `Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);` `d.setContentView(R.layout.list_layout);` `CustomDialogListAdapter customDialogAdapter;` `ListView customList=(ListView) d.findViewById(R.id.list_view);` `customDialogAdapter = new CustomDialogListAdapter(context,PaymentInfo.creditCardTypes, type);` `customList.setAdapter(customDialogAdapter);` `customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);` `customList.setBackgroundColor(Color.WHITE);` `d.show();` – Permita Oct 21 '11 at 08:13
  • Here is code of list_layout.xml : `` `` `` – Permita Oct 21 '11 at 08:21
  • android:layout_height="match_parent" i can't use this, I am developing fro android 2.1. and i am not coding xml file for list view i am creating it programatically. – Noby Oct 21 '11 at 08:57
  • you can use "fill_parent" instead of "match_parent". Also, I have tried the programmatic way, however was unable to make it look the you wished. – Permita Oct 21 '11 at 09:28
  • can you please post me the programmatic approach for this solution. I tried but not working. Please post me the code. Thank you for you support. – Noby Oct 21 '11 at 10:06
  • Try dis, working perfectly at my end: `Dialog d = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);` `RelativeLayout listLayout= new RelativeLayout(context);` `CustomDialogListAdapter customDialogAdapter;` `ListView customList = new ListView(context);` `RelativeLayout.LayoutParams param=new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WRAP_CONTENT);` `param.addRule(RelativeLayout.CENTER_IN_PARENT);` `customDialogAdapter = new CustomDialogListAdapter(context, PaymentInfo.creditCardTypes, type);` `customList.setAdapter(customDialogAdapter);` – Permita Oct 21 '11 at 12:18
  • `customList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);` `customList.setBackgroundColor(Color.WHITE);` `listLayout.addView(customList, param);` `d.setContentView(listLayout);` `d.show();` – Permita Oct 21 '11 at 12:18
1
 AlertDialog.Builder screenDialog = new AlertDialog.Builder(AndroidCaptureScreen.this);
    screenDialog.setTitle("Captured Screen");



    TextView TextOut = new TextView(AndroidCaptureScreen.this);
    TextOut.setText(EditTextIn.getText().toString());
    LayoutParams textOutLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TextOut.setLayoutParams(textOutLayoutParams);

    ImageView bmImage = new ImageView(AndroidCaptureScreen.this);
    bmImage.setImageBitmap(bmScreen);
    LayoutParams bmImageLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    bmImage.setLayoutParams(bmImageLayoutParams);

    LinearLayout dialogLayout = new LinearLayout(AndroidCaptureScreen.this);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);
    dialogLayout.addView(TextOut);
    dialogLayout.addView(bmImage);
    screenDialog.setView(dialogLayout);

    screenDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

         }
        });
    screenDialog.show();
   }
Android
  • 2,383
  • 1
  • 26
  • 44
  • I don't have a tittle or a button in my custom layout. The layout params which you have defined here I have done same in xml style. Still the output remains same... :( – Noby Oct 12 '11 at 07:00
0

Get the parent view layout and set its background :)

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
  • probably he referes at something like: `dialog.getWindow().getDecorView().getRootView().setBackgroundColor(android.R.color.transparent);` – Radu Comaneci May 16 '13 at 13:14
0

This will make it work :

in the Listview add this

android:cachecolorhint="#00000000"

Nitin
  • 1,451
  • 13
  • 17
0

In my case ,i got the desire output by using this code snippet :

Dialog dialog2 = new Dialog(Activity.this);
ListView modeList = new ListView(Activity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);


 String[] stringArray = new String[] { "No results" };

  ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(Activity.this, R.layout.list_main, R.id.item_subtitle, stringArray);
  modeList.setAdapter(modeAdapter);

 builder.setView(modeList);
 dialog2 = builder.create();
 dialog2.show();

And in the list_main.xml ,set the background color white :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="7dp"
android:background="#ffffff">
</LinearLayout>

For custom adapter :

    builder.setTitle(" title");
            MySimpleAdapter adapter = new MySimpleAdapter(Activity.this, data , R.layout.list_main, 
                    new String[] { "name", "distance" ,"phone","web"}, 
                    new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
            modeList.setAdapter(adapter);
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • I am having my own custom view to load into the list for that i have my own adapter. How can i do with that..? as you are using ArrayAdapter. I tried your approach with my adapter but its not working. – Noby Oct 12 '11 at 06:44
  • I also used this for custom adapter only...just replace ArrayAdapter with your own adapter..check the edited answer.. – Nibha Jain Oct 12 '11 at 06:50