Android ArrayAdapter Style - android

My question is simply how would I changed the background color (or text color) of the first 6 characters of every cell in my listview ?
(the first 6 chars contain the date. Example “Jan 01: Blah blah blah data data… ” )
I have one listview on my page and it gets populated dynamically from a SOAP webservice call
SoapObject oResponse = (SoapObject)soapEnvelope.getResponse();
ArrayList<String> oStringList = new ArrayList<String>();
for(int i = 0; i < oResponse.getPropertyCount(); i++)
oStringList.add(oResponse.getProperty(i).toString());
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, oStringList ));
Any guidance would be appreciated.

when setting the text of your TextView, you'll use a Spannable so you can change the color of a span. here's an example setting the foreground color to red for six characters. you'll use this code in getView and tv is the TextView for that particular cell.
TextView tv = ...
String text = "Jan 01: Blah blah blah data data...";
int startColor = 0;
int endColor = 6;
int color = 0xffff0000;
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(color), startColor, endColor, 0);
tv.setText(spannable);

dldnh's answer is right, but another way would be to have to TextView's next to each other, one for the date and one for your actual text. So something like this:
<LinearLayout android/+id="rowLayout" android:width="wrap_content" android:height="wrap_content" android:orientation="horizontal">
<TextView android/+id="txtDate" android:text="Jan 01" android:textColor="#color/red" android:width="wrap_content" android:height="wrap_content"/>
<TextView android/+id="txtItem" android:text="blah blah blah" android:layout_marginLeft="5dp" android:width="wrap_content" android:height="wrap_content"/>
</LinearLayout>
So in your Adapter, override getView(int position, View v) (I am pretty sure that's what it was) and you can set the text in there.

In think you should make your own ArrayAdapter layout.
In this new layout, each data of listview has two part: (two textview) and you put it in horizontal linearlayout. first textview, you make its attribute like what you say.

I will give you a simple solution for this. Just write down code to change the color for a block of text in html. some thing like
String s = "<text color=red>abcdef</text>"+"continuition part will be placed here";
TextView Object.setText(Html.toHtml(s));
I don't know the exact code in Html to change the text color exactly. Just google it.
This will solve your issue.

Related

Tag design in android using single textview and array of data

I am new to android programming and I want to achieve this in my android app:
My JSON is this:
"Keywords":["medical","science","international"],
and my Android code is this:
String keyStr = "";
for (String str : m.getKeywords())
{
keyStr += str + " ";
}
When I put it into my TextView, it shows the results like:
medical science international
But, I want to give it the effect it is shown in the above picture, which I did in my website. One solution was to use HTML.fromhtml() but it is not working for me, I need any other solution for it.
Assuming that each tag could be remove with the cross on their right, you have to create multiple TextViews and each ImageViews should have onClickListener.
From my point of view, it could be a lot of tags rendering in a small width screen, so I'd use a HorizontalScrollView. So you could create a container:
<HorizontalScrollView
android:id="#+id/horizontal_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/tags_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" />
</HorizontalScrollView>
And regarding the number of tags, do a loop to create dynamically each tag, as follows:
LinearLayout tagsContainer = (LinearLayout) findViewById(R.id.tags_container);
for (String keyword : m.getKeywords()) {
// inflate the child view by using a custom layout
View tagView = LayoutInflater.from(this).inflate(R.layout.tag_layout, null);
TextView tagTextview = (TextView) tagView.findViewById(R.id.tags_textview);
ImageView tagImageview = (ImageView) tagView.findViewById(R.id.tags_imageview);
// set the elements value
tagTextView.setText(keyword);
tagImageView.setOnClickListener(new View.OnClickListener() {
// close the tag
}
// add to the container
tagsContainer.addView(tagView);
}

How to change a strings color?

I want to change this string to the color red
String isFalse = False;
For some reason every tutorial seemed more complicated than I expected and I don't understand them. Is there a simple way to do this? Also, would this override the color of a textview? Because I would like it to.
String is not View, so it has no color at all.
Maybe what you want is to change the appearance color of it host TextView. To achieve this you can use:
TextView text;
//the initialize of this TextView
text.setText(isFalse);
text.setTextColor(Color.Red);
The parameter of the color could be resource from your color XML values file or android.R.color resource file, or from Color class, etc.
Please use this code.
SpannableStringBuilder builder = new SpannableStringBuilder();
String isFalse = "False";
SpannableString redSpannable= new SpannableString(isFalse);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, Tru_score.length(), 0);
builder.append(redSpannable);
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(builder, BufferType.SPANNABLE);
It is not complicated or hard
Addressing both possibilities..
You want to change the color for your text at runtime.
that would be like TextView.setTextColor()
i.e.
falseTextView.setTextColor(getResources().getColor(R.color.red))
if you wanted a segment of the same textview to have a different color,by which I mean that the isFalse string is only apart of the content of your TextView ,you need to use as mentioned in the other answer.
Try this...
string.xml
<string name="isFalse"><![CDATA[<b><font color=#FF0000>False</b>]]></string>
MainActivity.java
TextView textView1 = (TextView)findViewById(
R.id.textView1);
textView1.setText(Html.fromHtml(isFalse));
And result, you might get like this...

Select text from group of views in listview

Is it somehow possible to achieve that?
In example: we have listView with 20 items, every item has some text. User want to select half of ending text from item 1. and the half of another item text (same behaviour like in webView or selectable textView). Did someone think about that feature? Where should I search the solution?
This topic will be updated when solution will be found.
ps. I know you will say "show us code first". I do not have it yet.
In your istview on item click listener code like this
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
// TODO Auto-generated method stub
TextView tv;
String text_from_tv, finalText;
tv= (TextView) view.findViewById(R.id.textview_id);
text_from_tv = tv.getText();
if(!firstPartSelected)
finalText += text_from_tv.substring(0,text_from_tv.length()/2);
else
finalText += text_from_tv.substring(text_from_tv.length()/2,text_from_tv.length());
//Save this finalText String to any string array and use those values
}
});
and that string array contains the second halfs of the selected word, if it helpful up vote the answer!!
In your list_item.xml for your list view. You will want to set,android:textIsSelectable="true" and make android:clickable="true" for that same item.
I won't give any code, just how I would do it :
boolean firstPartSelected false;
String finalText ="";
// ListView Definition
// OnItemClickListener
OnItemClick(...){
if(!firstPartSelected)
finalText += TextFromItem.substring(0,TextFromItem.length()/2)
else
finalText += TextFromItem.substring(TextFromItem.length()/2,TextFromItem.length())
}
This is not some real code, just an idea of how to implement it. Is that what you wanted ?
Try handle OnFocusChangeListener for your EditText, when focus will be change, color selected text in EditText using spans (Android: Coloring part of a string using TextView.setText()?). If you have large count of EditText you can use view.setTag(etNumber) and (Integer)view.getTag() for each EditText and use this information while concat output string in loop (look for more info Android - get children inside a View?)
P.S. EditText is inheritor of TextView, what yo can do with TextView you will can do with EditText

