How to set programmatically width of TextView to be 0dp ? I am trying to put in LinearLayout couple TextViews but all to be same width, and when I put in xml like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="test"
android:layout_gravity="right"
android:gravity="right"
/>
</LinearLayout>
but when in code try like
TextView txtTemp = new TextView(this);
txtTemp.setText(captures.get(i));
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
if (i == 0) {
tempLL.gravity = Gravity.LEFT;
}
if (i == (captures.size() - 1)) {
tempLL.gravity = Gravity.RIGHT;
} else {
tempLL.gravity = Gravity.CENTER;
}
txtTemp.setLayoutParams(tempLL);
I got exception on tempLL.width null exception. How to solve this ?
Instead of using
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
try this
LinearLayout.LayoutParams tempLL = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT,1.0);
txtTemp.setLayoutParams(tempLL);
You are creating a new textview and there is no layoutparams specified yet. So getLayoutParams should return null. So create a new layoutparam object and set that to the new textview.
Related
here is xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
>
<TextView
android:id="#+id/textview_chatdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="date"
android:layout_marginTop="#dimen/chattab_messsagelayout_marginbottom"
android:textSize="#dimen/chattab_textview_date_fontsize"
android:textColor="#color/chattab_textview_date_fontcolor"
android:background="#android:color/transparent"
/>
<LinearLayout
android:id="#+id/llayout_chatlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview_chatdate"
android:layout_marginTop="#dimen/chattab_messsagelayout_margintop"
android:layout_marginLeft="#dimen/chattab_messsagelayout_marginleft"
android:layout_marginRight="#dimen/chattab_messsagelayout_marginleft"
android:layout_alignParentRight="true"
android:background="#drawable/aqua"
android:orientation="vertical">
<TextView
android:id="#+id/textview_chatname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="5dp"
android:text="username"
android:textSize="#dimen/chattab_edittext_message_fontsize"
android:textColor="#color/chattab_textview_chatname_fontcolor"
android:lines="1"
android:maxLines="1"/>
<TextView
android:id="#+id/textview_chatmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:padding="5dp"
android:text="message here"
android:textSize="#dimen/chattab_edittext_message_fontsize"
android:textColor="#color/chattab_textview_date_fontcolor"
android:maxLines="3"/>
</LinearLayout>
</RelativeLayout>
if(broadcastResponse.getUid().equals(uId))
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0,0,R.dimen.chattab_llayout_chatlayout_marginright,0);
holder.messageLayout.setLayoutParams(params);
holder.messageLayout.setBackgroundResource(R.drawable.aqua);
}
else
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.setMargins(0, 0, R.dimen.chattab_llayout_chatlayout_marginright, 0);
holder.messageLayout.setLayoutParams(params);
holder.messageLayout.setBackgroundResource(R.drawable.grey);
}
**
holder.messageLayout is the LinearLayout having id:llayout_chatlayout in xml see image it should display image same as aqua and align right
while right now its not setting image properly.how to solve this issue
? i already have all 9 patch images in all folders in hdpi ,
mdpi,xhdpi ,xxhdpi
now i m using 9 patch image programmmatically in my adapter class
but the image i m setting using java in not working fine while xml
image is displayed correctly.how can i align right the layout with grey image while align left with aqua image inside condition.
You can set your as per direction of message coming.
i have put some code for your understanding.
You can put this code in your adapter class.
private void setAlignment(ViewHolder holder, boolean isOutgoing) {
if (!isOutgoing) {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
layoutParams.gravity = Gravity.RIGHT;
holder.contentWithBG.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.content.setLayoutParams(lp);
layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
layoutParams.gravity = Gravity.RIGHT;
holder.txtInfo.setLayoutParams(layoutParams);
if (holder.txtMessage != null) {
holder.contentWithBG.setBackgroundResource(R.drawable.incoming_message_bg);
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
layoutParams.gravity = Gravity.RIGHT;
holder.txtMessage.setLayoutParams(layoutParams);
} else {
holder.contentWithBG.setBackgroundResource(android.R.color.transparent);
}
} else {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
layoutParams.gravity = Gravity.LEFT;
holder.contentWithBG.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.content.setLayoutParams(lp);
layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
layoutParams.gravity = Gravity.LEFT;
holder.txtInfo.setLayoutParams(layoutParams);
if (holder.txtMessage != null) {
holder.contentWithBG.setBackgroundResource(R.drawable.outgoing_message_bg);
layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
layoutParams.gravity = Gravity.LEFT;
holder.txtMessage.setLayoutParams(layoutParams);
} else {
holder.contentWithBG.setBackgroundResource(android.R.color.transparent);
}
}
}
Xml:
<LinearLayout
android:id="#+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/incoming_message_bg"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:maxWidth="250dp" />
</LinearLayout>
Tried to align imageview to right.. code crashes. why?
UPDATED: Here is all row im having problem with. I think i failed on determining layouts somewhere here
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParam
s.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textView1.setText(names);
tableRow1.addView(textView1);
ImageView icon;
icon = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);
// icon.setLayoutParams(params);
icon.setImageResource(R.drawable.ic_done); //default value
switch (value) {
case "1":
icon.setImageResource(R.drawable.ic_done);
break;
case "0":
icon.setImageResource(R.drawable.ic_bad);
default:
break;
}
System.out.println("value : " + value);
icon.setPadding(densityToPixels(13), 0, 0, 0);
tableRow1.addView(icon);
try in xml
<RelativeLayout
android:id="#+id/beam_contact_entry_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/beam_contact_entry_invite"
android:background="?entry_background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_toRightOf="#+id/contact_info_avatar" >
<TextView
android:id="#+id/beam_contact_fragment_entry_text"
style="?primary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alexander Great"
android:textSize="18sp" />
<TextView
android:id="#+id/beam_contact_fragment_entry_text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-4dp"
android:text="mobile"
android:visibility="gone"
style="?secondary_text"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/beam_contact_entry_invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/beam_contact_entry_contact"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/beam_contact_entry_contact"
android:background="?entry_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/beam_contact_fragment_entry_right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?entry_background"
android:duplicateParentState="true"
android:src="#drawable/sv_svyaznoy_contact" />
</LinearLayout>
First you defined the ImageView dynamically like
ImageView icon = new ImageView(this);
then you are trying to get the LayoutParam through icon.getLayoutParams(); which doesn't have any LayoutPram because you haven't set any.
To make it work replace
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)icon.getLayoutParams();
with
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);
Hello I have a main screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="55dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/toggleButton1"
android:background="#android:drawable/btn_default"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/plus_grey" />
<LinearLayout
android:id="#+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="0dp"
android:background="#A0FFFFFF"
android:orientation="horizontal"
android:visibility="invisible" >
<TextView
android:id="#+id/locinfo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="#color/cherniy"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
And there is a imageButton
button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bIcon == false) {
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
}
else {
button.setImageResource(R.drawable.plus_grey);
m.remove();
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t.setVisibility(View.INVISIBLE);
linLayout.setVisibility(View.INVISIBLE);
bIcon = false;
}
}
});
I want to programmatically add
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
where w
display = getWindowManager().getDefaultDisplay();
#SuppressWarnings("deprecation")
w = display.getWidth();
But when I do like this
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
My application crashes. Can you please tell how to programmatically create with my parameters (tied with the width and height of the screen of the device)? I will take any suggestions. Thank you.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(w*2)/3,
LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.
Instead use this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(w*2)/3,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
or
Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.
RelativeLayout.LayoutParams lp = linLayout.getLayoutParams();
lp.width = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);
Instead of making a new LayoutParams, you should modify the existing one.
Change your code to:
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = linLayout.getLayoutParams();
final int left_margin = whatever;
final int top_margin = 0;
final int right_margin = whatevermore;
final int bottom_margin = 0;
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
lp.width = (w*2)/3;
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
I'm adding TextViews and EditTexts to the the UI, but they are overlapping each other. I want them to appear next to each other. What I am missing in this code?
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setBackgroundResource(R.drawable.background);
for (int i = 0; i < 10 /*Changed the actual value for better Understanding*/; i++) {
tv = new TextView(this);
tv.setText("" + (productStr[i]));
tv.setId(i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_RIGHT, RelativeLayout.TRUE);
ll.addView(tv, lay);
et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setEms(2);
allEds.add(et);
et.setId(i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(et, p);
}
this.setContentView(sv);
Use LinearLayout instead of RelativeLayout, and leave positioning to the layouter.
PS: it's spelled "them" not "dem".
There is no need to add extra layouts, you could do it with the RelativeLayout. The code below should layout the TextView and EditText like you want:
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
ll.setBackgroundResource(R.drawable.background);
sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
for (int i = 0; i < 10; i++) {
tv = new TextView(this);
tv.setText("" + (productStr[i]));
tv.setId(1000 + i);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
if (i != 0) {
lay.addRule(RelativeLayout.BELOW, 2000 + i - 1);
}
ll.addView(tv, lay);
et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setEms(2);
et.setId(2000 + i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF, tv.getId());
if (i != 0) {
p.addRule(RelativeLayout.BELOW, 2000 + i - 1);
}
ll.addView(et, p);
}
this.setContentView(sv);
Just use LinearLayout in your xml. Something like this will generate a TextView next to the EditText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
</LinearLayout>
P.S: There are few norms which everyone follows in asking questions on this forums, like not using chat language in your question. Make short and sweet title and you can write any amount of explanation in your post.
I'm trying to correlate how the Android XML looks versus creating it programmatically and I'm failing miserably. I've read several references stating that the type for the LayoutParams for the View must be equal to those of the parent, but I'm just not grokking it.
Given the following XML, how would I re-create this programmatically?
<TableRow
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:paddingTop="3dp"
android:orientation="vertical"
>
<EditText
android:padding="2dp"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_column="0"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="15dp"
android:numeric="integer"
/>
</LinearLayout>
<LinearLayout
android:layout_height="35dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:padding="2dp"
android:orientation="vertical"
>
<CheckBox android:text="Check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox"
android:textColor="#color/Gray"
/>
<CheckBox android:text="Check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox"
android:textColor="#color/Gray"
/>
</LinearLayout>
Here's what I've tried:
// Get the TableLayout
TableLayout tl = (TableLayout)findViewById(R.id.score_table);
LayoutParams lp;
TableRow.LayoutParams tp;
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout ll0_1 = new LinearLayout(this);
// If the layout params of the child should be of
// the type of the parent this should be
// TableRow.LayoutParams?
tp = (TableRow.LayoutParams)tr.getLayoutParams();
tp.height = TableRow.LayoutParams.WRAP_CONTENT;
tp.width = TableRow.LayoutParams.FILL_PARENT;
tp.gravity = Gravity.CENTER_VERTICAL;
// TableRow.LayoutParams has no padding so this
// gets set on the LinearLayout?
ll0_1.setPadding(0,3,0,0);
ll0_1.setLayoutParams(tp);
tr.addView(ll0_1);
EditText et0 = new EditText(this);
et0.setId(100);
et0.setInputType(InputType.TYPE_CLASS_NUMBER);
// Same as above; LP set to type of parent
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f);
lp.height = 35;
lp.width = 100;
// Same as above; the parent's type doesn't have
// these attributes
et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
et0.setPadding(0, 3, 0, 0);
et0.setLayoutParams(lp);
ll0_1.addView(et0);
LinearLayout ll0_2 = new LinearLayout(this);
ll0_2.setOrientation(LinearLayout.VERTICAL);
tr.addView(ll0_2);
CheckBox cb0_1 = new CheckBox(this);
cb0_1.setTextSize(10f);
cb0_1.setTextColor(Color.GRAY);
cb0_1.setBackgroundResource(R.drawable.checkbox_background);
cb0_1.setButtonDrawable(R.drawable.checkbox);
cb0_1.setText("Nil");
ll0_2.addView(cb0_1);
CheckBox cb0_2 = new CheckBox(this);
cb0_2.setTextSize(10f);
cb0_2.setTextColor(Color.GRAY);
cb0_2.setBackgroundResource(R.drawable.checkbox_background);
cb0_2.setButtonDrawable(R.drawable.checkbox);
cb0_2.setText("10fer");
ll0_2.addView(cb0_2);
// Duplicate code removed for brevity
//...
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
There is no error, but it does not match the XML. Instead, it looks like this:
The first one is from the XML, the second one is my attempt at reproducing it programmatically.
LayoutInflater is the easiest way :
public View myView(){
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.myview, null);
TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
return v;
}