The title says it all.
I am making an application in which i have to add dynamic buttons in a linear layout i have tried:
testButtons = new Button[caseDetails.length()];
for (int i = 0; i < caseDetails.length(); i++) {
temp = caseDetails.getJSONObject(i);
Log.e("TEMP " + i, temp.toString());
testButtons[i] = new Button(this) ;
testButtons[i].setText("Hello Hi");
testButtons[i].setHeight(LayoutParams.WRAP_CONTENT);
testButtons[i].setWidth(LayoutParams.WRAP_CONTENT);
testButtons[i].setPadding(20, 20, 20, 20);
testLayout.addView(testButtons[i]);
}
All i can see on emulator is two buttons with no text. Why is this happening?
had the same problem.. try this..
testButtons = new Button[caseDetails.length()];
for (int i = 0; i < caseDetails.length(); i++) {
temp = caseDetails.getJSONObject(i);
testButtons[i] = new Button(this) ;
testButtons[i].setText("Hello Hi");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
testButtons[i].setPadding(20, 20, 20, 20);
testLayout.addView(testButtons[i], lp);
}
also make sure your linearLayout's orientation is vertical. Good Luck! :)
Related
I working on a table feature right now and I don't know how to change the oriƫntation of a table.
This is my horizontal view, this is what I expect for the horizontal view:
And my code for the horizontal view.:
private void setupTable() {
setStretchAllColumns(true);
setBackground(borderDrawable(mTableBorderWidth));
setPadding(mTableBorderWidth, mTableBorderWidth, mTableBorderWidth, mTableBorderWidth);
for (int currentRow = 0; currentRow <= 1; currentRow++) {
tableRow = new TableRow(mContext);
if (currentRow == 0) {
for (int h = 0; h < tableData.field_options.options.size(); h++) {
textView = new TextView(mContext);
textView.setTextColor(mTextColor);
textView.setBackground(borderDrawable(mTextViewBorderWidth));
textView.setText(tableData.field_options.options.get(h).label);
textView.setGravity(Gravity.CENTER);
textView.setPadding(0, 6, 0, 6);
textView.setTypeface(Typeface.DEFAULT_BOLD);
tableRow.addView(textView);
}
} else {
for (int currentColumn = 0; currentColumn < tableData.field_options.options.size(); currentColumn++) {
editText = new EditText(mContext);
editText.setTextColor(mTextColor);
editText.setBackground(borderDrawable(mTextViewBorderWidth));
editText.setGravity(Gravity.CENTER);
editText.setPadding(0, 6, 0, 6);
tableRow.addView(editText);
}
}
addView(tableRow);
}
}
And this is my vertical view right now:
My code of the vertical table. I set the orientation here with Linear.Vertical but it doesn't help:
private void setupVerticalTable() {
setStretchAllColumns(true);
setOrientation(LinearLayout.VERTICAL);
setBackground(borderDrawable(mTableBorderWidth));
setPadding(mTableBorderWidth, mTableBorderWidth, mTableBorderWidth, mTableBorderWidth);
for (int h = 0; h < tableData.field_options.options.size(); h++) {
tableRow = new TableRow(mContext);
textView = new TextView(mContext);
textView.setTextColor(mTextColor);
textView.setBackground(borderDrawable(mTextViewBorderWidth));
textView.setText(tableData.field_options.options.get(h).label);
textView.setGravity(Gravity.CENTER);
textView.setPadding(0, 6, 0, 6);
textView.setTypeface(Typeface.DEFAULT_BOLD);
tableRow.addView(textView);
addView(tableRow);
}
for (int currentColumn = 0; currentColumn < tableData.field_options.options.size(); currentColumn++) {
editText = new EditText(mContext);
tableRow = new TableRow(mContext);
editText.setTextColor(mTextColor);
editText.setBackground(borderDrawable(mTextViewBorderWidth));
editText.setGravity(Gravity.CENTER);
editText.setPadding(0, 6, 0, 6);
tableRow.addView(editText);
addView(tableRow);
}
}
And what I want is this:
I hope someone can help me to solve this view. I don't know how to solve this problem. I tried a lot.
I think changing these 2 tables programmatically is your problem, this is of course not the wrong way but probably harder way. You can prepare 2 tables in your XML file. One table is for portrait mode, vertical table. Other one is for landscape mode, horizontal table. You can work much more easily with this way. You can check the phone orientation and make visible the desired table, make gone the other table runtime. Or you can prepare 2 separate layouts also for portrait mode and landscape mode also.
Im creating several buttons by using this function:
private void setlayout(Integer numbers_of_buttons){
// creating LinearLayout
LinearLayout linLayout = new LinearLayout(this);
// creating LayoutParams
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// set LinearLayout as a root element of the screen
setContentView(linLayout, linLayoutParam);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button[] butArray = new Button[numbers_of_buttons];
for (int i = 0; i < numbers_of_buttons; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText("i ... " + i);
butArray[i].setId(i); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, butArray[i -1].getId());
}
butArray[i].setOnClickListener(listener);
linLayout.addView(butArray[i], Btnparams);
}
}
Which is called when I click something in my NavigationDrawer and the parameter: numbers_of_buttons is retrieved by some query done in the sqlite database.
My only issues is that if numbers_of_buttons is > 7 i.e. (25) i can see only 7 of them.
This is because my scrren width is 720pixel (Nexus7).
So how am I suppose to handle this ?
My idea is
- calculate the widthness of the screen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
System.out.println("DisplayWidth -> " + metrics.widthPixels);
if getLeft() of last button create is > screenwidth than go to next row.
Can't find a way to do the second pseudocode.
EDIT - 1
private void setlayout(Integer numbers_of_buttons){
FlowLayout flowLayout = new FlowLayout(this);
setContentView(flowLayout);
Button[] butArray = new Button[numbers_of_buttons];
for (int i = 0; i < numbers_of_buttons; i++)
{
butArray[i] = new Button(this);
butArray[i].setText("i ... " + i);
butArray[i].setId(i);
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
butArray[i].setOnClickListener(listener);
flowLayout.addView(butArray[i]);
}
I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
parentLayout.addView(lLayout[i]);
}
You need to set LayoutParams, try adding this:
lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
It looks like you are trying to make a listview yourself using linearlayouts rather than actually using the ListView already supported.
If you are actually trying to do that, you should first give the layouts inside the view a width and height. I'd also put the list in a scrollview in case it overflows the outer layout.
Maybe try this
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = 0;
myLayoutParams.topMargin = 50 * i;
myLayoutParams.width = myScreenSize; //e.g. 480
myLayoutParams.height = 50;
lLayout[i].setLayoutParams(myLayoutParams);
parentLayout.addView(lLayout[i]);
}
I want to create 4 RelativeLayout dynamically so that each subsequent layout is placed below the previous one. I'm trying to do this whit this piece of code:
RelativeLayout layoutParent = (RelativeLayout)findViewById(R.id.layoutParent);
int layouts = 4;
int dp15 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(dp15, dp15, dp15, dp15);
for (int l = 0; l <= layouts; l++)
{
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
if (l != 0) params.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout);
}
But I can't get the desired position of each layout. Can someone tell me how can I do what I wanna do?
Thank you in advance!
You set the BELLOW rule but never use it when you add the child RelativeLayout to the parent layout(as MisterSquonk said). Also use another set of LayoutParams for the child RelativeLayout:
for (int l = 0; l <= layouts; l++) {
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (l != 0) lp.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout, lp);
}
I have putting such data into LinearView. as like below code:
public void showResult()
{
//db.open();
//c2 = db.getAllTitles(table_name);
List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
//for(int i = 0; i < c.getCount(); i++)
//int limit = tempEmployerList.size();
for(int i = 0; i<=tempEmployerList.size()-1; i++)
{
RelativeLayout innerLayout = new RelativeLayout(this);
innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
innerLayout.setBackgroundColor(0x00000000);
// set the Multiple TextView
TextView qWordTV = new TextView(getApplicationContext());
TextView aWordTV = new TextView(getApplicationContext());
TextView aImageView = new TextView(getApplicationContext());
qWordTV.setText("\n"+"1");
qWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
qWordTV.setTextColor(0xFFFF0000);
qWordTV.setPadding(3, 0, 0, 0);
aWordTV.setText("\n"+"2");
aWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
aWordTV.setGravity(Gravity.CENTER);
aWordTV.setTextColor(0xFFFF0000);
aWordTV.setGravity(Gravity.CENTER);
aWordTV.setPadding(40, 0, 0, 0);
aWordTV.setTextColor(0xFFFF0000);
aImageView.setText("\n"+"3");
aImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
aImageView.setGravity(Gravity.RIGHT);
aImageView.setTextColor(0xFF000000);
aImageView.setPadding(0, 0, 9, 0);
/**** Any other text view setup code ****/
innerLayout.addView(qWordTV);
innerLayout.addView(aWordTV);
innerLayout.addView(aImageView);
myLinearLayout.addView(innerLayout);
textListWord.add(qWordTV);
textListAnswer.add(aWordTV);
imageListAnswer.add(aImageView);
}
//while (limit==0);
}
Now i want to create the Simple View dynamicaly here. . .
As like:
<View android:layout_height="2dip" android:background="#FF000000" />
in xml.
Now how to make such View like line dynamicaly ?
Please help me in this.
Thanks.
View line = new View(getApplicationContext());
line.setBackgroundColor(0xFF000000);
// You may need to convert dip to px here, raw pixels are used in the example:
LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, 2);
line.setLayoutParams(layoutParams);