LOS LAYOUTS
In the layout section of the project, right click, option New and then Layout Resource File
ACTIVITY MAIN (Main activity)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnAlerta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="MosAlerta"
android:text="Mostrar alerta con GridView" />
</RelativeLayout>
THE ITEM FOR THE GRIDVIEW (Location of objects in each gridview item)
item_grid.xml
<?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="wrap_content"
android:padding="1dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="2dp"
android:background="@drawable/bordes"
android:orientation="vertical"
android:padding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fondo"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="@+id/lblNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Titulo"
android:textSize="14dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imgFoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
THE GRIDVIEW (griview object to be embedded in the alert)
grid_alerta.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/idGAlert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:orientation="vertical">
<TextView
android:id="@+id/lblTitu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Titulo de mensaje"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/lblSubtitu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="SubTitulo de mensaje"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="italic" />
<GridView
android:id="@+id/grdGrilla"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@color/colorPrimary"
android:columnWidth="80dp"
android:numColumns="auto_fit" >
</GridView>
</LinearLayout>
LOS DRAWABLES
Should be placed in the drawable section of the NO EN LAYOUT project, they determine the style (border and background) of each grid item.
edges.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#cccccc" />
</shape></item>
<item><shape>
<gradient
android:angle="270" android:endColor="#ccc"
android:startColor="#fff" />
<corners android:radius="2dp" />
</shape></item>
</selector>
fondo.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#cccccc" />
</shape></item>
<item><shape>
<gradient android:angle="90" android:endColor="#f5f5f5" android:startColor="#fff" />
</shape></item>
</selector>
THE ADAPTER
clsAdaptadorGrid.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class clsAdaptadorGrid extends BaseAdapter {
Context Contexto;
String[] Nombre;
int[] Foto;
LayoutInflater inflater;
public clsAdaptadorGrid(Context cContexto, String[] cNombre, int[] cFoto) {
this.Contexto = cContexto;
this.Nombre = cNombre;
this.Foto = cFoto;
}
@Override
public int getCount()
{
return Nombre.length;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView xNombre;
ImageView xFoto;
inflater = (LayoutInflater) Contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_grid, parent, false);
xNombre = (TextView) itemView.findViewById(R.id.lblNombre);
xFoto = (ImageView) itemView.findViewById(R.id.imgFoto);
xNombre.setText(Nombre[position]);
xFoto.setImageResource(Foto[position]);
return itemView;
}
}
THE MAIN ACTIVITY
MainActivity.java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AlertDialog iconDialog;
int Elemento;
String[] aNombres = {"Juan","Carlos","María","Nadia","Perla"};
int[] aFotos = {R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
clsAdaptadorGrid Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void MosAlerta(View v){
Elemento=-1;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Adapter = new clsAdaptadorGrid(this, aNombres, aFotos);
LayoutInflater inflater = this.getLayoutInflater();
View ViewAlerta = inflater.inflate(R.layout.grid_alerta, null);
GridView xGrilla = (GridView) ViewAlerta.findViewById(R.id.grdGrilla);
TextView xTitulo = (TextView) ViewAlerta.findViewById(R.id.lblTitu);
TextView xSubti = (TextView) ViewAlerta.findViewById(R.id.lblSubtitu);
xGrilla.setAdapter(Adapter);
xGrilla.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Elemento = position;
iconDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
});
xTitulo.setText("Usuarios bloqueados");
xSubti.setText("Fíjate bien si estás en la lista");
builder.setView(ViewAlerta);
builder.setCancelable(false);
builder.setPositiveButton("Seleccionar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String sNombre = aNombres[Elemento];
Toast.makeText(getApplicationContext(), "Nombre: " + sNombre + " (item: "+Elemento+")", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cerrar", null);
iconDialog = builder.create();
iconDialog.show();
iconDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
THE CAPTURES