I want to design a game with Java and I want to do something in the project if the background value of an image button is equal to a certain image, how can I do this?
I have no idea about this problem and I do not know how to define if for photos ...
this is java code:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mnm = findViewById(R.id.textView);
Button but = findViewById(R.id.button);
Button butt = findViewById(R.id.buttonn);
ImageButton but1 = findViewById(R.id.button1);
ImageButton but2 = findViewById(R.id.button2);
ImageButton but3 = findViewById(R.id.button3);
ImageButton but4 = findViewById(R.id.button4);
ImageButton but5 = findViewById(R.id.button5);
ImageButton but6 = findViewById(R.id.button6);
ImageButton but7 = findViewById(R.id.button7);
ImageButton but8 = findViewById(R.id.button8);
ImageButton but9 = findViewById(R.id.button9);
but.setOnClickListener(view -> {
but1.setOnClickListener(View -> but1.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but2.setOnClickListener(View -> but2.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but3.setOnClickListener(View -> but3.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but4.setOnClickListener(View -> but4.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but5.setOnClickListener(View -> but5.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but6.setOnClickListener(View -> but6.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but7.setOnClickListener(View -> but7.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but8.setOnClickListener(View -> but8.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
but9.setOnClickListener(View -> but9.setBackground(getDrawable(R.drawable.ic_baseline_panorama_fish_eye_24)));
});
butt.setOnClickListener(view -> {
but1.setOnClickListener(View -> but1.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but2.setOnClickListener(View -> but2.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but3.setOnClickListener(View -> but3.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but4.setOnClickListener(View -> but4.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but5.setOnClickListener(View -> but5.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but6.setOnClickListener(View -> but6.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but7.setOnClickListener(View -> but7.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but8.setOnClickListener(View -> but8.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
but9.setOnClickListener(View -> but9.setBackground(getDrawable(R.drawable.ic_baseline_clear_24)));
});
if(but1.getBackground() == getDrawable(R.drawable.ic_baseline_clear_24)){
mnm.setText("mmd");
}
}
}
and this is xml code:
<?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"
tools:context=".MainActivity">
<ImageButton
android:id="@ id/button1"
android:layout_width="116dp"
android:layout_height="160dp"
android:layout_marginStart="19dp"
android:layout_marginTop="94dp"
android:layout_marginEnd="19dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button4"
app:layout_constraintEnd_toStartOf="@ id/button3"
app:layout_constraintStart_toEndOf="@ id/button2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button2"
android:layout_width="112dp"
android:layout_height="160dp"
android:layout_marginStart="16dp"
android:layout_marginTop="94dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints,UnusedAttribute" />
<ImageButton
android:id="@ id/button3"
android:layout_width="113dp"
android:layout_height="160dp"
android:layout_marginTop="94dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="5dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button4"
android:layout_width="116dp"
android:layout_height="160dp"
android:layout_marginStart="19dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="19dp"
android:layout_marginBottom="10dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button7"
app:layout_constraintEnd_toStartOf="@ id/button6"
app:layout_constraintStart_toEndOf="@ id/button5"
app:layout_constraintTop_toBottomOf="@ id/button1"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button5"
android:layout_width="112dp"
android:layout_height="160dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button2"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button6"
android:layout_width="113dp"
android:layout_height="160dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toTopOf="@ id/button9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button3"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button7"
android:layout_width="116dp"
android:layout_height="160dp"
android:layout_marginStart="19dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="19dp"
android:layout_marginBottom="137dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@ id/button9"
app:layout_constraintStart_toEndOf="@ id/button8"
app:layout_constraintTop_toBottomOf="@ id/button4"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button8"
android:layout_width="112dp"
android:layout_height="160dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="137dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button5"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints" />
<ImageButton
android:id="@ id/button9"
android:layout_width="113dp"
android:layout_height="160dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="137dp"
android:background="@drawable/ic_rectangle_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/button6"
tools:ignore="ContentDescription,HardcodedText,MissingConstraints,UnusedAttribute" />
<Button
android:id="@ id/buttonn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="287dp"
android:layout_marginBottom="26dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="HardcodedText" />
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="287dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="26dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.875"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="@ id/textView"
android:layout_width="97dp"
android:layout_height="49dp"
android:text="TextView"
tools:ignore="HardcodedText,MissingConstraints"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="609dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
befor your answer, thans a lot
CodePudding user response:
Try to use tag function in view like
but1.setTag("image1")
then check with
boolean sameImage = but1.getTag().equals(but2.getTag());
CodePudding user response:
setting a shadow might work
or the below, but here am using edges with red color
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="1dp"/>
<stroke android:width="6dp" android:color="@color/red"/>
</shape>
or check this Android LinearLayout : Add border with shadow around a LinearLayout
Comparing images (KOTLIN)
if (languageFlag.drawable.constantState != getDrawable(R.drawable.ic_french_flag)?.constantState) {
languageFlag.setImageResource(R.drawable.ic_french_flag)
// Toast.makeText(
// this,
// "Vous devez redémarrer l'application après avoir modifié ce paramètre",
// Toast.LENGTH_LONG
// ).show()
}