How to put CheckBox and button on same line in Android - android

Is possible to place the checkbox and button on the same line in a vertical Linear Layout?
I made the button and checkbox programmatically with this code:
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.line);
btn.setTextSize(24);
btn.append(rs1.getString(0));
CheckBox checkBox = new CheckBox(this);
//ll = Linear Layout (vertical)
ll.addView(checkBox);
ll.addView(btn);
In this case the checbox is over the button
Someone can help me?
EDIT:
Image that explain what i want

You can use a higher hierarchy for your views. Like adding your both views in a horizontal LinearLayout and then add the new layout to your root layout. Your code would look like this:
LinearLayout myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.line);
btn.setTextSize(24);
btn.append(rs1.getString(0));
CheckBox checkBox = new CheckBox(this);
myLinearLayout.addView(checkBox);
myLinearLayout.addView(btn);
ll.addView(myLinearLayout);

Related

Dynamic buttons in table layout adding Horizontally

I'm trying to add buttons in Table layout.
Initially I will have a single row with a single button.
When the user clicks that button, It should create 2 dynamic buttons in the next row 'Horizontally'.
But with my code, I'm getting vertically dynamic buttons Instead of horizontally.
while(id <rowcount+2)
{
tr = new TableRow(MainActivity.this);
table.addView(tr);
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
you are creating new object of TableRow each time, TableRow represents elements in Horizontal and TableLayout represent data in Vertical that's why you getting Buttons in Vertical manner
tr = new TableRow(MainActivity.this);
while(id <rowcount+2)
{
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);
you are creating a new row each time for the loop
try this.
tr = new TableRow(MainActivity.this);
while(id <rowcount+2) {
Button btn1 = new Button(MainActivity.this);
btn1.setText("");
btn1.setOnClickListener(this);
btn1.setId(id);
tr.addView(btn1);
id++;
}
table.addView(tr);

Add many linear layout in to scroll view

I have some code like this:
l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(l);
TextView ch = new TextView(this);
Button ndda = new Button(this);
ndda.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button nddb = new Button(this);
Button nddc = new Button(this);
Button nddd = new Button(this);
ch.setText("ndch");
ndda.setText("da");
nddb.setText("db");
nddc.setText("dc");
nddd.setText("dd");
l.addView(ch);
l.addView(ndda);
l.addView(nddb);
l.addView(nddc);
l.addView(nddd);
now i want to add lot of linear layout( may be 15) in to scroll view. How can I do that with shortly code? and the Button in each linear layout i want to setOnClickListener on them to do some thing. I try to do this with listview but it's alway refresh when scroll, i can't disable refresh. I'm a newbie so pls show me detail. Thank for all
Some guys already mentioned it before in the comments... use a ListView or even better ... a RecyclerView.
Here's a good tutorial for the RecylcerView.
Nevertheless a ScrollViewcan have only 1 child (in your case a LinearLayout), which again can contain multiple layouts within.

Add radio group to Table LAyout, android?

This is the layout I want to create but dynamically through java code.
Here parent layout is LinearLayout.
The tableLayout created through java code consists of 5 radio buttons which are created dynamically.
I need to place these radio button in radio group.
How to do that.
So far i tried this which is working fine. But am not able to add all the 5 radio buttons in 1 radio group.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); //parent layout
t1 = new TableLayout(this);
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
t1.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
TableRow row1 = new TableRow(this);
row.addView(rb1);
row.addView(rb2);
row1.addView(rb3);
t1.addView(row);
t1.addView(row1);
l.addView(t1);
}
I can't find you'r declaration of a RadioGroup.
You need something like this:
...
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
row.addView(rg);
...
RadioGroup inherits from LinearLayout. You'll have to create your own implementation of RadioGroup that inherits from TableLayout.
If you examine the source code of RadioGroup, you'll see it just keep track of its child views which are RadioButton's and makes sure only one of them is checked. Apart from that it provides a listener if checked RadioButton changes.
You can try modifying the source to make it extend from TableLayout etc.

Add new EditText component to an Activity at run time

How can i add a new EditText component to an Activity layout? I don't want to add the component from the GUI editor statically, but add it to the activity at run time, for example when the user clicks a button?
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ur_layout);
EditText editTextView = new EditText(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
linearLayout.addView(editTextView);
Have the EditText in the layout XML for your activity with android:visibility="gone".
When you want to show the EditText do:
EditText ed = (EditText) findViewById(R.id.your_edit_text);
ed.setVisibility(View.VISIBLE);

How do you declare part of a page programmaticaly

If I have a page whose layout is designated by XML code, but I want to possibly create a few radio buttons, say, in the middle, but decide that at runtime, how would I do it? I'm new to Android so I'm taking a stab in the dark. Would something like this work?
In the XML, add a LinearLayout to the middle of the page's XML like this:
<LinearLayout android:id="#+id/LinLayBut"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
And then in the java something like this:
public void setupRadioButtons(){
LinearLayout linLay;
linLay = (LinearLayout) findViewById(R.id.LinLayBut);
RadioGroup radGroup = new RadioGroup(this);
RadioButton radBut = new RadioButton(this);
radGroup.addView(radBut, 0);
radGroup.setText("A button");
}
This is not an efficient way to build dynamic UI. You would be better off defining the optional layout in an XML file and then inflate it when you want to use it:
public void setupRadioButtons() {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout buttons =
(LinearLayout) inflater.inflate(R.layout.LinLayBut, null);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
mainLayout.addView(buttons);
}
The above code assumes that the radio group and buttons are defined inside the LinearLayout with id LinLayBut and you main layout id is main.
OK, thanks to unluddite, I got it to work. For those tortured souls following the thread, here's the code. The XML doesn't have a layout around it. And if I don't call the method, the radio group takes no vertical space:
<RadioGroup android:id="#+id/radButGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
and here's the method:
public void setupRadioButtons(){
RadioGroup radGroup;
radGroup = (RadioGroup) findViewById(R.id.radButGroup);
RadioButton radBut0 = new RadioButton(this);
radGroup.addView(radBut0, 0); //2nd arg must match order of buttons
radBut0.setText("one Button");
RadioButton radBut1 = new RadioButton(this);
radGroup.addView(radBut1, 1);
radBut1.setText("Two Button");
radBut1.setChecked(true); //which button is set

Categories

Resources