Rafaalvfe Rafaalvfe - 6 months ago 40
Android Question

Add custom Popup/view to activity in android

I'm starting in Android development and don't still get how to do this. I have a ListView, when the user taps one of the items I wan´t to display some information about it.
A popup like this:
enter image description here

That's the way it should look, I already have the XML which is this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b5000000">

<RelativeLayout
android:layout_width="340dp"
android:layout_height="400dp"
android:columnCount="20"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:background="@drawable/menubutton">

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="X"
android:id="@+id/infraccionesPopUpCerrar"
android:layout_gravity="right|top"
android:layout_row="0"
android:layout_column="19"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView8"
android:layout_row="2"
android:layout_column="10"
android:layout_below="@+id/infraccionesPopUpCerrar"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Aqui se detallaran las causas de la infraccion"
android:id="@+id/infraccionesPopUpCausa"
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="20"
android:layout_below="@+id/imageView8"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/infraccionesPopUpFecha"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="PAGADA"
android:id="@+id/infraccionesPopUpSituacion"
android:layout_below="@+id/infraccionesPopUpCausa"
android:layout_alignStart="@+id/infraccionesPopUpCausa" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Folio: 00000000"
android:id="@+id/infraccionesPopUpFolio"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/infraccionesPopUpSituacion" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Estacionamiento prohibido"
android:id="@+id/infraccionesPopUpTitulo"
android:layout_below="@+id/imageView8"
android:layout_centerHorizontal="true"
android:textColor="#74cb51" />

</RelativeLayout>




How do I instantiate this Popup so I can change the content of the textviews inside? The information that displays the popup depends on what item he tap

Answer

You can make use of DialogFragment, it has some cool predefined layouts but you need your custom one.

public class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
  // Inflate the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View view = inflater.inflate(R.layout.dialog_custom_layout, null);

        // Get your views by using view.findViewById() here and do your listeners. 
  ...

  // Set the dialog layout
        builder.setView(view);

        return builder.create();
    }

}

More info can be found at the official documentation.