setting layout 9 patch image in background programmatically not working fine - android

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>

Related

What's wrong with this dynamic layout? [Android]

I need to create dynamically LinearLayout which represent this xml code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ddd"
android:orientation="vertical"
android:padding="1dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:paddingBottom="26dp"
android:paddingTop="26dp" >
<LinearLayout
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top"
android:layout_marginLeft="32dp"
android:layout_marginTop="4dp"
android:background="#eee" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:text="Name of event"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="16dp"
android:text="Group of this event"
android:textColor="#aaa"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But the problem is that I wrote some code but I have no idea why after debugging my application the only thing which I see is name of event.
Here's the code:
public class Event extends LinearLayout{
private LinearLayout globalContener;
private LinearLayout contener;
private LinearLayout avatarBox;
private LinearLayout contentBox;
private TextView eventName;
private TextView eventGroup;
private ImageView avatar;
private LinearLayout.LayoutParams globalContenerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
private LinearLayout.LayoutParams contenerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams avatarBoxParams = new LinearLayout.LayoutParams(160, LayoutParams.MATCH_PARENT, 1.0f);
private LinearLayout.LayoutParams contentBoxParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
private LinearLayout.LayoutParams eventNameParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams eventGroupParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams avatarParams = new LinearLayout.LayoutParams(48, 48);
public Event(Context context){
super(context);
// Design of global contener
globalContenerParams.setMargins(0, 0, 0, 32);
globalContener = new LinearLayout(context);
globalContener.setBackgroundColor(0xDDDDDD);
globalContener.setPadding(1, 1, 1, 1);
globalContener.setOrientation(LinearLayout.VERTICAL);
globalContener.setLayoutParams(globalContenerParams);
// Design of contener
contener = new LinearLayout(context);
contener.setBackgroundColor(0xFFFFFF);
contener.setPadding(0, 26, 0, 26);
contener.setOrientation(LinearLayout.HORIZONTAL);
contener.setLayoutParams(contenerParams);
// Design of avatar box
avatarBox = new LinearLayout(context);
avatarBox.setBackgroundColor(0xFFFFFF);
avatarBox.setLayoutParams(avatarBoxParams);
// Design of contet box
contentBox = new LinearLayout(context);
contentBox.setOrientation(LinearLayout.VERTICAL);
contentBox.setBackgroundColor(0xFFFFFF);
contentBox.setLayoutParams(contentBoxParams);
// Design of avatar
avatarParams.gravity = Gravity.TOP;
avatarParams.setMargins(32, 4, 0, 0);
avatar = new ImageView(context);
avatar.setBackgroundDrawable(Const.avatarImg);
avatar.setLayoutParams(avatarParams);
// Design of event name
eventNameParams.gravity = Gravity.TOP;
eventName = new TextView(context);
eventName.setGravity(Gravity.TOP);
eventName.setPadding(16, 0, 0, 4);
eventName.setTextSize(22);
eventName.setText("Sprawdzian z języka polskiego");
eventName.setLayoutParams(eventNameParams);
// Design of event group
eventGroup = new TextView(context);
eventGroup.setGravity(Gravity.CENTER);
eventGroup.setPadding(16, 0, 0, 0);
eventGroup.setTextSize(16);
eventGroup.setTextColor(0xAAAAAA);
eventGroup.setText("4ia Technikum Łączności w Krakowie");
eventGroup.setLayoutParams(eventGroupParams);
// Packing boxes
avatarBox.addView(avatar);
contentBox.addView(eventGroup);
contentBox.addView(eventName);
contener.addView(avatarBox);
contener.addView(contentBox);
globalContener.addView(contener);
// Adding to view
addView(globalContener);
}
}
Is anyone here who can help me with this?
set your main LinearLayout Orientation to Vertical
and then
set contentBoxParams weight to 1f
private LinearLayout.LayoutParams contentBoxParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
then
put this
contentBox.addView(eventName);
before this
contentBox.addView(eventGroup);
and then replace all the
0xFFFFFF with 0xFFFFFFFF

Defining ImageView to be aligned right

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);

Change linear layout top margin programmatically android

i have two linear layouts in one frame layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/image12">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layoutbtnlinear_aboutme"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:gravity="bottom"
android:layout_marginTop="10dp"
android:background="#b2b2b2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgShare_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="right|center|end"
android:layout_weight="1.63"
android:src="#drawable/ic_share" />
<TextView
android:id="#+id/txtTitle_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="left"
android:layout_weight="0.3"
android:fontFamily="Times New Roman"
android:text="About Me"
android:textColor="#android:color/black"
android:textSize="35sp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="#+id/btnSlidingDrawerHandler_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/ic_1_navigation_collapse" />
<ListView
android:id="#+id/listView_layout_aboutme"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:footerDividersEnabled="true"
android:dividerHeight="4px"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical">
</ListView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Here I'm seting top margin of linear layout with id layoutbtnlinear_aboutme to 10dp but in code I want to change this 10dp to 50dp on some condition how can I change this top margin programatically?
layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
params.setMargins(0, 50, 0, 0);
layout.setLayoutParams(params);
LayaoutParams usually create confusion while setting margin because of their parent layout... So this MarginLayoutParams is very useful which works with all layouts.
Java Code
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.width = 200; //Ths value 200 is in px... Please convert in DP
params.leftMargin = 100;
params.topMargin = 200;
Kotlin code
val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams
params.width = 200
params.leftMargin = 100
params.topMargin = 200
This updates the top margin without the need to update the other margin values.
LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.topMargin = 200;
layout.setLayoutParams(layoutParams);
use this
layout = (LinearLayout) findViewById(R.id.layuout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
layout.setLayoutParams(layoutParams );
Use this method to set margin in dp
private void setMargins (View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
// convert the DP into pixel
int l = (int)(left * scale + 0.5f);
int r = (int)(right * scale + 0.5f);
int t = (int)(top * scale + 0.5f);
int b = (int)(bottom * scale + 0.5f);
p.setMargins(l, t, r, b);
view.requestLayout();
}
}
Call the method :
setMargins(linearLayout,0,0,5,0);
https://stackoverflow.com/a/50951911/8523391
Kotlin solution:
fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams
val left = left?.let { it } ?: params.leftMargin
val top = top?.let { it } ?: params.topMargin
val right = right?.let { it } ?: params.rightMargin
val bottom = bottom?.let { it } ?: params.bottomMargin
params.setMargins(left, top, right, bottom)
this.requestLayout()
}
then you just need to:
layoutbtnlinear_aboutme.setMargins(top = 10)
I have set up margins directly using below code (I tried using LinearLayout.LayoutParams but is was not working for me)
LinearLayout layout = (LinearLayout)findViewById(R.id.yourrelative_layout);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(3, 300, 3, 3);
layout.setLayoutParams(params);
Only this here is to notice that LayoutParams should be imported for following package android.widget.RelativeLayout.LayoutParams unless you will hit error.

TextView and EditText alignment issues

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.

Set TextView width to 0dp doesn't work

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.

Categories

Resources