Suppose in my LinearLayout (say parentLayout) there are 5 other LinearLayouts (say childLayout), where only one of them are visible at the moment. The other layouts depend on some external event to make them visible. How do I count the number of childLayout in the parentLayout that are visible ?
You can iterate over the children of the parent layout and check their visibility. Something like this:
LinearLaout parent = ...;
int childCount = parent.getChildCount();
int count = 0;
for(int i = 0; i < childCount; i++) {
if(parent.getChildAt(i).getVisibility() == View.VISIBLE) {
count++;
}
}
System.out.println("Visible children: " + count);
here is a funntion that returns number of visible childs in ViewGroup like LinearLayout, RelativeLayout, ScrollView, ..etc
private int countVisible(ViewGroup myLayout)
{
if(myLayout==null) return 0;
int count = 0;
for(int i=0;i<myLayout.getChildCount();i++)
{
if(myLayout.getChildAt(i).getVisibility()==View.VISIBLE)
count++;
}
return count;
}
If you are using Kotlin just use extention:
fun LinearLayout.visibleChildCount(): Int {
var childVisible = 0
children.iterator().forEach {
if(it.isVisible) ++childVisible
}
return childVisible
}
Related
I want to paint the square to which a sudoku number selected belongs.
This code is for print the rows and columns :
//Pintem la fila del nombre seleccionat
for (int i = 0; i < parent.getChildCount(); i++) {
TextView child = (TextView) parent.getChildAt(i);
if ((i/9)==x) {
//child.setBackgroundColor(Color.parseColor("#75FFEE"));
child.setBackground(getDrawable(R.drawable.contornfonsblau));
}
}
//Pintem la columna del nombre seleccionat
for (int i = 0; i < parent.getChildCount(); i++) {
TextView child = (TextView) parent.getChildAt(i);
if ((i%9)==y) {
//child.setBackgroundColor(Color.parseColor("#75FFEE"));
child.setBackground(getDrawable(R.drawable.contornfonsblau));
}
}
I wanted the same but for the square.
EXAMPLE
You can find which large square a number belongs to by dividing the x and y co-ordinates of the small square by 3 using integer division, since the large squares are 3x3 small squares.
So, do the same comparisons as above, but divide both sides by 3 beforehand. You also need to check both x and y:
for (int i = 0; i < parent.getChildCount(); i++) {
TextView child = (TextView) parent.getChildAt(i);
if ((((i/9)/3)==(x/3)) && (((i%9)/3)==(y/3))) {
child.setBackground(getDrawable(R.drawable.contornfonsblau));
}
}
Of course, (i/9)/3 could be simplified to i/27, but it's debateable whether that is more readable.
You can combine all 3 tests into a single for loop:
for (int i = 0; i < parent.getChildCount(); i++) {
TextView child = (TextView) parent.getChildAt(i);
if(((i/9)==x) ||
((i%9)==y) ||
((((i/9)/3)==(x/3)) && (((i%9)/3)==(y/3)))) {
child.setBackground(getDrawable(R.drawable.contornfonsblau));
}
}
When I use Java code to dynamically add a button to an Android layout I first have to declare a local copy of the button similar to this:
Button btn = new Button(this);
Then there is some typical code as follows (but varies with the application):
// sets button width and height
RelativeLayout.LayoutParams bparms = new RelativeLayout.LayoutParams(w, h);
// sets button left and top position inside layout
bparms.setMargins(l, t, 0, 0);
This may be followed by setting other button properties such as text, background etc. Finally the button gets added to the parent layout as follows:
// add the dynamic button to the keypad view
kpad.addView(btn, bparms);
Here is my question. Does the layout receiving the button make a copy of the locally created dynamic button? Or does it just take a reference to the that created button leaving the originally created object intact?
Looking at the source code, there is an addInArray method of ViewGroup class, and it just takes a reference of the child view and adds it to children array:
private void addInArray(View child, int index) {
View[] children = mChildren;
final int count = mChildrenCount;
final int size = children.length;
if (index == count) {
if (size == count) {
mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
System.arraycopy(children, 0, mChildren, 0, size);
children = mChildren;
}
children[mChildrenCount++] = child;
} else if (index < count) {
if (size == count) {
mChildren = new View[size + ARRAY_CAPACITY_INCREMENT];
System.arraycopy(children, 0, mChildren, 0, index);
System.arraycopy(children, index, mChildren, index + 1, count - index);
children = mChildren;
} else {
System.arraycopy(children, index, children, index + 1, count - index);
}
children[index] = child;
mChildrenCount++;
if (mLastTouchDownIndex >= index) {
mLastTouchDownIndex++;
}
} else {
throw new IndexOutOfBoundsException("index=" + index + " count=" + count);
}
}
Generally, java is working with references. So your object will not be cloned, you just give a reference of the object to the layout. Also, whenever you receive the object, e.g. by iteration over all views in the layout programmatically, you will also only receive a reference to the object. Cloning may be possible, when you e.g. assign a new variable (with a new memory position).
I want to add some switches in a linear layout (declared as a view, not as a LinearLayout). I tried this, but it gets me an error:
numberDevices = 3; //This is going to be used after
Switch[] switches = new Switch[numberDevices];
for (int i = 0; i < numberDevices; i++) {
switches[i].setTextOn("ON");
switches[i].setTextOff("OFF");
switches[i].setId(i);
((LinearLayout) linearLayout).addView(switches[i]);
}
Any idea?
You haven't created any Switches, you've just made an empty array. You need to create the switches first:
for (int i = 0; i < numberDevices; i++) {
switches[i] = new Switch(linearLayout.getContext());
...
}
I am creating UI layout dynamically inside the RecyclerView which causes out of memory issue when the application is used for long time.Ui being re created on scrolling up and down. How can i prevent the creation of views on scrolling? my code looks as follows.
LinearLayout container = (LinearLayout) getBaseView().findViewById(R.id.container);
container.removeAllViews();
int position = 0;
for (int i= 0; i<content.size()/2;i ++) {
LinearLayout ln = new LinearLayout(getBaseView().getContext());
ln.setWeightSum(2);
ln.setGravity(Gravity.CENTER);
for (int j = 0; j<COLUMN_COUNT; j++) {
VIOPlayListItemView tile = new VIOPlayListItemView(getBaseView().getContext());
tile.setTag(position);
tile.setGravity(Gravity.CENTER);
ln.addView(tile);
if (position <content.size()) {
tile.setData(content.get(position));
}
position ++;
}
container.addView(ln);
}
}
I've been looking for hours on how to get all TableRow's in a TableLayout. I already know how to add and delete rows dynamically, but I need to loop over all the rows and selectively delete some of them.
I think I can come up with a work around, but I'm trying to avoid crude hacks in my app.
Have you tried using getChildCount() and getChildAt(int) respectively?
Should be fairly easy in a loop:
for(int i = 0, j = table.getChildCount(); i < j; i++) {
View view = table.getChildAt(i);
if (view instanceof TableRow) {
// then, you can remove the the row you want...
// for instance...
TableRow row = (TableRow) view;
if( something you want to check ) {
table.removeViewAt(i);
// or...
table.removeView(row);
}
}
}
if you have other type view
TableLayout layout = (TableLayout) findViewById(R.id.IdTable);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof TableRow) {
TableRow row = (TableRow) child;
for (int x = 0; x < row.getChildCount(); x++) {
View view = row.getChildAt(x);
view.setEnabled(false);
}
}
}
I have been looking for the same thing for a while. For me, I was looking to how to check views in my table layout. I did this by:
//This will iterate through your table layout and get the total amount of cells.
for(int i = 0; i < table.getChildCount(); i++)
{
//Remember that .getChildAt() method returns a View, so you would have to cast a specific control.
TableRow row = (TableRow) table.getChildAt(i);
//This will iterate through the table row.
for(int j = 0; j < row.getChildCount(); j++)
{
Button btn = (Button) row.getChildAt(j);
//Do what you need to do.
}
}
If you try to remove TableRows the ID Counter will decrease so pls use this loop for removing ... else if you dont want to remove you can use some of the loops above :D
TableLayout table = (TableLayout) findViewById(R.id.tblScores);
for(int i = 0; i < table.getChildCount(); i = 0)
{
View child = table.getChildAt(0);
if (child != null && child instanceof TableRow)
{
TableRow row = (TableRow) child;
row.removeAllViews();
table.removeViewAt(i);
}
}
This clears all rows!
I think your main problem is if you remove rows that all rows will be newly placed so that the row with the ID = 5 is now ID = 4 if you delete the row with ID = 3
so maybe you have to reset the counter and iterate again or you generate the table new after you cleared all rows
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = (TableRow) table.getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}