TextView background all colored - android

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.

Related

Why are these buttons mis-aligned in a TableLayout?

I'm using a table layout to arrange some buttons. As long as I use the same font for all the labels they are properly aligned in each row.
For some buttons I'd like to use icons from a custom ttf font.
When I use such an icon, the button is placed slightly higher, like so:
(This image is scaled up to make the the problem more evident.)
I took measurements - the buttons appear to be of same height, regardless of the used font.
Why are the buttons not aligned properly?
Does anyone have a suggestion to get them aligned?
Thanks.
Following CommonsWare's advice (thanks for the quick replies!), I tried this:
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_VERTICAL;
row.setLayoutParams(layoutParams);
This did not work. Will try the base alignment comment next.
Add the following attribute to your TableRow:
android:baselineAligned="false"
By default, the button labels' base lines are vertically aligned which causes the offset you experience.

Building a simple Group Chat View in Android

I'm building a very simple online chat room App. What I have achieved is something like below right now:
Robert:blah..
Tom: yes blah..
David(You): That sounds cool!
Lily: Do you know blah...
Robert:blah..
Robert:blah..
David(You): Wow! Blah...
The new feature/problem I'm facing is that I want current user's talks to show on the right.
Like below(this is what David see on his screen):
Robert:blah..
Tom: yes blah..
That sounds cool! : David(You)
Lily: Do you know blah...
Robert:blah..
Robert:blah..
Wow! Blah... : David(You)
Each line above is a TextView, and I'm dynamically creating TextView whenever there's new message, then adding them to the LinearLayout that contains these TextView. In order to make David(current user)'s talk on the right, I tried to set align right and I change the LinearLayout to RelativeLayout. But then I realize once I use RelativeLayout, all talks are in the same line overlapping each other since I didn't set their height.. Can anyone shed some light on how to achieve this please? My codes below:
...
//new messages are stored in lines[]
for (int i = 0; i < lines.length; i++) {
TextView newLine = new TextView(getBaseContext());
newLine.setText(lines[i]);
// check if the speaker of this line is user himself
if (speakerName.equals(userName)) {
//change the layout of this textView to make it align right..
}
myLinearView.addView(newLine);//add to linearView that contains these talks
}
You definitely need to use a ListView, as Chor WaiChun mentioned. However, the way you would do this with a relative layout would be to set the RelativeLayout.layoutParams for the view with a rule that sets it to layout_below the previous view. Just like you would in XML.
Also, even if you insist on adding 1 view per line (which as previously stated is wrong, use a ListView), there's no reason not to use the LinearLayout. You can have a LinearLayout with right justified text, just set the gravity to right.

Linearlayout programmatically - How to set up a divider?

I am creating TextViews in LinearLayout programmatically and I would like to separate them with a divider (just a simple line). I have googled endlessly, what I have found is that I can use .setDividerDrawable, but I don't want to use external images for this.
Any tips?
How to Add Divider to an Android Layout Programmatically
Create a View 1 or 2 pixels tall and width match_parent and set the background color to whatever color you want the divider to be.
Separate the divider from the items above and below with margin settings.
Example:
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);
You could use a simple drawable in xml for the divider (example here), or use a 9-patch image which barely takes anything.
Then, use the LinearLayoutICS in order to show the divider on most of the devices. you can check out this post i've made about it.
For linear layout you can use this attribute to set divider android:divider="some color"
android:showDividers="middle"

Android - Add Lines to Textview

Ive got a TextView and some String Arrays. I want to add a Line (trough Java) everytime the next Item is displayed(Ive got a for loop, so it loops until every Item is displayed in the TextView). Does anybody know how to do that?
Thank you
EDIT:
I want It to look like a ListView. But I dont want to use a listView insted because I want to set the Line just for certain events.
for(int i=0;i<lenght;i++)
{
//textview is tv
tv.setText(listOfString[i]);
//view is viewWithHorizontalLine with hight = dp and width = fill_parent and background = #000000
viewWithHorizontalLine = view;
//LinearLayout is ll
ll.addView(tv);
ll.addView(viewWithHorizontalLine);
}
XML
<LinearLayout>
<ScrollView>
<LinearLayout orientation=vertical/>
</ScrollView>
</LinearLayout>
What kind of line? If you just want a vertical line in your text, add "|" between each word. If you want a new line, use "\n".
Have you tried adding a newline (\n) character each time you add one of your strings?

Adding in a TextView programatically shows no text

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.

Categories

Resources