Using RecyclerView and GridView
Layout file:
<androidx.recyclerview.widget.RecyclerView android:id="@ + id/fjm" android:layout_width="match_parent" android:layout_height="50dp"/> <GridView android:id="@ + id/grid" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2"//Set a row of two />
code:
public void recy(){ List<String>m=new ArrayList<>(); m.add("Living Room");m.add("Kitchen");m.add("Bedroom"); m.add("Washroom");m.add("Kitchen");m.add("Kitchen"); class r1 extends RecyclerView. ViewHolder{ Button b; public r1(@NonNull @NotNull View itemView) { super(itemView); b=(Button)itemView.findViewById(R.id.button2); } } class r2 extends RecyclerView.Adapter<r1>{ @NonNull @NotNull @Override public r1 onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) { View vi=View.inflate(getActivity(),R.layout.buttonyi,null); r1 r=new r1(vi); return r; } @Override public void onBindViewHolder(@NonNull @NotNull r1 holder, int position) { holder.b.setText(m.get(position)); holder.b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { switch (holder. b. getText(). toString()){ case "Living Room": try { grid.setAdapter(new gAda(ja.getJSONObject(0))); } catch (JSONException e) { e.printStackTrace(); } break; case "Kitchen": try { grid.setAdapter(new gAda(ja.getJSONObject(1))); } catch (JSONException e) { e.printStackTrace(); } break; case "Bedroom": try { grid.setAdapter(new gAda(ja.getJSONObject(2))); } catch (JSONException e) { e.printStackTrace(); } break; case "Washroom": try { grid.setAdapter(new gAda(ja.getJSONObject(3))); } catch (JSONException e) { e.printStackTrace(); } break; } } }); } @Override public int getItemCount() { return m. size(); } } fjm.setAdapter(new r2()); LinearLayoutManager l=new LinearLayoutManager(getActivity()); l.setOrientation(RecyclerView.HORIZONTAL); fjm.setLayoutManager(l); } JSONArray ja=new JSONArray(); JSONObject Living=new JSONObject(); JSONObject Kitchen=new JSONObject(); JSONObject Bedroom = new JSONObject(); JSONObject Washroom = new JSONObject(); public void setGrid() throws JSONException { Living.put("1",R.drawable.fsj2);Living.put("2",R.drawable.fsj2);Living.put("3",R.drawable.fsj2);Living.put(" 4",R.drawable.fsj2); Living.put("5","Air\ Conditioner");Living.put("6","Smart\ Lamp");Living.put("7","Smart\ Tv");Living.put(" 8","Google Smart\ Home"); Kitchen. put("1", R. drawable. bz); Kitchen. put( "2", R. drawable. bz); Kitchen. put("3", R. drawable. bz); Kitchen.put("4","Air\ Conditioner2");Kitchen.put("5","Smart\ Lamp2"); Kitchen.put("6","Smart\ Tv2"); Bedroom.put("1", R.drawable.fz2);Bedroom.put( "2",R.drawable.fz2); Bedroom.put("3","Air\ Conditioner3");Bedroom.put("4","Smart\ Lamp3"); Washroom.put("1", R.drawable.zhu);Washroom.put("2","Air\ Conditioner4"); ja.put(Living); ja.put(Kitchen); ja.put(Bedroom); ja.put(Washroom); grid.setAdapter(new gAda(ja.getJSONObject(0))); } class gAda extends BaseAdapter{ JSONObject jo; public gAda(JSONObject jo){ this.jo=jo; } @Override public int getCount() { return jo. length()/2; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if(view==null){ view = LayoutInflater.from(getActivity()).inflate(R.layout.kapian,null); } try { ImageView image; TextView text; SwitchCompat switch; LinearLayout ka; image = (ImageView) view.findViewById(R.id.image); text = (TextView) view.findViewById(R.id.text); swich = (SwitchCompat) view.findViewById(R.id.swich); ka=(LinearLayout)view.findViewById(R.id.ka); image.setImageResource(jo.getInt((i + 1) + "")); text.setText(jo.getString((i + (jo.length()/2) + 1) + "")); swich.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(swich. isChecked()){ swich.setText("Off"); image.setColorFilter(R.color.cheng); ka.setBackgroundResource(R.drawable.cf); }else{ swich.setText("On"); image.setColorFilter(R.color.qing); ka.setBackgroundResource(R.drawable.qf); } } }); }catch (Exception e){ } return view; } }
GridView layout file
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@ + id/ka" android:layout_width="200dp" android:layout_height="180dp" android:layout_gravity="center" android:layout_marginTop="4dp" android:background="@drawable/qf"//<corners android:radius="10dp"/> android:orientation="vertical" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingRight="10dp" android:paddingLeft="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@ + id/image" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/bf" android:padding="10dp" app:srcCompat="@android:drawable/zoom_plate" app:tint="@color/qing" /> <TextView android:id="@ + id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Air\ Conditioner" android:textColor="@color/white" android:textSize="20sp" /> <androidx.appcompat.widget.SwitchCompat android:id="@ + id/swich" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="On" android:textColor="@color/white" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Layout file of RecyclerView
<?xml version="1.0" encoding="utf-8"?> <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"> <Button android:id="@ + id/button2" android:layout_width="wrap_content" android:layout_height="30dp" android:text="Button" android:layout_margin="3dp" android:textAllCaps="false" android:background="@drawable/qf1"//<corners android:radius="5dp"/> android:textColor="@color/white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Result rendering:
im
ImageView icon change color method
Method 1: Add layout files
app:tint="#ff0000"//Attention is app:
Method 2: Add code dynamically
imageView.setColorFilter(Color.WHITE);