Place Checkboxes next to each other dynamically Android - android

I want to place Check-boxes next to each other dynamically in android and when the width is over then the check boxes need to be aligned from the next line.
I have number of check-boxes which are initializing in an array. I have used the following code. It correctly place only one check-box. Other one gets placed in between. I have used the following code.
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout_symptoms_checkboxes);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);;
params.setMargins(0, 10, 0, 0);
for(int i = 0; i < arraySymptoms.length; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(arraySymptoms[i]);
cb.setId(i);
if(i==0)
{
cb.setLayoutParams(params);
layout.addView(cb);
}
else
{
params.addRule(RelativeLayout.ALIGN_RIGHT, i-1);
cb.setLayoutParams(params);
layout.addView(cb);
}
}
Please guide me through this. Your help will be greatly appreciated. Thank you in advance people :)

Just use LinearLayout with horizontal orientation instead of RelativeLayout

Related

Android : TextView set dynamically with RIGHT_OF

I need to set TextView dynamically, I have a word, and I want to create as TextView as there are letter in the word. So I put the first TextView and then each TextView on the right of the last one created. This is my code :
letters = new TextView[Anagrame.length()];
for (int i = 0; i < Anagrame.length(); i++)
{
TextView letter = new TextView(getApplicationContext());
letter.setBackgroundResource(R.drawable.square);
letter.setText(String.valueOf(Anagrame.charAt(i)));
letter.setGravity(Gravity.CENTER);
letter.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
if (i > 0)
{
Log.d("I ", Integer.toString(i));
Log.d("ID", Integer.toString(letters[i - 1].getId()));
params.addRule(RelativeLayout.RIGHT_OF, letters[i - 1].getId());
}
letter.setLayoutParams(params);
letters[i] = letter;
anagrameLayout.addView(letter);
}
Everything is fine except one thing the last TextView is not on the Right Position explanation :
The last TextView is on the same place than the first TextView, but when I checked in loop everything look fine, when I check log, the counter "i" and the id is good so I don't see the reason why the last one is not on the right of previous one but on the same place of the first one.
So if anyone have an idea, thanks in advance.

android: how to fill a relativelayout ( screen size) with many textviews created dynamically?

I tried to use below code, but the textviews do not change line after it reach the end of screen.
RelativeLayout ll = (RelativeLayout)findViewById(R.id.viewObj);
int lastid=0;
for (int i=0; i<100; i++) {
String teststr = " hello ";
TextView textView2 = new TextView(this);
textView2.setId(i+1);
textView2.setTextSize(30);
textView2.setBackgroundColor(Color.GREEN);
textView2.setTextColor(Color.RED);
textView2.setTypeface(Typeface.MONOSPACE);
textView2.setText(teststr+String.valueOf(i));
if (i>0)
{
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.RIGHT_OF, lastid);
ll.addView(textView2, lay);
} else {
ll.addView(textView2);
};
lastid = textView2.getId();
}
However, I dont know how to determine when to change line. I just keep on putting new textview to the right of the last one created. Can anyone tell me the correct logic to do the task? many thanks.
Easy fix. Switch to a dynamically-created LinearLayout, and set its orientation to vertical:
lay.setOrientation(LinearLayout.VERTICAL);

Dynamic button display programmatically Android

