Paint region of cells Sudoku - android

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));
}
}

Related

How to generate large seat layout with 1200 seats?

I am making an app where i need to display a seat layout containing 900 to 1200 seats. I had managed to generate a 900 seat layout by creating LinearLayouts with customized TextViews programatically by looping. But I'm pretty sure that is definitely not the way to do it.
Here is what I was doing:
// Seat Layout
LinearLayout linearLayoutSeatContainer;
static final LinearLayout[] rows = new LinearLayout[30];
static final TextView[][] seats = new TextView[30][30];
static final View[][] spaceVertical = new View[30][4];
static final View[] spaceHorizontal = new View[4];
// Populating the screen with seats
for (int i = 0; i < 30; i++) {
rows[i] = new LinearLayout(this);
rows[i].setOrientation(LinearLayout.HORIZONTAL);
// Each loop creates a row of seats
for (int j = 0; j < 30; j++) {
// To add horizontal space after every 10 seats
if (j % 10 == 0) {
spaceVertical[i][(j / 10)] = new View(this);
spaceVertical[i][(j / 10)].setLayoutParams(spaceVerticalParams);
rows[i].addView(spaceVertical[i][(j / 10)]);
}
seats[i][j] = new TextView(this);
seats[i][j].setId(i * 100 + j);
seats[i][j].setLayoutParams(textViewParams);
seats[i][j].setBackgroundResource(R.drawable.seat_circle_empty);
seats[i][j].setClickable(true);
seats[i][j].setOnClickListener(this);
// TODO: Delete randomized code for disabled seats
Random rand = new Random();
int num = rand.nextInt(20);
if (num % 5 == 0 && (i != 0 && j != 0))
seats[i][j].setEnabled(false);
rows[i].addView(seats[i][j]);
}
// Adding the last vertical space between screen and the last seat
spaceVertical[i][3] = new View(this);
spaceVertical[i][3].setLayoutParams(spaceVerticalParams);
rows[i].addView(spaceVertical[i][3]);
// To add horizontal space after every 10 rows of seats
if (i % 10 == 0) {
spaceHorizontal[i / 10] = new View(this);
spaceHorizontal[i / 10].setLayoutParams(spaceHorizontalParams);
linearLayoutSeatContainer.addView(spaceHorizontal[i / 10]);
}
linearLayoutSeatContainer.addView(rows[i]);
}
// Adding the last horizontal space between screen and the last row
spaceHorizontal[3] = new View(this);
spaceHorizontal[3].setLayoutParams(spaceHorizontalParams);
linearLayoutSeatContainer.addView(spaceHorizontal[3]);
Please ignore that bit with the randomization, that was for testing purposes. I know this is not the way of doing things. Please point me towards the right direction.

How do I display an unknown number of RelativeLayouts?

I have an activity that should display X RelativeLayouts, where X is the length of a JSON array. How can I do this?
So far, I have tried this:
RelativeLayout[] interestLayouts = new RelativeLayout[jarr.length()];
for (int interest = 0; interest < jarr.length(); interest++) {
interestLayouts[interest] = (RelativeLayout) view.findViewById(R.id.X); // I don't know how to determine X
}
You can add the Relative Layout dynamically.
Linear LAYOUT; // suppose this is the layout where you want to add relativelayout
for (int i =0;i < jsonarray.length; i++){
Relative rLayout = new RelativeLayout(this);
LAYOUT.addView(rLayout);
}
something like this

Memory issue on creating dynamic view inside RecyclerView

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);
}
}

Counting visible elements in a layout

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
}

Get all TableRow's in a TableLayout

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);
}
}

Categories

Resources