Java: Stuck inside if statement block - java

i am starting with creating android apps, i already have some basic experience with C# and Java. Now i'm stuck at a strange problem, see below:
public void pictureSwitch (View view){
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
boolean switched = false;
if (switched){
imageView.setImageResource(R.drawable.catstart);
Log.i("Status-Start", "Wert: " + switched);
switched = false;
} else {
imageView.setImageResource(R.drawable.catswitch);
Log.i("Status-switched", "Wert: " + switched);
switched = true;
}
}
}
The Problem is, on the first click on the Button it changes to the catswitch drawable. But it never switches back and i dont know why.
Thanks for your help in advance.

Lets have a boolean as instance variable.
private boolean switched = false;
public void pictureSwitch() {
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
if (switched) {
imageView.setImageResource(R.drawable.catstart);
} else {
imageView.setImageResource(R.drawable.catswitch);
}
switched = !switched;
}
And you do not need a parameter View view if you are not using it .

Related

How to compare images in Android (java)

I recently started learning to code with Android Studio. And I wanted a button when clicked to toggle between two images, but I cant seem to compare them.
I tried comparing them but it doesn't think they are the same.
ImageView dog = new ImageView(this);
dog.setImageResource(R.drawable.dog);
ImageView goofy = new ImageView(this);
goofy.setImageResource(R.drawable.goofydog);
if(img==dog)
{
img.setImageResource(R.drawable.goofydog);
}
else{
img.setImageResource(R.drawable.dog);
}
You can only compare primitive types (String, Int, Double...) in If statements, if you compare object it will compare the instance of each object which in your case is different.
If you want to toggle between two images based on what your imageView is actually displaying you should compare the Int resources that is displayed like that :
Image img = findViewById(R.id.image_view);
Button button = findViewById(R.id.button);
String current = "";
button.setOnClickListener(new View.OnClickListener( v-> {
#Override
void onClick(View v){
if (img.getDrawable() == ResourcesCompat.getDrawable(getResources(),
R.drawable.dog, null)) {
img.setImageResource(R.drawable.goofyDog);
} else {
img.setImageResource(R.drawable.dog);
}
}
}));
If you want to learn more about using objects in If statement, you can learn more about instance of object here : https://en.wikipedia.org/wiki/Instance_(computer_science)
You can't compare images the way you are doing. If you want to toggle between two images using a button, you can simple do:
Image img = findViewById(R.id.image_view);
Button button = findViewById(R.id.button);
String current = "";
button.setOnClickListener(new View.OnClickListener( v-> {
#Override
void onClick(View v){
if (current = "Dog") {
img.setImageResource(R.drawable.dog);
current = "Dog";
} else {
img.setImageResource(R.drawable.goofydog);
current = "Goofy";
}
}
}));

How to hide the imageview in RecyclerView permanently through firebase realtime database

This is my first time with recyclerview and I need some help. I am using a firebase and I want to hide an item(imageview) from my recyclerview adapter.
public RecyclerViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
image = itemView.findViewById(R.id.listrow_img1);
title = itemView.findViewById(R.id.listrow_tv1);
lock = itemView.findViewById(R.id.lock_accountImg);
}
I want to hide the lock image if my firebase realtime database don't have a child named("password"). if it has that child() the lock image will show up. Thank you in advance for those who help.
To hide or display a view according to a condition, create a method inside the ViewHolder class:
public void hideDisplayImageView(boolean passwordExist) {
if (passwordExist) {
lock.setVisibility(View.VISIBLE);
} else {
lock.setVisibility(View.GONE);
}
}
And call from within the onBindViewHolder method:
String password = list.get(position).getPassword();
if(password == null) {
holder.hideDisplayImageView(true);
} else {
holder.hideDisplayImageView(false);
}
You can try this like
if("your condition for the child is true"){
image.setVsisibility(View.VISIBLE)}
else{
image.setVsisibility(View.GONE)}
Kolin
val firebaseData = list.[position].getChild()
if(!firebaseData == "password")
holder.image.isVisible = false
else holder.image.isVisible = true
Java
String firebaseData = list.getPosition().getChild();
if(!firebaseData.contains("password")){
holder.image.setVisibility(View.GONE)
}else{
holder.image.setVisibility(View.VISIBLE)
}

Android Studio TextToSpeech run issue