I want to add buttons programmatically on the screen and I am getting the value by parsing an API and now I want to display the buttons according to the length of an array. I am doing this but I am only getting the last button displayed, but inside the for loop I'm getting all values correct but displaying only the last button. This is my code:
RelativeLayout relate;
//...
relate = (RelativeLayout)findViewById(R.id.relative);
protected void onPostExecute(Void result) {
if(dialog.isShowing() == true) {
dialog.dismiss();
}
//int width = 100, height =50, x = 10, y = 20;
for (int i =0;i<adapt_obj.city_name_array.length;i++){
b1 = new Button(myref);
b1.setText(adapt_obj.city_name_array[i]);
relate.addView(b1);
//relate.addView(b1, i, new RelativeLayout.LayoutParams(width,height));
//height = height+80;
}
listlocation.setAdapter(adapt_obj);
adapt_obj.notifyDataSetChanged();
}
A RelativeLayout will stack the views you add to it at the top-let corner if you don't specify some placement rules. Your buttons are added to the layout but they are placed one on top of each other and so the only visible is the last one you add. Here are some modification of your for loop:
RelativeLayout relate; relate = (RelativeLayout)findViewById(R.id.relative);
for (int i = 0; i < adapt_obj.city_name_array.length; i++){
Button b1 = new Button(myref);
b1.setId(100 + i);
b1.setText(adapt_obj.city_name_array[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.addRule(RelativeLayout.BELOW, b1.getId() - 1);
}
b1.setLayoutParams(lp);
relate.addView(b1);
}
You mustn't give x and y values in Android.you can add buttom top left right of an item. Also layout parameters you should use wrap_content or fill_parent.
Button button = new Button(this);
button.setText(#"text");
button.setLayoutParams(new LayoutParams(WRAP_CONTENT,WRAP_CONTENT));
layout.addView(button);
I think the problem is with the relative layout. Your buttons might be getting stack on top of each other. Try making the parent a linear layout.

How to remove this extra space next to a checkbox?

First off I am new to android development and some seemingly easy tasks are frustrating me to no end. This is a follow on question to what was answered here: Changing the checkbox size in Android 2.0
The gist of it is that for some reason there is this extra space that keeps showing up next to my checkboxes (See Below).
Now I have tried to set the width to 1 and it doesn't get any shorter than what is shown. I have tried to set the width bigger and I can make the size bigger, but no shorter.
Please take a look at the code below and let me know what you see.
TableLayout table = (TableLayout) findViewById(R.id.timeEntriesTable);
for (int i = 0; i < timeEntries.getLength(); i++)
{
Element element = (Element) timeEntries.item(i);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
CheckBox box = new CheckBox(this);
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
box.setPadding(0,0,0,0);
box.setWidth(1);
box.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
if (box.isChecked())
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_on_background));
}
else
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
}
}
});
TextView dateBox = new TextView(this);
String dateText = element.getAttribute("date");
dateBox.setText(dateText);
dateBox.setTextColor(Color.BLACK);
TextView customerBox = new TextView(this);
String customerName = element.getAttribute("customer");
customerBox.setText(customerName);
customerBox.setTextColor(Color.BLACK);
TextView hoursBox = new TextView(this);
String hours = element.getAttribute("hours");
hoursBox.setText(hours);
hoursBox.setTextColor(Color.BLACK);
TextView test = new TextView(this);
test.setTextColor(Color.RED);
test.setText("Test");
tr.addView(box);
tr.addView(dateBox);
tr.addView(customerBox);
tr.addView(hoursBox);
table.addView(tr, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
Thank you in advance!
That looks suspiciously like the "Time Entries" header is in its own table cell in a header row and the column of checkboxes below it is matching the same width. (See how the right edge of the text lines up perfectly with the date below it?) Since the checkbox's layout_gravity is likely defaulting to TOP|LEFT, it's being positioned in the upper left of the cell.
Try altering either the header row or the layout_gravity of the checkboxes.
So after the input of the two other answerers #adamp and #Phobos. I stumbled across what worked for me.
How to set (Text)View attributes programmatically?
By adding this:
TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.width = 15;
lp.setMargins(3, 5, 0, 0);
lp.height = 15;
tr.addView(box, lp);
I was able to tweak that cell exactly how I wanted it. It was very strange to me that it wasn't growing the column of the cell it was growing the checkbox itself. This is because in Android there are no actual cells, it uses the views and their sizes to generate the table apparently.
I have came across no tutorials or anything that has you set the LayoutParams programmatically that way. Thank you for your help troubleshooting this!
Table Layouts naturally want to make all the cells the same size. If you want one, or more, to be different sizes then that takes additional parameters.
To shrink all the extra space in your cells add this to your table object
table.setShrinkAllColumns(true);
Or to just shrink the first column where your checkbox is
table.setColumnShrinkable(int columnIndex, boolean isShrinkable)

Set a layoutmargin on a dynamically created checkbox

I'm writing a task manager app that downloads a list of tasks and subtasks from a server and creates a new checkbox for each item and adds it to a linear layout (called ll below). The problem I'm having is that I cannot set the "layout margin left" using java like I can with XML (this is for the subtasks to indent them a bit on the screen). I can set most other xml properties, but cb.setMargins() doesn't work (says undefined for type checkbox). Setting the padding of course does not achieve the desired result.
for(int i=0;i<tasks.size();i++) {
CheckBox cb = new CheckBox(this);
cb.setText(tasks.get(i).subtask_desc);
cb.setButtonDrawable(R.drawable.checkbox_xml);
ll.addView(cb);
}
Any ideas or how I would work through this?
I think you should add the checkbox to the LinearLayout using the correct LayoutParams:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;
li.addView(cb, params);
Hope that helps!
Hi Try this...
private LinearLayyout ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = 123;;
CheckBox cb;
for(int i = 0; i < 10; i++) {
cb = new CheckBox(this);
cb.setText(categoryListArray[i]);
ll.addView(cb, ll);
}

Categories

Resources