This question already has an answer here:
App crashing at the end of array
(1 answer)
Closed 7 years ago.
final ImageView element = (ImageView) findViewById(R.id.element);
final ImageView name = (ImageView) findViewById(R.id.name);
Button nextButton = (Button) findViewById(R.id.buttonNext);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
element.setImageResource(image_elements[1]);
name.setImageResource(image_names[1]);
}
});
My code works when hard coded but I want to use operators like ++ and --. Why can't I? Example below.
element.setImageResource(image_elements[++]);
name.setImageResource(image_names[++]);
Why doesn't this work and how can I get it to.
If your goal is to move to the next element on each click, why not simply use an int index field and increment it?
nextButton.setOnClickListener(new View.OnClickListener() {
private int i = 0;
public void onClick(View v) {
// parallel array anti-pattern below is a no-no
element.setImageResource(image_elements[i]);
name.setImageResource(image_names[i]);
i++;
// if you want to cycle elements of the array and
// to prevent array index out of bounds:
i %= image_elements.length;
}
});
Related
This question already has answers here:
How to add button tint programmatically
(22 answers)
Closed 1 year ago.
I have dynamically created buttons. I just want change the background color of the button which I have clicked. For example, Initially all buttons should have grey background color. If I clicked a button then clicked button background color should changed to red and other buttons background color should be in grey.
Here I have tried a bit
for (int i = 0; i < 5; i++) {
Button myBtn = new Button(ProductDetailsActivity.this);
myBtn.setText("My Button"+i);
myBtn.setBackGroundColor(Color.parseColor("#cccccc"));
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myBtn.setBackGroundColor(Color.parseColor("#ff0000"));
}
});
}
Your code seems legit.
But instead of
myBtn.setBackGroundColor(Color.parseColor("#ff0000"));
You can use
myBtn.setBackGroundColor(Color.RED);
I think you want to change all buttons' colors dynamically according to which one is clicked. And want to turn others to gray.
So to achieve that you need to keep buttons in an array. Then by reaching others index you can modify them. You can get the index from the end of the buttonText String and parse it to integer.
Button[] buttonArray = new Button[4];
for (int i = 0; i < 5; i++) {
Button myBtn = new Button(ProductDetailsActivity.this);
myBtn.setText("My Button"+i);
myBtn.setBackGroundColor(Color.GREY);
myBtn.setId(new Random().nextInt());
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int j=0; j<buttonArray.lenght;j++){
if(view.getId() == buttonArray[j].getId()){
buttonArray[j].setBackgroundColor(Color.RED);
}else{
buttonArray[j].setBackgroundColor(Color.GREY);
}
}
}
});
buttonArray[i]=myBtn;
}
So the main idea is to keep all buttons in an array. If one of them clicked, get that one's index make it's background's red. And turn other buttons'(which is not that index) background grey again.
I'm working on a project in Android Studio where I have one EditText where the user will insert one word at a time, 10 times. Everytime the user writes a new input and clicks on the button it goes to a different TextView than the previous ones and different from the next ones.
How can I put the different inputs into the specific (different) TextViews?
Every TextView has a different sequencial ID like, word1, word2, etc.
I haven't done java in a long time, so I'm having problems with logic. I tried to do the following but the app crashes.
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
printwords(gameword.getText().toString());
}
});
public void printwords(String word) {
String[] array = new String[10];
TextView[] positions = new TextView[10];
for (int i=0; i < 10; i++){
array[i] = word;
positions[i].setText(array[i]);
}
}
}
You're creating a new array of TextView's and String every time the button is clicked. Change your code to the code below and it should work.
Make your int[] textViews , String[] array & int i = 0 class variables and then initialize them in onCreate() after setContentView()
In above code, int[] textViews is the array of ID's of TextView's from your activity.
After doing that, change your code to following:
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
array[i] = gameword.getText().toString();
YourActivity.this.findViewById(textViews[i]).setText(array[i]);
i++;
}
});
I have this function which is supposed to create an array of TextViews with unique ids.
Each TextView is supposed to perform a unique action, however when any one of them is clicked, they perform the function of the last TextView .
(ie, anyone of them appends a 9 to the last TextView the way this i set up) Do you know why it does this, and how I can fix it?
Any help would be greatly appreciated.
//Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_can_imake);
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i].setText("Title"+Integer.toString(i));
textViewArray[i].setPadding(8,8+50*i,8,0);
textViewArray[i].setId(i);
LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
textViewArray[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int myId = v.getId();
((TextView) v).append(Integer.toString(myId));
}
});
myLayout.addView(textViewArray[i],myTitleDimensions);
}
}
You are using different paddingTop to layout your TextViews vertically:
textViewArray[i].setPadding(8,8+50*i,8,0);
this makes the TextViews visually separate to each other, but in fact they are all overlapped, the 2nd one overlapped the 1st, the 3rd one overlapped the 2nd, etc. At last, the 9th one overlapped all, so no matter which text you clicked, you actually clicked the 9th one.
To fix this, you should change the way you layout the TextViews.
For example, use RelativeLayout.addRule(int verb, int anchor):
RelativeLayout.LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
myTitleDimensions.addRule(RelativeLayout.BELOW, i - 1);
}
By the way, 0 is not a valid id, so your 1st TextView will be still overlapped by the 2nd one, just change the way to generate ids a little.
The string answers_log is the result of a database query. So, when i loop trough the results, it generates 4 radio groups. When it try to set all radio groups enable, the radioGroup.getChildAt(j).setEnabled(false); is applied only to the last radio group. What can i do, to set all radio groups enable?
Here is my code:
radioGroup = new RadioGroup(this);
for (Answer an : answers) {
String answers_log = " " + an.getAnswer();
answer = new RadioButton(this);
answer.setText(answers_log);
radioGroup.addView(answer+log);
}
linearLayout.addView(radioGroup);
finishButton = new Button(this);
linearLayout.addView(finishButton);
finishButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < 4; i++) {
radioGroup.getChildAt(i).setEnabled(false);
}
}
});
Thanks!
Using a RadioGroup do not allow you to select multiple RadioButtons at same time. You should add 4 radio buttons without RadioGroup.
I have a textView set up on my main activity, and a button. When I click the button, I'd like the textView to start updating it's value based on the code below. However, this doesn't work and the problem is the loop. Can someone explain why? I am new to Java and Android Development
button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new OnClickListener() {
TextView textView = (TextView)findViewById(R.id.refView);
public void onClick(View arg0) {
for(i=1;i<1;i++){
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
}
});
Thank You
Try this:
TextView textView = (TextView)findViewById(R.id.refView);
button2.setOnClickListener(new View.OnClickListener() {
int i = 0;
public void onClick(View arg0) {
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
});
Your for loop conditions were wrong.
for(i=1;i<1;i++) won't even start, because 1<1 is already met.
Initiate count variable i before onClick and then update it before click and set new text with updated i.
Not sure what exactly you want to happen. But, you can get rid of this line
i = i + 1;
because the i++ already increments i by 1 with each iteration of the for loop.
Second, since i starts off at 1 and you want the loop to run while i<1, it will never enter the loop. It is never less than 1.
Third, if the conditions were different, say
for (int i=0; i<10; i++)
it will run through the loop so fast that you won't even recognize a change.