Android how to set 2 TextView side by side programmatically? - java

How i can display two textview side by side in Java ?
I succeeded to do in xml !
My code :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>

Make a LinearLayout in my activity_layout.
LinearLayout lm=(LinearLayout) findViewById(R.id.Linearlayout1);
Make LinearLayout of type horizontal, dynamically.
LinearLayout llh=new LinearLayout(context);
llm.setOrientation(LinearLayout.HORIZONTAL);
Then created two TextView dynamically
TextView tv1=new TextView(context);
TextView tv2=new TextView(context);
finally add these two TextView's to the horizontal LinearLayout (that we created dynamically) and later the same layout to the xml layout.
llh.addView(tv1);
llh.addView(tv2);
lm.addView(llh);
I hope it was helpful.

Try this,
Java Code,
layout = (LinearLayout)findViewById(R.id.mainLay);
TextView tv1 = new TextView(ActivityName.this);
tv1.setText("TextView 1");
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv1.setLayoutParams(childParam1);
TextView tv2 = new TextView(ActivityName.this);
tv2.setText("TextView 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv2.setLayoutParams(childParam2);
layout.addView(tv1);
layout.addView(tv2);
and xml code,
<LinearLayout 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">
<LinearLayout
android:weightSum="1"
android:id="#+id/mainLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
This should work.

First Solution:
use online converter
[http://www.xmltojava.com/][1]
Second Solution:
inflate your layout
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layoutView = mInflater.inflate(R.rowLayout, null);
TextView tvOne=(TextView) layoutView.findViewById(R.id.tvOne);

this is what I try to do
final LinearLayout ll = (LinearLayout) findViewById(R.id.container_LV);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
//ll.setOrientation(orientation);
// params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
LinearLayout llh=new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < fields.length(); i++)
{
final TextView txtLabel = new TextView(getApplicationContext());
final TextView txtValue = new TextView(getApplicationContext());
jsonLabel = fields.getJSONObject(i).getString(TAG_LABEL) ; //+ "\n";
jsonValue = fields.getJSONObject(i).getString(TAG_VALUE) ; //+ "\n";
//txtLabel.setBackgroundColor(Color.YELLOW);
//txtValue.setBackgroundColor(Color.YELLOW);
//txtLabel.setWidth(pixels);
txtLabel.setLayoutParams(params);
txtValue.setLayoutParams(params);
txtValue.setGravity(Gravity.RIGHT);
txtLabel.setGravity(Gravity.LEFT);
//params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
txtLabel.setText(jsonLabel);
txtValue.setText(jsonValue);
txtLabel.setTextColor(Color.WHITE);
txtValue.setTextColor(Color.WHITE);
//txtLabel.
llh.addView(txtLabel);
llh.addView(txtValue);
}
ll.addView(llh);

The key is to use android:layout_weight only after setting android:layout_width to "0dp", try this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>

Related

How to save the data from multiple Edittexts that are being added dynamically in android?

I have some EditTexts and I'm trying to save their values to an ArrayList, but my EditTexts are being added by using an add button which adds a LinearLayout, and the EditTexts are within the LinearLayouts being created.
Normally having a fixed amount of EditTexts would be easy to save, but how would I save a continuous amount of EditTexts. I'm adding the LinearLayouts in the method createNewLinearLayout() below, and I want to save the input from the EditTexts from those LinearLayouts.
How can I do that?
My layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeOverall"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:id="#+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addCounter"
android:text="Add"
android:layout_marginLeft="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/saveBtn"
android:layout_toRightOf="#+id/addCounter"
android:text="Save Data"
android:layout_marginLeft="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnView"
android:text="View Data"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/saveBtn" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/horizontalLine"
android:background="#B3B3B3"
android:layout_below="#+id/relativeLayout"
android:layout_marginTop="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nameEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="Name of Patient"
android:layout_below="#+id/horizontalLine"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine2"
android:background="#B3B3B3"
android:layout_below="#+id/nameEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="ID of Patient"
android:layout_below="#+id/horizontalLine2"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine3"
android:background="#B3B3B3"
android:layout_below="#+id/idEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/actionEditText"
android:textAlignment="center"
android:hint="Action To Record"
android:layout_below="#+id/horizontalLine3"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/durationEditText"
android:textAlignment="center"
android:hint="Duration of Action"
android:layout_below="#+id/actionEditText"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#+id/durationEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:id="#+id/minusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="-"
android:textSize="25dp"/>
<TextView
android:id="#+id/counterTxt"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textSize="45sp"
android:text="25"/>
<Button
android:id="#+id/plusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/delete"
android:layout_below="#+id/linearLayout"
android:text="Delete"
android:textAlignment="center"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/linearLayoutText"
android:layout_below="#+id/horizontalLine3">
</LinearLayout>
</RelativeLayout>
</ScrollView>
My method
private LinearLayout createNewLinearLayout(){
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.gravity = Gravity.CENTER_HORIZONTAL;
final ViewGroup.LayoutParams btnParam = plusBtn.getLayoutParams();
final ViewGroup.LayoutParams textParam = counterTxt.getLayoutParams();
final LinearLayout.LayoutParams deleteParam = new LinearLayout.LayoutParams(delete.getLayoutParams());
deleteParam.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(actionEditText.getLayoutParams());
editParams.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams durationParams = new LinearLayout.LayoutParams(durationEditText.getLayoutParams());
durationParams.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams line = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,TypedValue.COMPLEX_UNIT_SP, 1);
line.setMargins(0,15,0,0);
final LinearLayout newLinear = new LinearLayout(this);
newLinear.setLayoutParams(lparams);
final LinearLayout combined = new LinearLayout(this);
combined.setLayoutParams(lparams);
final Button plus = new Button(this);
plus.setLayoutParams(btnParam);
final Button minus = new Button(this);
minus.setLayoutParams(btnParam);
final TextView count = new TextView(this);
count.setLayoutParams(textParam);
final EditText editText = new EditText(this);
editText.setLayoutParams(editParams);
editText.setHint("Action To Record");
editText.setGravity(Gravity.CENTER_HORIZONTAL);
final View v = new View(this);
v.setLayoutParams(line);
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
final EditText duration = new EditText(this);
duration.setLayoutParams(durationParams);
duration.setHint("Duration of Action");
duration.setGravity(Gravity.CENTER_HORIZONTAL);
final Button delete = new Button(this);
delete.setLayoutParams(deleteParam);
delete.setText("delete");
delete.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
linearLayoutText.removeView(combined);
}
});
plus.setText("+");
plus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
plus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
counting[0]++;
count.setText(counting[0] + "");
}
});
minus.setText("-");
minus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (counting[0] > 0)
counting[0]--;
count.setText(counting[0] + "");
}
});
count.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);
newLinear.setOrientation(LinearLayout.HORIZONTAL);
newLinear.setGravity(Gravity.CENTER_HORIZONTAL);
combined.setOrientation(LinearLayout.VERTICAL);
combined.setGravity(Gravity.CENTER_HORIZONTAL);
newLinear.addView(minus);
newLinear.addView(count);
newLinear.addView(plus);
combined.addView(editText);
combined.addView(duration);
combined.addView(newLinear);
combined.addView(delete);
combined.addView(v);
counting[0] = 0;
count.setText(counting[0] + "");
return combined;
}
}
I suggest :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
EditText edt= new EditText (this);
edt = LinearLayout.getChildAt(i);
YourAarraylist[i]=edt.getText().toString();
}
If you have multiple views inside your LinearLayout you can repeat the for like this :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
LinearLayout secondLayout= (LinearLayout) LinearLayout.getChildAt(i);
// and then:
EditText edt= new EditText (this);
edt = secondLayout.getChildAt(indexofyoutEdittext);//You should know this because it's the order of the views you add in your layout
YourAarraylist[i]=edt.getText().toString();
}
I hope it helps !

