Button not responding when clicked - java

I am new to Android Programming and I wanted to make a simple app where the user would enter there name and after they click the button, it would say Hello (name of person). Also the button will change to thanks for clicking me after it is clicked. I have looked through the code but I am having a hard time finding the error. I have the code down below. I will also add the xml under the code.
code
public class MainActivity extends AppCompatActivity {
private EditText yourName;
private TextView outputName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourName = (EditText)findViewById(R.id.inputText);
outputName = (TextView)findViewById(R.id.outputText);
}
public void printHello (View view){
Button button =(Button) view;
((Button)view).setText("Thanks for Clicking Me!");
yourName =(EditText)findViewById(R.id.inputText);
outputName =(TextView)findViewById(R.id.outputText);
outputName.setText("Hello, "+ yourName.getText());
outputName.setVisibility(View.VISIBLE);
}
}
Here is the xml also
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="328dp"
android:text="Talk to Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />
<TextView
android:id="#+id/Label1"
android:layout_width="0dp"
android:layout_height="31dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text=" Please Enter Your Name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="88dp"
android:text="TextView"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputText"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="72dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Label1" />
</android.support.constraint.ConstraintLayout>

You need to have an android:onClick="printHello" inside of the button XML to link the button to the function inside of the Java class. Then any time the button is clicked, it calls printHello and does what you need to.

Related

How to set a textView to visible after an onClick event and Activity change?

I have three passages in my scrollview that need to each become visible after an onclick event on one of three buttons.
I have currently set them to all invisible. And since I cannot get it to work, I am only trying it out with one of the passages.
Because of this I created a private textview constant for only the first passage. But after I pass the intent to switch the activity, I also try to turn the view on that package to visible.
I have included my MainActivity.java and the xml file I used to set invisible.
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView passage1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passage1 = findViewById(R.id.passage_1);
}
public void launchPassageOne(View view) {
passage1.setVisibility(view.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageTwo(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageThree(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
}
<?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=".PassageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/passage_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage1"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage2"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage3"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My program just crashes. And I cannot find any error messages.
How can I make the packages visible whenever I want the activity to change?
There are three passages that I want to each become visible for the respective button, then turn invisible if the back button is pressed.
It seams the three views are in the started activity. And so you can't change their visibility because they haven't been created.
Add this before you start the activity
intent.putExtra("passageNum", 1)
Then call startActivity(intent)
In PassageAactivity onCreate do the following :
If (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1)
passage1.setVisibility(View.VISIBLE)
And so on for the other views
passage1.setVisibility(View.VISIBLE)
read more about views and how to modify their behavior here :
https://developer.android.com/reference/android/view/View
Use View.VISIBLE, capital V, it's a integer constant from the View class. Remove the View argument from the method launchPassageOne:
public void launchPassageOne() {
passage1.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
Image click hereWhatever I understood with your code I got that you are not initializing your methods in On create, whatever defined outside the On create will not be used until or unless called from inside On create method.
Designed some code may help you understanding in a better way.
In below code, I made text views scrollable, but you can only scroll if text is too long to fill the entire textview.
MainActivity.java
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit1;
EditText edit2;
EditText edit3;
Button btn1;
Button btn2;
Button btn3;
Button btnV;
Button btnI;
TextView t1;
TextView t2;
TextView t3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// defining id for edit texts
edit1=findViewById(R.id.passage_1);
edit2=findViewById(R.id.passage_2);
edit3=findViewById(R.id.passage_3);
// defining id for buttons
btn1=findViewById(R.id.button_1);
btn2=findViewById(R.id.button_2);
btn3=findViewById(R.id.button_3);
btnV=findViewById(R.id.btnvisi);
btnI=findViewById(R.id.btninvisi);
// defining id for text views
t1=findViewById(R.id.textview1);
t2=findViewById(R.id.textview2);
t3=findViewById(R.id.textview3);
// making text views scrollable
t1.setMovementMethod(new ScrollingMovementMethod());
t2.setMovementMethod(new ScrollingMovementMethod());
t3.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setText(edit1.getText().toString());
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t2.setText(edit2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t3.setText(edit3.getText().toString());
}
});
btnV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Making passages Visible
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
t3.setVisibility(View.VISIBLE);
}
});
btnI.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setVisibility(View.INVISIBLE);
t2.setVisibility(View.INVISIBLE);
t3.setVisibility(View.INVISIBLE);
}
});
}
}
Set activitymain.xml as below
<?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"
android:background="#2196F3"
tools:context=".MainActivity">
<EditText
android:id="#+id/passage_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="passage 1"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<EditText
android:id="#+id/passage_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 2"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.102" />
<EditText
android:id="#+id/passage_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 3"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.194" />
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_1"
app:layout_constraintTop_toTopOf="#+id/passage_1" />
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_2"
app:layout_constraintTop_toTopOf="#+id/passage_2" />
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_3"
app:layout_constraintTop_toTopOf="#+id/passage_3" />
<TextView
android:id="#+id/textview1"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 1"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.446"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview2"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 2"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview3"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 3"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.895"
tools:visibility="invisible" />
<Button
android:id="#+id/btnvisi"
android:layout_width="175dp"
android:layout_height="44dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Passage Visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.036"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/btninvisi"
android:layout_width="174dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="passage invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.886"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope it makes you understand in a better way,
Thanks

