XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="@drawable/gradient_touchscreen"
tools:context=".tochscreen_page_2">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
android:gravity="center"
/>
</android.support.constraint.ConstraintLayout>
Java
public class tochscreen_page_2 extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tochscreen_page_2);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(tochscreen_page_2.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
R.drawable.gradient_charger, R.drawable.gradient_flashlight,
R.drawable.gradient_vibrtion, R.drawable.gradient_charger,
};
}
Это результат:
http://prntscr.com/kvqe34