layout_weight programmatically explainedv

I have been looking these past two days on questions regarding setting the weight of a layout or a group of layouts programmatically.
all the answers i found are almost the same so that means i know what code no to use but i do not seem to understand how to assign the float attribute. in the following code.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
would someone please give me an example of how to assign the weights of two TextView with a weight sum of 3???
It depends on your view if you want to split the view horizontally between them you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>
but if you want to split the view vertically you can use something like this.
TextView secondTV = findViewById(R.id.secondTextView);
TextView firstTV = findViewById(R.id.firstTextView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 3);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
firstTV.setLayoutParams(layoutParams);
secondTV.setLayoutParams(layoutParams1);
And your layout looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/icon"
android:text="Hello" />
</LinearLayout>

in android display layout 1 to left and layout 2 to the right [duplicate]

This question already has answers here:
create a chatView layout in android
(4 answers)
Closed 4 years ago.
in android i am trying to change the layout base on who the message is from.
if the message is from me then display the layout mymessage.xml to the right else display message.xml to the left.
i used if condition, but i don't know how to display one layout to the right and the second to the left,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO
View messageView = null;
// Get a reference to the LayoutInflater. This helps construct the
// view from the layout file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Change the layout based on who the message is from
if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.mymessage , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
return messageView;
}
try this layout for mymessage.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/mytimeTextView"
android:alpha="0.5"
android:layout_alignParentRight="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/mytimeTextView"
android:id="#+id/mymessageTextView"
android:layout_alignParentRight="true"
android:text="mymessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and similarly this for message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/timeTextView"
android:alpha="0.5"
android:layout_alignParentLeft="true"
android:text="5:00 pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_below="#+id/timeTextView"
android:id="#+id/messageTextView"
android:layout_alignParentLeft="true"
android:text="yourmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
for message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/receive_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/message_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="#+id/tv_receiver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="User 1"
android:textColor="#color/NavDrawerTextColor"
android:textSize="14dp"/>
<TextView
android:id="#+id/tvmessagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Hi"
android:textColor="#color/MessageText"
android:textSize="14dp"/>
<TextView
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="04:16 pm"
android:textSize="10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/send_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:background="#drawable/message_sender_box"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="visible">
<TextView
android:id="#+id/tvmessagetext_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="user 2"
android:textColor="#color/White"
android:textSize="14dp"/>
<TextView
android:id="#+id/timestamp_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Hello"
android:textColor="#color/White"
android:textSize="10dp"/>
</LinearLayout>
</RelativeLayout>
for java class: getview()
convertView = inflater.inflate(R.layout.message,parent, false);
if (messages.get(position).fromMe()) {
// for reference receive_ll is GONE and send_ll layout is VISIBLE
} else {
// for reference receive_ll layout is VISIBLE and send_ll layout GONE
}
For left.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:background="#drawable/textview"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sampleleft"
android:textColor="#000" />
</RelativeLayout>
For right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/msgr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:background="#drawable/textview"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Sample"
android:textColor="#000" />
</LinearLayout>
just replace this
// Change the layout based on who the message is from
if (messages.get(position).fromMe()) {
messageView = inflater.inflate(R.layout.left , parent, false);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
} else {
messageView = inflater.inflate(R.layout.right , parent, true);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
}
Just make it simple .
create single layout contain two category
set the data's in the fields
And VISIBLE GONE the layout by the type
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/sender_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//sender side
</LinearLayout>
<LinearLayout
android:id="#+id/reciver_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
//reciver side
</LinearLayout></LinearLayout>
if the message type is sender means visible thesender_layout else visible the reciver_layout
when you getting myMessage that time show mymessage layout other layout will hide like make below codition ...
if (messages.get(position).fromMe()) {
myMessageView = inflater.inflate(R.layout.mymessage , parent, false);
myMessageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
msgView.setText(messages.get(position).getMessage());
messageView.setVisibility(View.GONE);
} else {
messageView = inflater.inflate(R.layout.message , parent, true);
messageView.setVisibility(View.VISIBLE);
//initialization of 2 textView, message and time
TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
timeView.setText(messages.get(position).getTime());
TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
msgView.setText(messages.get(position).getMessage());
myMessageView.setVisibility(View.GONE);
}

