How to add CheckBoxes dynamically to an Android layout? [duplicate] - android

This question already has answers here:
android add checkbox dynamically
(3 answers)
Closed 9 years ago.
I have to add CheckBox dynamically to my Activity layout, the XML of the layout is as follows
`
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentSV"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/parentLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<RelativeLayout
android:id="#+id/feedbackRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/feedbackCustomerNameLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Name : "
android:textColor="#android:color/black" />
<TextView
android:id="#+id/feedbackCustomerName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="#android:color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/feedbackPlansLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackCustomerNameLL"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Plans Explained"
android:textColor="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
`
CheckBoxes are to be added in the commented area. How to add them dynamically, because I have to add them in runtime based on data sent from server. I cannot remove the ScrollView or any other view from the hierarchy.

Give that layout an id such as...
<LinearLayout
android:id="#+id/check_add_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
Initialize a parent layout using the given id as...
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.check_add_layout);
Then create your CheckBox...
CheckBox checkBox = new CheckBox(this);
checkBox.setId(id);
checkBox.setText("text");
Create parameters about its size, padding, alignment...
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.CENTER;
now add this newly created CheckBox to the that parent layout...
parentLayout.addView(checkBox, checkParams);

Follow following steps to add CheckBox dynamically :
STEP 1: Assign id to LinearLayout in xml to for accessing in code :
<LinearLayout
android:id="#+id/yourlayout"
....
/>
STEP 2: access layout in code :
LinearLayout linear=(LinearLayout)findViewById(R.id.yourlayout);
STEP 3: Add CheckBox to layout :
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<your_count;i++){
CheckBox checkBox = new CheckBox(context);
checkBox.setLayoutParams(lparams);
linear.addView(checkBox);
}

Try This:
LinearLayout layout = (LinearLayout) findViewById(R.id.yourlayout);
CheckBox chkTeamName = new CheckBox(this);
chkTeamName.setText(teamName.get(i));
chkTeamName.setTextColor(Color.BLACK);
layout.addView(chkTeamName);

CheckBox checkBox = new CheckBox(context);
// set the properties including layoutparams
// Then add it to parent
Parent.addView(checkBox);

Give an ID to your LinearLayout and do
LinearLayout linear = (LinearLayout) findViewById('your id');
Checkbox cb = new CheckBox(this);
//Give customization to it
linear.addView(cb);
Simple. :) Isn't it. Hope this helps :)

Related

How remove scrollview programmatically (e.q. disable/enable wordwrap)?

XML (variant 1) - text WORD WRAP OFF:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</HorizontalScrollView>
</LinearLayout>
How remove HorizontalScrollView programmaticaly, after that XML should be, like :
XML (variant 2) - TEXT WORD WRAP ON
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
What the appropriate way for do this:
Maybe use dynamic fragment ? Maybe use View.removeView ?
Or....
Please help...
This code should remove the scroll view and put the item in it's place (For this to work be sure to add the id's to the view elements on the xml).
View bodyLayout = (View)m_dialog.findViewById(R.id.my_item);
ScrollView scroller = (ScrollView)m_dialog.findViewById(R.id.my_scroll);
ViewGroup topLayout = (ViewGroup)scroller.getParent();
ViewGroup.LayoutParams params = scroller.getLayoutParams();
int index = topLayout.indexOfChild(scroller);
scroller.removeAllViews();
topLayout.removeView(scroller);
topLayout.addView(bodyLayout, index, params);

Why are the textviews invisible?

