My requirement is to apply '6' different colors to '6' ImageViews on programatically. In my Layout i can have more than six ImageViews and these colors has to be applied only to those specific ImageViews.
for (int i = 0; i < view.getChildCount(); i++) { }
But how do i identify those specific 6 ImageViews among all the views?
You can mark specific ImageViews with setTag() function (eg. imageView.setTag("Specific")) on their initialization and after that do next
for(i = 0; i < view.getChildCount(); i++){
//check for view is ImageView
if(view.getChildAt(i) instanceof ImageView){
//check for tag
if(((String) view.getChildAt(i).getTag()) == "Specific"){
//code for set color
}
}
}
Hope it helps
Related
I have been trying to hack a foreach to all elements of type label in screen or layout, with no luck!
My goal is to translate all screen1.labels.text, the translation are in list which has lists of pair (label.text, translation).
Is this possible in App-inventor?
Have you thought of getting an array list of sub view and iterating thought them. the below will work for direct children of the screen. if you need sub view as well you can use recursion
RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_screen_to_search);
for (int i = 0; i < layout.getChildCount(); i++)
{
View child = layout.getChildAt(i);
if(child instanceOf TextView)
{
txtView = (TextView)child;
if(txtView.getText().length!=0)
{
yourTranslateFunction(txtView.getText());
}
}
}
I have a LinearLayout which includes x amount of LinearLayouts (children) and a bunch of other views such as TextViews, Relative views etc..
How does one return the amount of children LinearLayout views only?
Normally you would use ViewGroup.getChildCount(), but since you want only the count of LinearLayouts you would need to create an algorithm to do so:
public int linearLayoutChildCount(ViewGroup parent) {
int count = 0;
for (int x = 0; x < parent.getChildCount(); x++) {
if (parent.getChildAt(x) instanceof LinearLayout) {
count++;
}
}
return count;
}
Of course, doing something like this is not ideal since it uses reflection which can be slow on Android.
I have a gridview called grid where I'm displaying random coloured circles that the user can change, each view inside the grid is tagged according to the colour of circle that is inside it. I have no problem getting the tag when the user clicks on a view but I'm now needing to be able to go through the whole grid and count how many of the circles are a certain colour, here is my code I have tried, this gives me a null pointer exception:
public void targCheck(){
targNum = 0;
int pos = 0;
for (int i = 0; i < gridSize; i++){
View v = grid.getChildAt(pos);
obj = (Integer) v.getTag(); //this is whats causing the problem
if (obj == target){
targNum += 1;
test.setText(targNum.toString());
}
pos += 1;
}
}
I think I might need to use the Image adapter to get the tags but I'm not sure how to use it in a method like this.
I have some dynamically declared ImageButtons, these ImageButtons don't have ids' and they are declared in a LinearLayout, I want to create a method to change Images resources of these ImageButtons when called, and as I don't have a predefined ids' for these ImageButtons I'm using the function getChildAt() with the LinearLayout, but getChildAt() doesn't provide setImageResource(), it just provide setBackgroundResource() and this function doesn't make a replacement to the old Image and also doesn't fit at the old Image as it provides a background not an Image Resource, what can I do with that ?
this is my method code :
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
Righter.getChildAt(i).setBackgroundResource(drawable); // here is where i change the background image
Righter.getChildAt(i).setClickable(true);
}
}
getChildAt() returns View. you need to typecast this View to ImageButton and call setImageResource() method...
ImageButton imageButton = (ImageButton) linearLayout.getChildAt(0);
imageButton.setImageResource(R.drawable.btnimage1);
try this
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
((ImageButton)Righter.getChildAt(i)).setImageResource(R.drawable.btnimage1);
Righter.getChildAt(i).setClickable(true);
}
}
type cast you Righter.getChildAt(i) with ImageButton
Working with apis, I am uncertain of the number of checkBoxes that will appear in the application. How can get the count of the number of checkBoxes produced in the application ?
Also, these checkBoxes are declared inside a listView.
You can loop though all the child views in you layout and find out how many of them are checkboxes. Looping and checking is done in following way:
ll is LinearLayout here
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
if (view instanceof CheckBox) {
count++;
}
}