Change image on all ImageViews' by the help of getTag() in TicTacToe

I'm making a TicTacToe, in which there is a background of a grid and it contains nine ImageViews'.
There is a reset button at the bottom, on clicking it all the ImageViews' should disappear(setting them to white color).
This is the initial state when the app is launched, after clicking the reset button again this should appear
Here's the MainActivity.java:-
What to write in btnTap function which is set to be OnClick of reset button so that my purpose is fulfilled?
(any other logic other than getTag() will be appreciated)
package com.example.honey1.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
int active = 0;
int[] state = {2, 2, 2, 2, 2, 2, 2, 2, 2};
public void btnTap(View view)
{
for (int i = 0; i < 9; i++)
{
if(state[i]!=2)
{
//What to write here?
}
}
}
public void imgTap(View view) {
ImageView img = (ImageView) view;
int tapped = Integer.parseInt(img.getTag().toString());
if (state[tapped] == 2) {
if (active == 0) {
img.setImageResource(R.drawable.cross);
img.animate().setDuration(1000).rotation(90);
active = 1;
state[tapped]=0;
} else {
img.setImageResource(R.drawable.circle);
active = 0;;
state[tapped]=1;
}
} else {
Toast.makeText(getApplicationContext(), "Invalid Click!", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
XML file:-(This app contains only of one activity)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#drawable/background"
tools:context="com.example.honey1.tictactoe.MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.911"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#drawable/button"
android:onClick="btnTap"
android:text="#string/btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.553"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941"
tools:text="#string/btn" />
</LinearLayout>
<ImageView
android:id="#+id/h"
android:tag="0"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/f"
android:tag="1"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/b"
android:tag="2"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.899"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.223"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/i"
android:tag="3"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="8dp"
android:layout_marginStart="3dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.101"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/g"
android:tag="4"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/a"
android:tag="5"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.899"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.436"
app:srcCompat="#android:color/background_light"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="#+id/d"
android:tag="6"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.655"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/c"
android:tag="7"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.655"
app:srcCompat="#android:color/background_light" />
<ImageView
android:id="#+id/e"
android:tag="8"
android:layout_width="69dp"
android:layout_height="57dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:onClick="imgTap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.876"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.661"
app:srcCompat="#android:color/background_light"
android:layout_marginStart="8dp" />
You don't need to identify the ImageViews or even check which ones changed, just iterate through them and set them as needed. And don't forget to set all states to 2
for(int index=0; index<((ViewGroup)viewGroup).getChildCount(); ++index) {
View nextChild = ((ViewGroup)viewGroup).getChildAt(index);
if(nextChild instanceof ImageView){
//remove image
}
}

App stopped responding when accessing EditText [duplicate]

This question already has answers here:
What is a NumberFormatException and how can I fix it?
(9 answers)
Closed 5 years ago.
I'm new on Android Studio and Java. I want to get a value from an EditText and convert it to an Integer and set it to itself in onClickListener method, but it keeps stopped responding after I pressed buttons(buttons that are worked for decreasing and/or increasing a value on an EditText, as stated with btnInc and btnDec) when it debugged on my phone. I debugged on an emulator and it didn't worked either. I declared a final EditText and final Button in the protected void onCreate method after everything is created.
Here's my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnCalculate = findViewById(R.id.btn_calc);
final EditText edtPrime = findViewById(R.id.EditText_numMaxPrime);
final Button btnInc = findViewById(R.id.btn_incNum);
final Button btnDec = findViewById(R.id.btn_decNum);
btnDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer number = Integer.parseInt(edtPrime.getText().toString());
number--;
edtPrime.setText(number, TextView.BufferType.EDITABLE);
}
});
btnInc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer number = Integer.parseInt(edtPrime.getText().toString());
number++;
edtPrime.setText(number, TextView.BufferType.EDITABLE);
}
});
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Sieve(Integer.parseInt(edtPrime.getText().toString()));
}
});
}
I had imported android.widget.Button and android.widget.EditText.
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="karuntodroid.sieveoferatosthenes.MainActivity">
<TextView
android:id="#+id/TextView_askPrime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Enter the prime number up to:"
app:layout_constraintBottom_toBottomOf="#+id/EditText_numMaxPrime"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/EditText_numMaxPrime"
app:layout_constraintVertical_bias="0.032" />
<EditText
android:id="#+id/EditText_numMaxPrime"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="100"
android:inputType="number"
app:layout_constraintEnd_toStartOf="#+id/btn_incNum"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_incNum"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="+"
app:layout_constraintEnd_toStartOf="#+id/btn_decNum"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_decNum"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CALCULATE"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TextView_askPrime" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="407dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/TextView_numbersare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Numbers are:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/TextView_numbers"
android:layout_width="350dp"
android:layout_height="309dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="numbers"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TextView_numbersare" />
<Button
android:id="#+id/btn_numbercopy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Copy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btn_numsReset"
app:layout_constraintTop_toBottomOf="#+id/TextView_numbers" />
<Button
android:id="#+id/btn_numsReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Reset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent
app:layout_constraintTop_toBottomOf="#+id/TextView_numbers" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
EDIT:
Here I found an error on Logcat:
01-26 18:40:15.710 32310-32310/karuntodroid.sieveoferatosthenes E/AndroidRuntime: FATAL EXCEPTION: main
Process: karuntodroid.sieveoferatosthenes, PID: 32310
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at karuntodroid.sieveoferatosthenes.MainActivity$2.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:5638)
at android.view.View$PerformClick.run(View.java:22444)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:6139)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
01-26 18:40:15.713 1390-2874/? W/ActivityManager: Force finishing activity karuntodroid.sieveoferatosthenes/.MainActivity
Any help is appreciated. Thank you!
You are setting the text in your EditText via edtPrime.setText(number, TextView.BufferType.EDITABLE); where your number variable is Integer.
If you look at the docs, this will try to fetch a resource with the identifier (id) of that number. (and you dont have that resource with that id...those resources are accessed for ex. via R.string.my_string)
What you need to do is convert the number to string and set it via setText(..)

