Skip to content

Commit

Permalink
HomeFoodType
Browse files Browse the repository at this point in the history
  • Loading branch information
minhquan1313 committed Jun 29, 2023
1 parent c5b475e commit 69188e9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class HomeFoodTypeGridAdapter extends BaseAdapter {
private Context context;

private List<HomeFoodType> list;
private LayoutInflater inflater;
private final List<HomeFoodType> list;
private final LayoutInflater inflater;

public HomeFoodTypeGridAdapter(Context context, List<HomeFoodType> list) {
this.list = list;
Expand All @@ -24,7 +24,7 @@ public HomeFoodTypeGridAdapter(Context context, List<HomeFoodType> list) {

@Override
public int getCount() {
return 0;
return list.size();
}

@Override
Expand Down
34 changes: 30 additions & 4 deletions app/src/main/java/com/mtb/foodorderreview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
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
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();
}
});
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/shape_food_type_box.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/grey_1" />
<corners android:radius="4sp" />
<solid android:color="@color/white" />
<corners android:radius="12sp" />
</shape>
44 changes: 5 additions & 39 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
Expand All @@ -14,46 +13,13 @@


<GridView
android:id="@+id/home_food_type_grid_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4" />

<LinearLayout
android:id="@+id/LinearLayout1"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:background="@drawable/shape_food_type_box"
android:padding="12sp"
android:orientation="vertical">


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/icon_food_type_rice"
android:importantForAccessibility="no" />

</androidx.constraintlayout.widget.ConstraintLayout>

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginTop="8sp"
android:text="TextView TextView" />
</LinearLayout>
android:numColumns="4"
android:paddingHorizontal="20sp"
android:verticalSpacing="20sp"
android:horizontalSpacing="20sp" />


</LinearLayout>
16 changes: 9 additions & 7 deletions app/src/main/res/layout/home_food_type_grid_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand All @@ -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" />

</androidx.constraintlayout.widget.ConstraintLayout>


<TextView
android:id="@+id/home_food_type_grid_layout_text_1"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginTop="8sp"
android:text="TextView TextView" />
</LinearLayout>
android:text="TextView TextView"
android:layout_marginTop="8dp"
android:textSize="12sp" />
</LinearLayout>

0 comments on commit 69188e9

Please sign in to comment.