Currently working in Android Studio and running into a little trouble. Trying to get my rollScore button to roll over and over. At this point it "rolls" once and stops. I've tried a for loop and while loop and unable to get it to allow multiple "rolls".
public class GameScreen extends AppCompatActivity {
private Button rollButton; // Roll button being declared as a variable
private TextView rollScore; // Text view being declared as a variable
private TextView totalSCore; //
private int mCounter = 0;
private int totalRuns = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Random randomGenerator = new Random();
final int randomInt = randomGenerator.nextInt(7) + 1;
rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rollScore.setText(Integer.toString(randomInt));
mCounter++;
}
});
}
}
You have to generate a new random number on each click on your button.
Like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Random randomGenerator = new Random();
int randomInt;
rollButton = (Button) findViewById(R.id.rollButton);
rollScore = (TextView) findViewById(R.id.rollScore);
totalSCore = (TextView) findViewById(R.id.totalScore);
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
randomInt = randomGenerator.nextInt(7) + 1;
rollScore.setText(Integer.toString(randomInt));
mCounter++;
}
});
}
You need to generate a new random number after every roll e.g.
rollScore.setText(Integer.toString(randomGenerator.nextInt(7) + 1));
i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
I want to print A,B,C..... in series, but when I click the button it's Unfortunately stopped,
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView alphabets = (TextView) findViewById(R.id.alphabet);
Button next = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
};
next.setOnClickListener(listener);
the reason is an IndexOutOfBoundException here:
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
change it to
for (int i=0;i<alpha.length;i++)
alphabets.setText(alpha[i]);
}
with your code you try to enter the last element +1 and that element does not exists.
btw. your textview will only display the last element!
EDIT:
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
StringBuffer alphabet = new StringBuffer();
for (int i=0;i<alpha.length;i++){
alphabet.append(alpha[i]);
}
alphabets.setText(alphabet.toString());
Try this
String alpha[30];
alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
I try to create dynamic buttons. When a button is clicked, the color of the button will change to red. When another one is clicked, the color of the previous button should be reset to the default color.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linear;
linear = (LinearLayout) findViewById(R.id.ly);
for (i = 1; i < 4; i++) {
final Button btn = new Button(this);
btn.setId(1000 + i);
btn.setBackgroundColor(Color.BLUE);
btn.setMinimumHeight(150);
btn.setMinimumWidth(150);
linear.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
btn.setBackgroundColor(Color.RED);
}
});
}
How can I get the id of the non clicked button?
You can try this:
ArrayList<Button> mButtonList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linear;
linear = (LinearLayout) findViewById(R.id.ly);
for (int i = 1; i < 4; i++) {
final Button btn = new Button(this);
btn.setId(1000 + i);
btn.setBackgroundColor(Color.BLUE);
btn.setMinimumHeight(150);
btn.setMinimumWidth(150);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (Button button : mButtonList) {
if (button.getId() == view.getId()) {
button.setBackgroundColor(Color.RED);
} else {
button.setBackgroundColor(Color.BLUE);
}
}
}
});
linear.addView(btn);
mButtonList.add(btn);
}
}
Add implements onClickListener to Your Activity and set this listener to you button in for loop like
valueB.setOnClickListener(this);
And Override the onClick method where you get button id
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "" + v.getId(), 800).show();
}
Once you get button id you can change text color
The following are my textfields, radio buttons etc.:
et1 = Edit Text Field
radioButton1 = Radio Button (part of group1)
radioButton2 = Radio Button (part of group1)
textView2 = Text View
What I am trying to is to constantly have textView2 update depending on what is inside et1 and which Radio Button is selected.
I was thinking the problem could be to do that the method convertUp / convertDown. It only runs when a Radio Button is actually clicked, and not selected.
If this is the problem can someone tell me how to do this. And if it is not, can someone explain to me where I am going wrong. I would be very grateful. Thanks in advance.
Here is my code I use:
("mark" is the variable that will be updating in textView2 and "value" is the variable that "mark" depends on - as seen in the methods)
public class MainActivity extends ActionBarActivity {
double mark = 0;
double value = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText text = (EditText)findViewById(R.id.et1);
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
OnClickListener listener1 = new OnClickListener(){
public void onClick(View v) {
mark = convertUp(value);
}
};
OnClickListener listener2 = new OnClickListener(){
public void onClick(View v) {
mark = convertDown(value);
}
};
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener2);
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(""+mark);
}
You would need to do several things to have it constantly updating.
First, you need a TextWatcher for the EditText which would modify the textView every time you input a new letter.
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
if(rb1.isChecked()){ mark = convertUp(value); }
else{ mark = convertDown(value); }
tv1.setText(""+mark); //will update ur tV every time you input a lleter
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) { }
});
You also need to do this to modify your textView in case you change the radioButton
OnClickListener listener1 = new OnClickListener(){
public void onClick(View v) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
mark = convertUp(value);
tv1.setText(""+mark);
}
};
OnClickListener listener2 = new OnClickListener(){
public void onClick(View v) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
mark = convertDown(value);
tv1.setText(""+mark);
}
};
Hope it helps! Good Luck!
Or try tv1.setText(""+mark); in OnClickListener listener1 and OnClickListener listener2