diff --git a/app/src/main/java/com/mtb/foodorderreview/HomeFoodTypeGridAdapter.java b/app/src/main/java/com/mtb/foodorderreview/HomeFoodTypeGridAdapter.java index a2e5c59..96efbe0 100644 --- a/app/src/main/java/com/mtb/foodorderreview/HomeFoodTypeGridAdapter.java +++ b/app/src/main/java/com/mtb/foodorderreview/HomeFoodTypeGridAdapter.java @@ -13,8 +13,8 @@ public class HomeFoodTypeGridAdapter extends BaseAdapter { private Context context; - private List list; - private LayoutInflater inflater; + private final List list; + private final LayoutInflater inflater; public HomeFoodTypeGridAdapter(Context context, List list) { this.list = list; @@ -24,7 +24,7 @@ public HomeFoodTypeGridAdapter(Context context, List list) { @Override public int getCount() { - return 0; + return list.size(); } @Override diff --git a/app/src/main/java/com/mtb/foodorderreview/MainActivity.java b/app/src/main/java/com/mtb/foodorderreview/MainActivity.java index 0bf2396..b089b5b 100644 --- a/app/src/main/java/com/mtb/foodorderreview/MainActivity.java +++ b/app/src/main/java/com/mtb/foodorderreview/MainActivity.java @@ -1,11 +1,15 @@ package com.mtb.foodorderreview; import android.os.Bundle; -import android.widget.LinearLayout; +import android.view.View; +import android.widget.AdapterView; +import android.widget.GridView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; +import java.util.Arrays; + public class MainActivity extends AppCompatActivity { @Override @@ -13,10 +17,32 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + HomeFoodTypeUI(); + } + + private void HomeFoodTypeUI() { + HomeFoodType rice = new HomeFoodType("Rice", R.drawable.icon_food_type_rice); + HomeFoodType rice2 = new HomeFoodType("Rice2", R.drawable.icon_food_type_rice); + HomeFoodType rice3 = new HomeFoodType("Rice3", R.drawable.icon_food_type_rice); + HomeFoodType rice4 = new HomeFoodType("Rice4", R.drawable.icon_food_type_rice); + HomeFoodType rice5 = new HomeFoodType("Rice5", R.drawable.icon_food_type_rice); + + HomeFoodType[] l = {rice, rice2, rice3, rice4, rice5}; + + HomeFoodTypeGridAdapter adapter = new HomeFoodTypeGridAdapter(this, Arrays.asList(l)); + + GridView gridView = findViewById(R.id.home_food_type_grid_1); + gridView.setAdapter(adapter); + + Toast.makeText(this, "Hihi", Toast.LENGTH_SHORT).show(); - LinearLayout layout = findViewById(R.id.LinearLayout1); - layout.setOnClickListener(v -> { - Toast.makeText(this, "Hhihi", Toast.LENGTH_SHORT).show(); + gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Object o = gridView.getItemAtPosition(position); + HomeFoodType foodType = (HomeFoodType) o; + Toast.makeText(MainActivity.this, "Selected :" + " " + foodType.getName(), Toast.LENGTH_LONG).show(); + } }); } } \ No newline at end of file diff --git a/app/src/main/res/drawable/shape_food_type_box.xml b/app/src/main/res/drawable/shape_food_type_box.xml index 5f19199..4a9a823 100644 --- a/app/src/main/res/drawable/shape_food_type_box.xml +++ b/app/src/main/res/drawable/shape_food_type_box.xml @@ -1,5 +1,5 @@ - - + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c71ba53..0c270f6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,5 @@ - - - - - - - - - - - - + android:numColumns="4" + android:paddingHorizontal="20sp" + android:verticalSpacing="20sp" + android:horizontalSpacing="20sp" /> \ No newline at end of file diff --git a/app/src/main/res/layout/home_food_type_grid_layout.xml b/app/src/main/res/layout/home_food_type_grid_layout.xml index 479c31c..b59ed42 100644 --- a/app/src/main/res/layout/home_food_type_grid_layout.xml +++ b/app/src/main/res/layout/home_food_type_grid_layout.xml @@ -8,10 +8,9 @@ android:layout_height="wrap_content" android:background="@drawable/shape_food_type_box" - android:padding="12sp" + android:padding="8sp" android:orientation="vertical"> - @@ -22,21 +21,24 @@ android:layout_width="match_parent" android:layout_height="0dp" + app:layout_constraintDimensionRatio="3:2" + android:importantForAccessibility="no" android:src="@drawable/icon_food_type_rice" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - + - \ No newline at end of file + android:text="TextView TextView" + android:layout_marginTop="8dp" + android:textSize="12sp" /> +