-2

How can I display a list view in an Android Alert Dialog? i tried this.

Community
  • 1
  • 1
Harihar
  • 35
  • 1
  • 7

2 Answers2

1

Try this:

custom_dialog.xml

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

   <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

Activity/Fragment.java

RecyclerView mRecyclerView; 
AlertDialog.Builder builder = new AlertDialog.Builder(context); 
LayoutInflater inflater = LayoutInflater.from(context);
View content = inflater.inflate(R.layout.custom_dialog, null);
builder.setView(content); 
mRecyclerView = (RecyclerView) content.findViewById(R.id.my_recycler_view);
Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
1

you should extend DialogFragment and create custom dialog which will have recycler view in it.

Layout :-fragment_dialog.xml

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

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

your custom dialog :- MyDialogFragment

public class MyDialogFragment extends DialogFragment {
    private RecyclerView mRecyclerView;
    // this method create view for your Dialog
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          //inflate layout with recycler view
         View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        //setadapter
         //get your recycler view and populate it.
         return v;
    }
}
santosh kumar
  • 2,952
  • 1
  • 15
  • 27