I'm adding in a TextView programatically to a linear layout. I can see the background image but for some reason the text is never displayed. Any ideas? Here's the code:
TextView pointsTV = new TextView(getApplicationContext());
pointsTV.setText("Test Should Show");
pointsTV.setGravity(Gravity.CENTER);
pointsTV.setTextColor(android.R.color.white);
pointsTV.setPadding(0, 20, 0, 20);
pointsTV.setBackgroundResource(R.drawable.gamesummary_bottom);
pointsTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ll.addView(pointsTV);
You're setting the colour to a numeric value, which causes it to disappear. Use
pointsTV.setBackgroundResource(android.R.color.white);
In order to use setTextColor(), you have to give it a statelist or hex instead of a colour value.
The salient bit of the developer guide is here.
Related
I have dynamically added TextView depending on the number of elements. Here is my pseudo code:
FlowLayout.LayoutParams lparams = newFlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(getApplicationContext());
tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.tagsColor));
tv.setLayoutParams(lparams);
tv.setText("#" + (post.getTags().get(i).toString()));
tv.setPadding(20,0,20,0);
But when I add background color, the color is set on all text and the padding. I want to know how to fix this.
Here is my current Output
enter image description here
Padding is part of the View so the background color will be present there as well. You need to add margin/spacing between your TextViews.
I think you are using https://github.com/nex3z/FlowLayout this lib for the FlowLayout. According to the specs, you can add app:flChildSpacing="16dp".
Or, you can add margin to the layout params.
FlowLayout.LayoutParams lparams = newFlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
lParams.setMargins(16, 0, 16, 0);
Edit:
Setting margins should've worked.. if you can't here's a trick, if you need:
textView.setBackgroundColor(getResources().getColor(R.color.colorAccent))
val gap = "<span style=\"color:blue; background-color:blue;\">gap</span>\n"
val text = Html.fromHtml("#PHP " + gap + "#WEB " + gap + "C# ")
textView.text = text
Set color accordingly.
Result:
If you want to change text color not the background/padding, use this:
String tags = post.getTags().get(i).toString();
tv.setText("#" + Html.fromHtml("<font color='#EE0000'>" + tags + "</font>"));
instead of ContextCompat.getColor(getApplicationContext(), R.color.tagsColor) you can directly paste your color code at color="#"
Or
you can use setTextColor() pointed out by #PPartisan
tv.setTextColor(getResources().getColor(R.color.tagsColor))
I think your question needs more clarity about what your requirement is. From what we understood you need background only behind the text and spacing around the textview.
As #Froyo already mentioned in his answer setting Padding will cause the background to be applied in the padding space. Therefore in order to add spacing without applying the background you need to add Margins instead.
The problem is FlowLayout extends a ViewGroup so in order to set margins you need to get MarginLayoutParams instead of the usual LayoutParams. So write your code as shown below:
FlowLayout.MarginLayoutParams lparams = new FlowLayout.MarginLayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(20, 0, 20, 0);
TextView tv=new TextView(getApplicationContext());
tv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.tagsColor));
//even though getApplicationContext() works I think you should make use of the activity/fragment context instead.
tv.setLayoutParams(lparams);
tv.setText("#" + (post.getTags().get(i).toString()));
Setting margin won't apply the textview background you are setting, so perhaps this is what you were looking for. Let us know if this worked or were there some other issues.
Basically you are setting the background for whole text. Not for the word inside your text.
To do this, you have to make a custom Spannable. See my current UI i'm working on.
I have written a code to change the minWidth of a button so that I can get rid of the padding around the button text.
for(int i=0;i<26;i++){
Button alphabet = new Button(getContext());
alphabet.setText(""+(char)('A'+i));
alphabet.setMinWidth(0);
allAlphabets.addView(alphabet);
}
I am having a letter in each button and finally adding it to a layout dynamically. Due to padding, it is taking up much space, because of which I have set the minimum width to 0, but still it doesn't work. It shows the same default button.
Please help.
as you want a clickable view look like button with text
you can make a xml layout with your button or a cardview and textview inside it
then add it like this:
Button alphabetButton = LayoutInflater.from(context).inflate(R.layout.alphabet_layout);
so it's better than code design it's now a layout so you can modify the layout as you want
also you can add to your button in alphabet_layout.xml
android:background="#null"
You have to apply LayoutParams with your Button alphabet.
pls try set padding of button like
Button alphabet = new Button(getContext());
alphabet.setText(""+(char)('A'+i));
alphabet.setPadding(0,0,0,0);
allAlphabets.addView(alphabet);
You can set using below code :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);
This is the code used to generate the button
Button delBtn = new Button(activity);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
delBtn.setLayoutParams(params);
delBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_action_cancel, 0);
delBtn.setBackgroundColor(Color.rgb(208, 0, 0));
Result Looks like this:
Whereas I expect:
`
What is it that I missed?
You can use Imageview for this purpose as button has some padding value. And if the image has a background, you can make it transparent by:
imageButton.setBackgroundColor(android.R.color.transparent);
You can try this:
Try Using ImagView instead of Button.
Set your image in "src" attribute.
Set background null/transperant.
As, suggested by Mansi (as comment, above), a simple TextView did the work!
I have created marquee textview by giving properties in xml. but when I create textview on runtime with the same property then the text does not scroll. I have written below code. Has anybody face this problem?
TextView textView = new TextView(this);
textView.setText("This is marquee text it should scroll");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setFocusableInTouchMode(true);
textView.setFreezesText(true);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.setFocusable(true);
textView.setSelected(true);
I tested your code on my Galaxy Nexus (working on Jelly Bean), and it works fine. Could you give more detail information about your test setting?
Ah, one thing I added is layout param to ensure the width of view is shorter than text itself.
textView.setLayoutParams(new ViewGroup.LayoutParams(100,
ViewGroup.LayoutParams.MATCH_PARENT));
Animation mAnimation = new TranslateAnimation(START_POS_X, END_POS_X,
START_POS_Y, END_POS_Y);
mAnimation.setDuration(TICKER_DURATION);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
TextView tvTitulo = new TextView(this);
tvTitulo.setText("Some loooooooooooooooooooooong text");
tvTitulo.setAnimation(mAnimation);
jUST CHECK this code .....
I'm generating dynamicaly a Test based questionary.
The generated views are shown with a kind of grey color, that makes them near invisible. I can change the color of the textViews and the problem is solved, but I can't change the color of the circle from the radioButton.
I don't know why it's not the same color as the xml radioButtons.
Here is my code:
LinearLayout layout = (LinearLayout) findViewById(R.id.loaded_exercice);
Context tContext=getApplicationContext();
for (int i=0;i<t.getPreguntes().length;i++){
TextView tv = new TextView(tContext);
tv.setText(t.getPreguntes()[i].getFrase());
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
layout.addView(tv);
RadioGroup RG=new RadioGroup(tContext);
for (int j=0;j<t.getPreguntes()[i].getRespostes().length;j++){
RadioButton tv2 = new RadioButton(tContext);
tv2.setAlpha(1.0f);
tv2.setText(t.getPreguntes()[i].getRespostes()[j].getResposta());
tv2.setTextColor(Color.RED);
RG.addView(tv2);
}
RG.setAlpha(1.0f);
layout.addView(RG);
}
Does anyone know how to easily change the color of the circle or make the generated views shown with a "normal" color?
Thank you very much!
I found the solution. Just not using the Context and create all the views like this one:
RadioButton tv2 = new RadioButton(this);
Just replacing tContext for this