Getting exception when inserting tablerow through code in a TableLayout - android

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.

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 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.

Dynamically add element to layout

I am quite new to developing apps. Still I would have thought that this is a basic action, so if there is already a solved thread I would be OK with the link. But since I am searching for over 2 hours for this I am asking anyway:
I want to dynamically add an element to my layout every time the user clicks a button.
By now I have this:
XML (R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Java
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
Since the XML file does not change, it only works once.
So how can I add a second text when the user clicks the button a second time (without changing the XML file)? Examples are appreciated.
The problem comes from this line, that recreate the whole layout every time:
LinearLayout ll = new LinearLayout(this);
You should define it and setContentView(ll) outside the submitChoice function. Then on click only create and add the textView , then call ll.invalidate(); to see the changes.
Something like:
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ll = (LinearLayout) findViewById(R.id.ll_game);
}
// More code...
public void submitChoice(View view) {
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
ll.addView(textView);
ll.invalidate();
}
where ll_game is the id you have to set in xml for your LinearLayout.

how to get values from edit text without using xml code

I have created edittext and textview without using xml attributes i have defined in java file only but i am unable get values entered in edittext how to get values.i have 2 edittexts if user enters the values i should add the values
public class JavacodeActivity extends Activity {
LinearLayout layout;
TextView view;
EditText edit;
Button btn;
EditText edit1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("enter the value");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
edit1=new EditText(this);
edit1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
edit1.setHint("enter the number");
edit1.setGravity(Gravity.CENTER);
edit1.setTextColor(Color.RED);
edit1.setTextSize(20);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
layout.addView(edit1);
btn=new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
btn.setText("add");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
btn.setTextColor(Color.GREEN);
btn.setTextSize(20);
layout.addView(btn);
view=new TextView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
view.setText("addition of values");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.BLUE);
view.setTextSize(20);
layout.addView(view);
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values. Please help me.
I don't know the attribute to retrieve values from edittext if the code is java I am unable to retrieve values
Read your EditText like any other EditText:
String text = edit.getText().toString();

Android - how do I display a list of items in an HTML-like Ordered list?

Still new to Android
I want to display some data as an HTML-like ordered list (non-actionable).
Example;
Item One
Item Two
Item Three
I feel I am missing something obvious.
Thanks,
JD
Either use a ListView and do nothing when something is selected, Use an AlertDialog.Builder and call setItems and do nothing in the Select handler, or use a WebView and create the list in html as on ordered list.
There's no default view or widget to do this i guess. You can try with ListView or doing the views programatically (however this is a little dirty)
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
The Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get your main layout from the XML
LinearLayout llMain = (LinearLayout) findViewById(R.id.llMain);
ArrayList<String> alItems = new ArrayList<String>();
//Put some example data
alItems.add("Text1");
alItems.add("Text2");
alItems.add("Text3");
for(int i=0; i<alItems.size(); i++){
//We create a Layout for every item
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//A TextView to put the order (ie: 1.)
TextView tv1 = new TextView(this);
tv1.setText(i+1 + ". ");
ll.addView(tv1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0));
//TextView to put the value from the ArrayList
TextView tv2 = new TextView(this);
tv2.setText(alItems.get(i));
ll.addView(tv2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
//Add this layout to the main layout of the XML
llMain.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0));
}
}

Categories

Resources