Complex return in Ternary Operator? - android

I am trying to return a specifically styled date after checking the state of an element but struggling with the exact way to write this.
I have this code for a textview in my XML:
android:text='#{String.format(item.status.toLowerCase().contains("check") ? ("Scheduled for %s", item.ScheduledDate) : ("Published on %s", item.PublishedDate))}'
but it is expecting a +<>=- rather than the ,
Can someone please help me encapsulate this properly?

Unfortunately, you will have to do that from within whichever Java class sets the content view to the xml file in question.
Like so:
TextView tv = (TextView) findViewById(R.id.textview);
String text = (item.status.toLowerCase().contains("check") ? String.format("Scheduled for %1$s", item.ScheduledDate) : String.format("Published on %1$s", item.PublishedDate);
tv.setText(text);

Related

how do i check if the edit text view contains 2 white spaces?

Currently my edit text view checks if the searched term contains one space as follows:
if(mSearchView.getText().toString().contains(" ")
How do I make it such that it makes sure it checks if the searchview contains 2 spaces between 3 search terms for example: "here it is"
You can use a regular expression to do that. Use code like this one:
if(Pattern.matches("^\\w+\\s\\w+\\s\\w+$", mSearchView.getText().toString()))
Also make sure to check if mSearchView.getText() is not null - you probably will get a NullReferenceException with a blank EditText content.
In the end you may want to create a method like this one:
public static boolean containsTwoSpaces(Editable text) {
if (text == null) return false;
return Pattern.matches("^\\w+\\s\\w+\\s\\w+$", text.toString());
}
just for convenience, clearance and making sure you don't bump into a NullPointerException.
See here.
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
int mms=matcher.groupCount();

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...

TextView in customView

I wanted to put a TextView into a CustomView. I had put it by using findViewById. That TextView I had directly inserted into the xml. Now i wanted to add text when a touch event is made.
Inside the ViewActivity I had put this.
public void getTextv(TextView tv)
{
tv1=tv;
}
int hit;
public void setText()
{
if(touch==true)
tv1.setText(hit);
}
Inside the main i had put ViewActivity.getTexttv(tv);
Then i got a error whenever text was added.
Is your error a nullpointerexception maybe?
shouldn't you set touch=true in getTextv(..)?
tv.setText(String.valueOf(hit))
you should parse it to String firstly.
for the setText(int), the parameter should be a string resource.
Use
tv.setText(hit+"");
As textview also takes an integer valuw which it thinks as an id from R.java file. So , you will get the error like :
Resourse id 0x02 was not found

android (change string in java code)

In the /res/values folder of my android project i have a string and that is referenced in a text view in my xml file, i want to change the string in my java file.
As you can see below in the code i have made a string variable and then below that i have set what the string variable is set to, which is where the string is located. where i have "here" posed in the code that's where i want to change to string in the values folder. but i don't know what code to use to set it.
I could just change the text in a text view from my java file, which i know how to do, but that is an old way and it sets of a warning so i would rather use a string which is the best way to do so.
With my knowledge of changing text in a text view i have basically guessed my way to this stage but i don't know how to go any further could any one give me some advice on what to do, thanks.
String string;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.badd);
sub = (Button) findViewById(R.id.bsub);
reset = (Button) findViewById(R.id.breset);
display = (TextView) findViewById(R.id.tvdisplay);
string = (String) getString(R.string.counter);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((///////////////here////////////////))
counter++;
}
});
You cannot modify the text assigned to <string> elements of a /res/values/strings.xml file at runtime. They're constants so effectively final.
You also cannot change a layout xml file at runtime. If you've created a layout with a TextView that has its android:text attribute set to some initial resource string, that is basically an 'initial' value and cannot be changed to something else at runtime.
You told us a lot of changing text, but you don't said what the text should be. I need to guess, too:
The strings.xml file should be used for texts that might change for different languages. If you just want to change the text of a counter, you shouldn't do it via strings.xml as the numbers are universal :)
Try to go with that:
display.setText(String.valueOf(counter));
You will want to use the setText() method.
display.setText("text");

Two problems about adding HTML to a textView in a ListView

In my work, I have to add custom links (use custom tag) in each listview item.
I met two problems and have searched by Google and stackoverflow, but no result...
Here is the code segment and I process the tag with TagHandler.
TextView tv = (TextView)paramView.findViewById(R.id.tv_dynamics_desc);
tv.setClickable(true);
tv.setText(Html.fromHtml("<uc id=\"133\">This is a Uc link</uc>", null, this));
tv.setMovementMethod(LinkMovementMethod.getInstance());
The first problem is how to get the attribute id.
#Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if(tag.toLowerCase().startsWith("uc")) {
if (opening) {
//String id = xmlReader.getProperty( "id" ).toString(); <=== to get the attribute, but failed with exception of no such property.
startClick(tag, output, xmlReader);
} else {
endClick(tag, output, xmlReader);
}
}
}
xmlReader.getProperty( "id" ).toString(); does not work here.
So I change the tag to "uc+id", such as uc133, and this solve the problem, although a little ugly.
Is there any way to get the attribute directly?
The second is the listview item can NOT receive the touch event, no matter I touch the links or the other area of the item.
Artem Russakovskii said he also met this problem in a comment here.
I'm from China, so sorry for my poor English.
Thanks very much:)
I got the same problem and I solved the first problem like this:
final String html = "萝卜白菜的博客<img src='http://m3.img.libdd.com/farm3/115/BBE681F0CAFB16C6806E6AEC1E82D673_64_64.jpg'/><mytag id='123' color='blue'>自定义</mytag>";
Using your way, I got nothing:
final String color = (String) xmlReader.getProperty("mytag123");

Categories

Resources