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);
Related
I have the code as below, and I need 2 things:
I wish to put tv[i] on the same line as txt[i]
and I want keyboard to appear after touching tv[i].
I'm a complete beginner to Android. Thank you.
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Button[] buttons = new Button[10](this);
/*
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
Button txt1 = new Button(this);
// linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);
*/
Integer.toBinaryString(12);
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};
MAYBE I'm putting your line
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
at a wrong place.
KOD MainActivity.java
public void click2(View view) {
Button button3 = findViewById(R.id.button2);
button3.setText("hello");
// Put buttons into an array
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
// GridLayout gridLayout=(GridLayout) findViewById(R.id.activity_main);
// Put buttons into an array
// Button[] txt = {new Button(this), new Button(this)};
Button[] txt = new Button[8];
TextView[] tv = new TextView[8];
// loop over all values of i between 0 to the end of the button array
for (int i = 0; i < txt.length; i = i + 1) {
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Access array elements by index so txt1 is txt[1], etc.
txt[i]=new Button(this);
txt[i].setText(Integer.toBinaryString(i));
linearLayout.addView(txt[i]);
tv[i]=new TextView(this);
linearLayout.addView(tv[i]);
}
};
Call
linearLayout.setOrientation(LinearLayout.VERTICAL);
to make 2 items appear side by side.
For showing keyboard, take a look here:
How do you close/hide the Android soft keyboard programmatically?
Looks like you are referencing your xml file here:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
You should go to your xml file, add a id (android:id="#+id/some_id") tag to your <LinearLayout> and use this id in your LinearLayout linearLayout =(LinearLayout) findViewById(R.id.HERE)
Other option is to go to your xml file and add sandroid:orientation="horizontal" like this:
<LinearLayout
android:id="#+id/some_id"
android:orientation="vertical"
...>
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);
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.
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.
I am trying to insert rows from my code in a TableLayout. I got several tutorials on internet and stackOverflow and some how each time i get this exception.
12-12 17:54:07.027: E/AndroidRuntime(1295): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-12 17:54:07.027: E/AndroidRuntime(1295): at com.kaushik.TestActivity.onCreate(TestActivity.java:41)
Here is the activityclass:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* adding another row */
TableRow tr2 = new TableRow(this);
tr2.addView(b); // Exception is here
tl.addView(tr2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
and here is the XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TableLayout>
Please help me.
Your are doing wrong with
/* adding another row */
TableRow tr2 = new TableRow(this);
tr2.addView(b); // Exception is here
B is a button , as its already added in your table 1st row "t1". As button is a view and each view can be hold by one parent only. The Button b is already shown to 1st row. Its can be show again on row2.
As it don't make a logic when user click the button or row1 or row2 then how to know which button is pressed? I mean you can't know it is pressed by row 1 or row 2. So this is unexpected thing you are doing.
As in
onClick(View view){
if(view == b){
// So you cant do that this is button row1 button or row2 button.
}
// Or you can check the pressed button by id which will also be same.
}
So you should create new Button button2 and then add to row2.