Hide text only but not whole textview

My goal to hide text and keep textview occupying its space in the activity , i set already my text to be invisible by using :
tv.setVisibility(View.INVISIBLE);
and when button clicked it show the text , every thing work fine except the result of my code lead to hide whole textview not only the text , because i set my textview background to drawable shape which form red border around the text as:
android:background="#drawable/border1"
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border1"
android:textSize="20sp" />
when start app you can see empty space only which will be fill with text after button click but there's no border there (which come from shape background) , so it hide whole textview and i need it to hide only text and keep the textview with its background shown when text set to INVISIBLE,
any help will be really appreciated ,thanks.
this is how i did it:
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
tv11.setVisibility(View.INVISIBLE);
then after click the but and write correct password it show the text as:
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
EditText password = (EditText) dialog.findViewById(R.id.password);
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
tv11.setVisibility(View.VISIBLE);
}
Transparent text color hides the text:
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border1"
android:textColor="#android:color/transparent"
android:textSize="20sp" />
When you want to show your text, change the text color programmatically using method setTextColor():
tv11.setTextColor(color);
// try this way
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border1">
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
you can make simple trick : write two string
lets say in your first and second piece of code just remove this line
tv11.setVisibility(View.INVISIBLE);
and
tv11.setVisibility(View.VISIBLE);
so it will be
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
then in second piece write as follow ;
if( password.getText().toString().equals("test")) {
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one_appear)));
}
where first string will be empty
<string name="introclusion_one">
and second string you will write your text in it
<string name="introclusion_one_appear">
Hope help you .
save what is in the text view as a string like this:
String x = (String)tv11.getText();
then make the text view empty like this:
String x = "";
for(int i = 0; i < x.length(); i++){
x +=" ";
}
tv11.setText(x);
to make the textview visible again do:
tv11.setText(x);

TextView: how to dynamically change the color of certain word in paragraph

How to change the color of a paragraph word by word with timer. like at first second i want to change color of The in the below paragraph, after 5th second change color of text, after 8th second change color of will be and so on....
The text will be wrapped in tags, and displayed in a monospaced font.
just use Timer and change the font color of your edit text accordingly and stop timer in focus lost.
i think you can do something like this :
Split the paragraph to words by using the method :
split(String separator);// it will return an array of Strings
//in your case you will do somthing like this
myWords = paragraph.split(" ");// the separator is the space
And then , you can use the method to colorate what ever you want of those words by using the method :
myNewParagraph.append(HTML.fromHtml("<font color...>... Your HTML Code to colorate your Text"));
and when you finish coloring each word , you update your textView to display the new text colored
Hope it helps
You can use spans to control the appearance of text. Take a look at Spannable and CharacterStyle.
Here is an example. Of course you would need to put this in some sort of timer.
Spannable spannableText = getSpannableText(yourTextView);
spannableText.setSpan(new TextAppearanceSpan(...), wordStart, wordEnd)
yourTextView.setText(spannableText);
private Spannable getSpannableText(TextView tv) {
CharSequence cs = tv.getText();
if (cs instanceof Spannable) {
return (Spannable)cs;
} else {
return SpannableString.valueOf(cs);
}
}

Categories

Resources