0

In an android app, I have a second screen which contains only a ListView. It is presented by clicking on a Button in the main screen and is closed when the user clicked on an Item.

The problem is that there is a relatively big empty area in my second screen and does not look good.

The question is: how can I limit the size of my second screen so that it looks like a DialogBox but with a ListView?

ext: The ListView is just an example and the content of the second screen could be every thing like a Compound View which is complex but does not fill the whole screen. So it is important to me to implement the second screen in XML not programmatically.

XML-code of my second screen:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/myListView2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        >
    </ListView>
</LinearLayout>

-- I tried to limit the layout_width and layout_height of the second screen to wrap_content or 300dp but non of them works.

Thanks for your answer!

Behy
  • 483
  • 7
  • 23
  • Refer this SO Answer: http://stackoverflow.com/questions/15762905/how-can-i-display-a-list-view-in-an-android-alert-dialog – Amy Jun 24 '15 at 07:36
  • do you have list view inside actvity ? – Amarjit Jun 24 '15 at 08:01
  • actually I call a secondary class (related to the second screen) via `Intent`. It generates the items of the `ListView` and send to the Screen via `ArrayAdapter`. – Behy Jun 24 '15 at 08:08

3 Answers3

0

Use This Code in onCLick

AlertDialog.Builder builderSingle = new AlertDialog.Builder(
                DialogActivity.this);
        builderSingle.setIcon(R.drawable.ic_launcher);
        builderSingle.setTitle("Select One Name:-");
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                DialogActivity.this,
                android.R.layout.select_dialog_singlechoice);
        arrayAdapter.add("Hardik");
        arrayAdapter.add("Archit");
        arrayAdapter.add("Jignesh");
        arrayAdapter.add("Umang");
        arrayAdapter.add("Gatti");
        builderSingle.setNegativeButton("cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builderSingle.setAdapter(arrayAdapter,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String strName = arrayAdapter.getItem(which);
                        AlertDialog.Builder builderInner = new AlertDialog.Builder(
                                DialogActivity.this);
                        builderInner.setMessage(strName);
                        builderInner.setTitle("Your Selected Item is");
                        builderInner.setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        dialog.dismiss();
                                    }
                                });
                        builderInner.show();
                    }
                });
        builderSingle.show();

This will display the listview in the dialogue

For more info click here

Amaresh Jana
  • 732
  • 11
  • 22
0

Finally I could find the answer!

The only thing to do is to set the android.theme to Dialog in the AndroidManifest, like this:

    <activity android:name=".SecondActivity"
              android:label="secondAct"
              android:theme="@android:style/Theme.Holo.Dialog"

Compared to AlertDialog.Builder, this method has the advantage of customizing the secondary layout without any limitation in terms of the type of views. And the secondary layout should not be created programmatically.

Behy
  • 483
  • 7
  • 23
-1

your listview No content try adapter