I use the following layout (shortened):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" ...>
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
Like this (shortened also):
LayoutInflater inflater = parent.getLayoutInflater();
View layout = inflater.inflate(R.layout.merger_sale, null);
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
builder.setTitle(R.string.sell_stock_after_merger)
.setView(layout)
.setPositiveButton(R.string.sell_all, new OnClickListener() {...
.setNeutralButton(R.string.trade_sell, new OnClickListener() {...
.setNegativeButton(R.string.trade_all, new OnClickListener() {...
AlertDialog dialog = builder.create();
dialog.show();
The layout has no errors, nor does the code. The layout shows exactly as it should in Eclipse's layout editor.
The dialog appears when it should appear, buttons behave how they should, except for the texview (#+id/sell_text) [and the other textviews in the shortened <TableRow>s], which don't appear.
What am I overlooking? Thanks!
I think it would work if you change the width and height to wrap content instead of match_parent.
match_parent means it would be fillparent which may fill the entire layout.
Or try giving weights to each item like layout_weight with value as 0 .This may work either
try it:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>

How to use and dynamically populate multiple tabhost views

I need to create a single activity tabhost, ideally create tabs and views, and dynamically populate the views with widgets. Having a 'control' button at the bottom of the screen is important.
I am able to create three tabs, using three layouts. Getting the EditTexts to a) appear, and b) line up below the tabs [using the separator] was a challenge, assisted by existing answers on StackOverflow.
What I now cannot work out is why the EditTexts on the second layout/tab don't appear, whereas those on the first tab do?
The layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<View
android:id="#+id/separator"
android:layout_below="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="2dip" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_below="#+id/separator"
android:layout_above="#+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tabview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/ll1text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="ll1text" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/ll2text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ll2text" />
</LinearLayout>
<RelativeLayout
android:id="#+id/tabview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/rl3text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="ll3text" />
</RelativeLayout>
</FrameLayout>
<Button
android:layout_alignParentBottom="true"
android:text="Start Travelling"
android:id="#+id/btnSend"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</RelativeLayout>
</TabHost>
The activity code
setContentView(R.layout.template);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Customer").setContent(R.id.tabview1));
LinearLayout ll = (LinearLayout) findViewById(R.id.tabview1);
EditText et1 = new EditText (this);
et1.setText("ET1");
ll.addView(et1);
TextView tv2 = new TextView(this);
tv2.setText("et2:");
ll.addView(tv2);
EditText et2 = new EditText (this);
et2.setText("ET2");
ll.addView(et2);
th.addTab(th.newTabSpec("tab2").setIndicator("Job").setContent(R.id.tabview2));
ll = (LinearLayout) findViewById(R.id.tabview2);
EditText et21 = new EditText (this);
et21.setText("et21");
ll.addView(et21);
EditText et22 = new EditText (this);
et22.setText("et22");
ll.addView(et22);
EditText et23 = new EditText (this);
et23.setText("et23");
ll.addView(et23);
th.addTab(th.newTabSpec("tab3").setIndicator("Tab 3").setContent(R.id.tabview3));
RelativeLayout rl = (RelativeLayout) findViewById(R.id.tabview3);
EditText et3 = new EditText (this);
et3.setText("ET3");
rl.addView(et3);
th.setCurrentTab(0);
How come the three EditTexts on the second tab don't show?
I think the problem is that you are trying to hold all your tabs under one activity. You should separate them as follows:
Use a class that deals with the TabActivity
Create different Activities for each of your tabs
Add a tabhost layout
Check out this tutorial for help.

Layout References

I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondoverde3"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="adasdasdasdas"
ads:loadAdOnCreate="true" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout4"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
So what I would like is to refer in my java code to the LinearLayout2 an add some things. I prove with an easy code to know that my code works (and it works!! I show the two textviews), exactly with this:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tituloIngr2 = new TextView(this);
tituloIngr2.setText("AAAAA");
ll.addView(tituloIngr2);
TextView tituloIngr1 = new TextView(this);
tituloIngr1.setText("BBBBB");
ll.addView(tituloIngr1);
But what I would like to do is to show in this linearLayout2 many Textviews that I take from a StringArray, so the code is the next:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
for (int m= 0; m<few.length; m++) {
TextView ingr= new TextView(this);
ingr.setText(few[m]);
ingr.setPadding(5, 5, 5, 5);
ingr.setGravity(0x11);//sacados todos los values de developers
ingr.setTextColor(0x96610098);
ingr.setBackgroundResource(android.R.color.white);
ingr.setTextSize(22);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
ll.addView(ingr, layoutParams);
}
this.setContentView(ll);
The error is a force close, and the log cat:
11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Remove this call:
this.setContentView(ll);
ll is already part of the content view. Trying to add it again as the content view is likely what's triggering the exception. You also don't need this call:
ll.setOrientation(LinearLayout.VERTICAL);
The orientation is specified as vertical in the xml.

How to add row margin while inflating a LinearLayout?

I have a Linearlayout part of a Tablerow of tablelayout. Below is sample description
<!-- Master Layout-->
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="match_parent"
android:stretchColumns="0" android:baselineAligned="false">
<TableRow>
<Button></Button>
</Tablerow>
<TableRow>
<LinearLayout android:id="#+id/listinfo"></LinearLayout>
</Tablerow>
Then I wrote another linearlayout to create rows of linearlayout which I inflate and add to
linearlayout of Main Layout.
<!-- listrow layout-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="left"
android:src="#drawable/icon" />
<TextView android:layout_weight="1" android:id="#+id/textView1" />
<TextView android:layout_weight="1" android:id="#+id/textView1" />
</LinearLayout>
Everything is working perfectly fine but I am not getting row demarcators. How to do that?
Java code
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listinfo);
for (int i =0; i < myarray.size(); i++) {
final Employee eobj = myarray.get(i);
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null);
lrow.setBackgroundColor(android.graphics.Color.WHITE);
lrow.setPadding(2, 2, 2, 2);
((TextView)lrow.getChildAt(1)).setText(eobj.getName());
//some more settings
placeHolderLinearLayout.addView(lrow);
}
The view doesn't show any demarcator between subsequent linearlayout. How can I achieve that?
|---------------------|
| lrow1 |
|______demarcator_____|
| lrow2 | => The demaractor is missing in my view
|_____________________|
LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null);
Try to pass placeHolderLinearLayout instead null.
If you define true as third parameter the infalted layout will be added automatically to the given ViewGroup. false will avoid that.

Categories

Resources