Sure, here's an example of the Android Studio project that you can use as a starting point. It uses GridView to display images and text labels, implements the App Bar Options Menu with a search function, and shows information about the selected image in the second activity.
Create a project in Android Studio with a GridFragment class
java Copy code
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
public class GridFragment extends Fragment {
GridView gridView;
// Sample drawable image names
String[] names = new String[]{
"Flower", "Animal", "Book", "Computer", "Guitar", "Painting", "Speaker", "Television"
};
// Sample drawable image resource IDs
int[] resourceIds = new int[]{
R.drawable.flower, R.drawable.animal, R.drawable.book, R.drawable.computer,
R.drawable.guitar, R.drawable.painting, R.drawable.speaker, R.drawable.television
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_grid, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(new CustomGridAdapter(getContext(), names, resourceIds));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NavHostFragment.findNavController(GridFragment.this)
.navigate(R.id.action_GridFragment_to_DetailFragment);
}
});
}
}
Create an XML layout file for the GridFragment
xml Copy code
<!-- res/layout/fragment_grid.xml -->
<androidx.constraintlayout.widget.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"
tools:context=".GridFragment">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Create the adapter class for the GridView - CustomGridAdapter.java
java Copy code
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 CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] names;
private int[] resourceIds;
public CustomGridAdapter(Context context, String[] names, int[] resourceIds) {
this.context = context;
this.names = names;
this.resourceIds = resourceIds;
}
@Override
public int getCount() {
return names.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) {
View view;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) view.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) view.findViewById(R.id.grid_image);
textView.setText(names[position]);
imageView.setImageResource(resourceIds[position]);
} else {
view = convertView;
}
return view;
}
}
Create XML layout file for the grid_single
xml Copy code
<!-- res/layout/grid_single.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/grid_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>