Set Relativelayout height inside listview

I want to set a relativeLayout height inside a ListView.
This is my code in MenuListAdapter.java
RelativeLayout background = (RelativeLayout) convertView.findViewById(R.id.background_color);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setTypeface(typeface);
if(navDrawerItems.get(position).getType() == MenuType.HEADER)
{
background.setBackgroundColor(context.getResources().getColor(R.color.header_color));
background.setClickable(false);
background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
}else{
background.setBackgroundColor(color.transparent);
}
I'm adding xml for more information of my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:id="#+id/background_color">
<ImageView
android:id="#+id/icon"
android:layout_width="#dimen/spacing_layout_image"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/spacing_margin_text"
android:layout_marginRight="#dimen/spacing_margin_text"
android:src="#drawable/menu_btn"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:textColor="#drawable/menu_text"
android:paddingRight="#dimen/spacing_scrollview"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/notifTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/spacing_margin_text"
android:background="#android:color/holo_red_dark"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:paddingLeft="#dimen/radius_default"
android:paddingRight="#dimen/radius_default"
android:visibility="gone"/>
but i get an error saying that "E/AndroidRuntime(11406): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"
Can someone tell me what's wrong with my code ?
Thank's
It's working, use AbsListView instead of RelativeLayout.
Thank you for Mr.Piyush Kukadiya
Try this :
Change
background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
to
background.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
UPDATE :
RelativeLayout.LayoutParams and AbsListView.LayoutParams are two different classes of android.widget Package.
Here you are replacing existing layout params of correct type AbsListView.LayoutParams with more generic ViewGroup.LayoutParams i.e. RelativeLayout.LayoutParams.