I am basically making a firstaid training app (to help get people prepared for NL (National lifeguard) and Standard first aid).
This is done in a multiple choice form factor, and I have an activity which pulls all the questions, answers, and explanations and stores them in an array. As the user answers each question, it moves onto the next by replacing the text on the buttons and textview of the question, and I have made it so android says the question using TextToSpeech.
My error, is that for some reason on the first question, the string (question) gets given to the TextToSpeech talker, but it does not say it? What is weird is that for the other questions (2,3,4,etc.) it works, just not for the first.
I thought that maybe it was that it didnt have enough time to construct (which some other thread said was the problem when the TextToSpeech lags), so I added a delay to the button that starts the multiple choice activity and it still did not work for the first question, but works for the rest.
Here is my code for the TextToSpeech:
In the onCreate of the class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_menu);
prefs = getSharedPreferences("com.comsci.michelaustin.comscisummative", MODE_PRIVATE);
moduleNumber = getIntent().getIntExtra("MODULE_ID", 0);
talker = new TextToSpeech(QuizMenuActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
result = talker.setLanguage(Locale.UK);
}
else
{
Toast.makeText(getApplicationContext(), "Talking feature not supported on this device", Toast.LENGTH_SHORT).show();
}
}
});
In the method that sets the textview and buttons to the correct question and answers:
private void displayQuestions(){
String ques = mQuestionLibraryTest.getQuestion(questionNumber);
questionLabel.setText(ques);
talker.speak(ques,TextToSpeech.QUEUE_FLUSH,null);
option1.setText(getChoice(questionNumber,0));
option2.setText(getChoice(questionNumber,1));
option1.setEnabled(true);
option2.setEnabled(true);
option3.setEnabled(true);
option4.setEnabled(true);
option1.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option2.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option3.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option4.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
//to test specifically for true and false questions and to remove visibility of the last two buttons
if (testWhetherBlank(2)) {
option3.setVisibility(View.GONE);
}
else{
option3.setVisibility(View.VISIBLE);
option3.setText(getChoice(questionNumber,2));
}
if (testWhetherBlank(3)) {
option4.setVisibility(View.GONE);
}
else{
option4.setVisibility(View.VISIBLE);
option4.setText(getChoice(questionNumber,3));
}
//
//answerArray=(ArrayList<Object>)mQuestionLibrary.getCorrectAnswer(questionNumber).clone();
answerArray = (ArrayList<String>) mQuestionLibraryTest.getCorrectAnswer(questionNumber).clone();
//Collections.copy(mQuestionLibraryTest.getCorrectAnswer(questionNumber), answerArray);
//explanation=mQuestionLibrary.getExplanation(questionNumber);
explanation = mQuestionLibraryTest.getExplanation(questionNumber);
amountCorrect=mQuestionLibraryTest.getAnswerAmount(questionNumber);
nextArrowButton.setVisibility(View.GONE);
writeResume();
}
//allows the UI to test and remove buttons if the options are blank
public boolean testWhetherBlank(int q){
/*if(mQuestionLibrary.getChoice(questionNumber,q).equals("")){
return true;
}*/
if(mQuestionLibraryTest.getChoice(questionNumber,q).equals("")){
return true;
}
else{
return false;
}
}
Any help is greatly appreciated!

toggle button state depend on feedback from server

Arduino server and android client i want to make toggle button state depend on string feedback from server
i have tried this but does not work
this code below was woeking week ago i didnt change anything but now i got the problem
Arduino:
if (packetBuffer[0]=='3')
{
if (buttonstate == HIGH)
{
Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
Udp.write("on");
Udp.endPacket();
}
else
{
Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
Udp.write("off");
Udp.endPacket();
}
}
Android:
coming = new String(recd.getData());
Toast.makeText(getApplicationContext(),coming,Toast.LENGTH_LONG).show();
textView = (TextView)findViewById(R.id.textView);
textView.setText(coming);
if (coming=="on")
{
apf.setChecked(true);
}
else if (coming=="off")
{
apf.setChecked(false);
}
d1.close();

How can I show a button when all views are viewed in viewpager?

Right now I have a save button that I want to show only if all the views inside a viewpager are shown. That means that when the user swipes between views and have seen all views inside a viewpager, then show a save button.
I want to show the save button on every view when they have seen all views hereby after.
The trouble I am having is how to set up the logic. I started out with setting the save button invisible until the last view of the viewpager. On the last view of the viewpager, show the save button. But the problem is when the user goes to the last view (there's a save button) and then goes back to a previous view, the save button is gone.
So, I was wondering how can I show the save button permanently on all views after the user has seen all views?
Here's what I have so far:
I have this snippet inside my InstantiateItem() :
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
System.out.println("Is this called? isViewed = true");
}else if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
where
#Override
public void onPageSelected(int position) {
isViewed = true;
}
EDIT:
I tried the following solutions but with no luck.
Button save_button = (Button) findViewById(R.id.save);
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.length-1 && !isViewed[position]) {
isViewed[position] = true;
save_button.setVisibility(Button.VISIBLE);
}
isViewed[position] =true;
And
isViewed[position] = true;
if (isViewed[position] == isViewed[numberOfPages.length-1]) {
save_button.setVisibility(Button.VISIBLE);
}
if (isViewed[position]) {
save_button.setVisibility(Button.VISIBLE);
} else {
save_button.setVisibility(Button.INVISIBLE);
}
In your onPageSelected, do the following
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
Note the above are two seperate if statements.
Make your isViewed global and default to false.
boolean []isViewed = new boolean[noOfPages.size()];
#Override
public void onPageSelected(int position) {
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
else {
save_button.setVisibility(Button.GONE);
}
isViewed[position] = true;
}

Categories

Resources