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.
Related
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
Which will be the better way to create a Vertical Lineal layout with four or more buttons in each row
The problems I have face are the following:
Setting the id of each button manually will result in a lot of repetitive code, more resources usage and you will have to change everyone to add a feature or change something (I think using an adapter will be the most efficient way, but...)
From what I know using a CustomAdapter don't help you set a unique ID to the buttons
Can you use an adapter to set a different id for each button dipending of the row?
Example:
second button of third row: r3b2
fifth button of first row: r1b5
Thanks.
You can create the buttons programmatically in our Activity class like this
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); // the layout in which u want to display the buttons
layout.setOrientation(LinearLayout.VERTICAL);
int count = 1;
for (int i = 0; i < 5; i++) { // i = row count
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 7; j++) { // j = column count (create 7 buttons in each row)
int id = count;
final Button btnTag = new Button(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
btnTag.setLayoutParams(params);
btnTag.setBackgroundColor(Color.parseColor("#ffffff"));
String dateSuffix = count + getDayNumberSuffix(count);
btnTag.setTag(dateSuffix);
btnTag.setId(id);
btnTag.setText(count + "");
btnTag.setTextSize(12.0f);
btnTag.setOnClickListener(getOnClickDoSomething(btnTag, count));
row.addView(btnTag);
count++;
}
layout.addView(row);
}
View.OnClickListener getOnClickDoSomething(final Button btnTag, final int count) {
return new View.OnClickListener() {
public void onClick(View v) {
// here you can do what u want to do on button click
}
}
How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.
I have the following code:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
all the images are show vertically i want it to be horizontal
Try this code:
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
lyt.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
Avoid setting attributes in your Java code when it can be done via XML. Try setting the orientation attribute in your ListView:
<LinearLayout
...
android:orientation="horizontal"
...>
</LinearLayout>
Try it like that:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
LinearLayout buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++)
{
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
lyt.addView(buttonsLinearLayout);
That way, you still have your main vertical linearlayout, so you can put stuff under the buttons.
EDIT:
to use it for more than 1 row, i did a simple math calc...
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
// calculate the number of rows needed
int numOfButtons = something you already have i guess;
// row layout
LinearLayout buttonsLinearLayout = new LinearLayout(context);
;
for (i = 0; i <= numOfButtons; i++) {
// for every 3 rows, create a new layout
// and add it to the main linear layout
if (i % 3 == 0) {
// create layout for a 3 button row
buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add the new row with 3 buttons to the main lineal layout
lyt.addView(buttonsLinearLayout);
}
ImageButton ib = new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image, null, getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
}
not tested, let me know if that worked for you...
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