Android dynamically add custom component to LinearLayout

i want add a custom component on a LinearLayout whenever i touch a button.
This is my code:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini);
View custom = vi.inflate(R.layout.custom_list_folder, null);
TextView textView = (TextView) custom.findViewById(R.id.label_pathFolder);
textView.setText(pathDirectory);
Spinner spinnerLevel = (Spinner) custom.findViewById(R.id.spinner_level);
try{
root.addView(custom);
}catch(Throwable e) {
Log.e("BurgerClub", "MEX: " + e.getMessage());
e.printStackTrace();
}
In this way, only first custom component is added, why?
Thanks
edit:
I've modified my code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);
This is my custom component:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom"
android:layout_marginBottom="2dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
style="#style/Theme.BurgerClubStyle" >
<TextView
android:id="#+id/label_pathFolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center"
android:padding="20dp"
android:textSize="25sp"
style="#style/customText" />
<Spinner
android:id="#+id/spinner_level"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_show_desc_img"
android:entries="#array/n_level_array"
android:padding="20dp"
android:prompt="#string/n_level_prompt"
android:textAlignment="3"
style="#style/customText" />
<ImageButton
android:id="#+id/btn_show_desc_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_remove_folder"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_desc" />
<ImageButton
android:id="#+id/btn_remove_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_delete" />
</RelativeLayout>
The first time that i press the button, custom component was added, but all other times NOT.
The first custom element is not overwrite, it's the only visible!
Where's your layout_cartelle_immagini declared?
Probably it's a horizontal LinearLayout, just set orientation to vertical and your code should work (considering the code to add a custom view is inside a onClickListener).
Try this. It will help you.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
root.addContentView(custom, params);

Categories