Resources$NotFoundException: String resource ID [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 5 years ago.
I try to show in MainActivity from another class by getter a value.
1st I make object and insert to him value from editText.
App crash with android.content.res.Resources$NotFoundException: String resource ID #0x28 What is wrong here ?
Check code:
public class MainActivity extends AppCompatActivity {
EditText editText, editText2;
Button button, button2;
TextView tV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
editText = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
tV = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int a = Integer.valueOf(editText.getText().toString());
Calculations calc = new Calculations(a){
};
tV.setText(calc.getResult());
}
});
}
2nd Class
public class Calculations {
public int getResult() {
return result;
}
public int result;
public Calculations(int a){
result = a * 10;
}
public Calculations(int a, int b){
result = a * b;
}
}
Here is my layout, maybe this will help
Here is my layout, maybe this will help
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="ge.ger.training.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.351" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.121"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1 and 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.668" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.109"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.838" />
</android.support.constraint.ConstraintLayout>
Replace your line with below in onClickListener
Calculations calc=new Calculations(a);
tV.setText(String.valueOf(calc.getResult()));

How to make a button reappear in different place when clicked?

I started to learn coding in Android studio few weeks ago. Now I'm trying to make an application which has 9 buttons, but you always see just one of them. When you click on visible button it should disappear and show another one and so on.
This is what I've made so far:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button shownButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shownButton = (Button) findViewById(R.id.b1);
shownButton.setVisibility(View.VISIBLE);
}
public void buttonDisappear(View view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(10 - 0) + 10;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}
}
In every button property "onClick" I set a function "buttonDisappear". Everything looks fine to me, no errors, but when I run the program and click on the first button (b1), application just stops. Can you tell me why it is happening and how can I fix it? Thank you.
UPDATE
activity_main.xml
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b9" />
<Button
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b7" />
<Button
android:id="#+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
you said you have only 9 buttons and the name starts from b1-b9
while you are generating a number that is always greater or equal to than 10
public void buttonDisappear(Button view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(9)+1;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}

Categories

Resources