How to put layout inside another layout? - android

I want to put a layout inside another layout but when I do this it gives NullPointerException on relLayout.addView(squareLayout); line
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
RelativeLayout relLayout = (RelativeLayout) findViewById(relIds[i][j]);
relLayout.removeAllViews();
RelativeLayout squareLayout = (RelativeLayout) findViewById(R.id.square);
relLayout.addView(squareLayout);
}
}
Please tell me what should I do?

relLayout is not null while accessing removeAllViews() method. So either findViewById() or another thread would have set null to relLayout instance.

improper use of relIds[][] without initialization could have also caused Null pointer exception

Related

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

Android: add an array of Switches in a LinearLayout

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

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

Android passing R.layout.extra_layout to View.inflate dynamically

I'm trying to programmatically pass the id of a layout.xml file to the View.inflate method but am not sure how I could go about that. I have a test bit of code that I'm tring to get working and, as you'll see I've tried passing this in as a String but that doesn't work.
Any ideas? My thanks.
LinearLayout main = (LinearLayout) findViewById(R.id.mainLayout);
for (int i = 0; i < 3; i++) {
String boxToInflate="R.layout.box"+Integer.toString(i);
LinearLayout ll = (LinearLayout) View.inflate(MainActivity.this, boxToInflate, null);
main.addView(ll);
}
Edit: A few minutes later.
I think I may be making some progress with this test code:
// Get outer relative layout
LinearLayout main = (LinearLayout) findViewById(R.id.mainLayout);
for (int i = 0; i < 3; i++) {
// String boxToInflate="R.layout.box"+Integer.toString(i);
int resID = getResources().getIdentifier("box" + i, "id",
getPackageName());
LinearLayout ll = (LinearLayout) View.inflate(MainActivity.this,
resID, null);
main.addView(ll);
}
int layoutId = getResources().getIdentifier("box"+i, "layout", getPackageName());
It also works for other things like drawables ("drawable" instead of "layout"), etc. Just replace the second parameter.
EDIT : Saw your edit, just replace "id" with "layout" and you're good.

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