I'm writing a chat program. In chat activity, my sent and received messages are displaying with EditTexts like bubbles. I want to set my width of edittexts with the same width of its messages. In my code, when message is created, new edittexts are being created programmatically in the relativelayout. How can I set it?
public void sendmyMessage(String msg){
// mRR is my RelativeLayout
RelativeLayout.LayoutParams params;
EditText e = new EditText(mRR.getContext());
e.setId(arr.size()+2);
e.setEnabled(false);
e.setFocusable(false);
e.setClickable(false);
e.setText(msg);
e.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
e.setTypeface(null,Typeface.BOLD);
e.setTypeface(Typeface.SANS_SERIF);
e.setTextColor(Color.parseColor("#080808"));
params = new RelativeLayout.LayoutParams(setDp(150, e.getContext()),RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, setDp(8, e.getContext()), 0, 0);
if(arr.isEmpty()){
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
}else{
params.addRule(RelativeLayout.BELOW,arr.get(arr.size()-1).getId());
}
Spannable str = e.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
e.setLayoutParams(params);
e.setBackgroundResource(R.drawable.mytext);
arr.add(e);
mRR.addView(e);
}
The function is this. I don't want to set the width fixsize. What is the solution?
something like this..?
editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Related
The code below creates a varying amount of textViews at run-time depending on the number of keys in friendMap:
generatedViews = new TextView[numberOfFriends];
int count = 0;
for (String k : friendMap.keySet()) {
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setText(k);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
textView.setGravity(Gravity.CENTER);
String mutual = friendMap.get(k);
if (mutual.equals("no")) {
textView.setBackgroundColor(Color.RED);
} else {
textView.setBackgroundColor(Color.GREEN);
}
linLayout.addView(textView);
generatedViews[count] = textView;
count++;
}
My question is: how can I set constraints to the created textViews so they don't bunch?
It's simple:
Set the layout parameters of the LinearLayout with:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(100, 20, 100, 20);
linLayout.addView(textView, layoutParams);
As #TheWandered said you should probably use RecyclerView.
But if for some reason it's not an option, here is how you can do it:
First thing, you are going to have to generate ID for each TextView, so you can target the constraints and then:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(text_view_id,ConstraintSet.TOP, previous_text_view_id,ConstraintSet.BOTTOM,0);
// is equal to "layout_constraintTop_toBottomOf="previous_text_view_id"
constraintSet.applyTo(constraintLayout);
I am trying to create a textview and a button programmatically in the existing relative layout. The idea is to put the textview in the left corner of the parentView(relativeLayout) and add then the button to the right of the textView. But in the app it looks like they are on the one place. The button is in front of textView, not on the right. Please, give me some advice.
the code:
TextView textView = new TextView(getActivity().getApplicationContext());
textView.setText("...");
textView.setTextColor(Color.GRAY);
int id = 0;
textView.setId(id);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.setMargins(16, 16, 0, 0);
textView.setLayoutParams(params);
notificationContentLayout.addView(textView, params);
Button customerButton = new Button(getActivity().getApplicationContext());
customerButton.setText("...");
customerButton.setTextColor(Color.parseColor("#00b3ff"));
customerButton.setBackgroundColor(Color.TRANSPARENT);
id = id + 1;
customerButton.setId(id);
final RelativeLayout.LayoutParams paramsForButton =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsForButton.addRule(RelativeLayout.RIGHT_OF, textView.getId());
paramsForButton.addRule(RelativeLayout.ALIGN_PARENT_TOP); // with or without that rule everything is the same
// paramsForButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
paramsForButton.setMargins(10,0, 16, 0);
customerButton.setLayoutParams(paramsForButton);
notificationContentLayout.addView(customerButton, paramsForButton);
For RelativeLayout.LayoutParams rules, 0 means false, and only applies to rules that don't refer to sibling Views, such as CENTER_IN_PARENT. Since you've set your TextView's ID to 0, the RIGHT_OF rule you're adding is being ignored, as false doesn't make sense with that.
To remedy this, simply set the TextView's ID to any positive int value; e.g., 1.
I'm trying to create a TextView programmatically in this way:
TextView textDescription = new TextView(this);
LinearLayout.LayoutParams layoutParamsDesc = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 25);
layoutParamsDesc.setMargins(10, 0, 10, 0);
textDescription.setLayoutParams(layoutParamsDesc);
textDescription.setText("Lengthy text in TextView");
linearLayoutDatos.addView(textDescription);
textDescription.setEllipsize (TextUtils.TruncateAt.END);
textDescription.setMaxLines(1);
textDescription.setHorizontallyScrolling(false);
However, no "three dots" are displayed.
What am I doing wrong?
Thanks in advance.
You wrote textDescription.setMaxLines(1); but the proper usage for it to work is like below:
For single line
textDescription.setSingleLine(true);
For multiple lines (more than 1)
textDescription.setMaxLines(2);
An the other thing is, write this line after you do all your settings with textDescription.
linearLayoutDatos.addView(textDescription);
So in the end it will be like this:
TextView textDescription = new TextView(this);
LinearLayout.LayoutParams layoutParamsDesc = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 25);
layoutParamsDesc.setMargins(10, 0, 10, 0);
textDescription.setLayoutParams(layoutParamsDesc);
textDescription.setText("Lengthy text in TextView");
textDescription.setEllipsize (TextUtils.TruncateAt.END);
textDescription.setSingleLine(true);
textDescription.setHorizontallyScrolling(false);
linearLayoutDatos.addView(textDescription);
Add textDescription.setSingleLine()
You must set a maxLength property of the TextView. You can do that like this:
TextView textView = new TextView(this);
int maxLength = 25;
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(maxLength);
textView.setFilters(filterArray);
Try this...
textDescription.setEllipsize (TextUtils.TruncateAt.MARQUEE);
I use
appMusicTab.setButtonDrawable(R.drawable.all);
to set the RadioButton's button style,
But the drawable is always algin to the left of RadioGroup, How can I set a left padding to the 'button' in Code? Thanks!!
I've tried
appMusicTab.setPadding(20, 0, 0, 0);
And:
appMusicTab.setLeft(20);
But those do not worked.
This is my code:
RadioButton appMusicTab = new RadioButton(mContext);
appMusicTab.setText(tabInfo.name);
appMusicTab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
appMusicTab.setGravity(Gravity.CENTER);
appMusicTab.setId(tabInfo.id);
// appMusicTab.setButtonDrawable(android.R.color.transparent);
appMusicTab.setButtonDrawable(R.drawable.all);
appMusicTab.setBackgroundResource(R.drawable.app_tab_bg);
appMusicTab.setPadding(30, 0, 0, 0);
RadioGroup.LayoutParams lp =
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
lp.bottomMargin =MASettings.MAIN_UI_APP_TAB_MARGIN_BOTTOM;
mAppsTab.addView(appMusicTab, lp); //mAppsTab is a RadioGroup
I used a icon with some transparent paddings to solve this problem. I checked RadioButton source code and found that the left padding of button Drawable has been set to 0 in code: buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
For that you have to set the LayoutParams.
See below:
qButt = new Button(this);
qButt.setPadding(0, 0, 0, 0);
TableRow.LayoutParams params = new TableRow.LayoutParams(BtnHeight,BtnHeight); // You can set any Layout params in Place of TableRow
YourParentLayout.addView(qButt,params); // Your parent layout may be any Layout in xml
hi i've tried to add margin programmatically
i've tried using
TextView tv = new TextView(this);
LayoutParams params = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.setMargins(100, 20, 0, 0);
layout.addView(tv, params);
which uses android.widget.LinearLayout.LayoutParams, but because of the table, i got runtime error for it.
how should i do it then ?
this is my code :
//create table row
TableRow trA = new TableRow(this);
trA.setId(1000+i);
trA.setLayoutParams(new LayoutParams (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//add data to table row
TextView tvA = new TextView (this);
tvA.setId(2000+i);
tvA.setText(aList.get(i).toString());
tvA.setTextSize(18);
tvA.setTextColor(Color.BLACK);
tvA.setClickable(true);
tvA.setOnClickListener(this);
tvA.setSingleLine(false);
tvA.setHorizontallyScrolling(false);
tvA.setWidth(0);
tvA.setHeight(100);
trA.addView(tvA);
tableA.addView(trA,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
== edited with import android.widget.TableLayout.LayoutParams; ==
i added this at the end where i declare the tablerow
LayoutParams params = new LayoutParams (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 20);
and edited the addView at tableA
tableA.addView(tvA, params);
i have error in the log cat, it display quite alot, so i gave the main one here : The specified child already has a parent. You must call removeView() on the child's parent first.
TableRow.LayoutParams params = new TableRow.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.setMargins(100, 20, 0, 0);
You could try using TableRow.LayoutParams instead.
So change your import to
import android.widget.TableRow.LayoutParams