I have a java code for an apk. I want to change the color from java activity
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"User : "+json.getString("username")+"\n"+
"Send : "+json.getString("time")+
" & "+json.getString("date")+"\n"+
Now, I want to set 2 different color, for the "user" one color and json.getString("username") a different color as well as font weight, bold or not.
and my XML code is
<TextView
android:id="#+id/result"
android:background="#2E2EFE"
android:layout_width="match_parent"
android:textColor="#F3F781"
android:textSize="24dp"
android:layout_height="wrap_content"
android:paddingLeft="20sp"
android:text="TextView"/>
But I do not want to change the color from XML
Related
I have a HTML string like this:
Some text %1$s
<br></br>
StackOvewflow
<br></br>
Contact: example#gmail.com
How to display it so it shows the line breaks and links to the text properly in a TextView?
I have tried both
Spanned text = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
text = Html.fromHtml(getString(R.string.text, "parameter"), Html.FROM_HTML_MODE_LEGACY);
}
else
{
text = Html.fromHtml(getString(R.string.text, "parameter"));
}
textView.setText(text);
and
String text = TextUtils.htmlEncode(getString(R.string.text, "parameter"));
textView.setText(text);
In the XML textView is defined as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text"
android:text="#string/text"
/>
When displayed, it just looks like
Some text parameter StackOvewflow Contact: example#gmail.com
without any line breaks or links
I used the same code you shared
Spanned text = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
text = Html.fromHtml(getString(R.string.text, "parameter"),
Html.FROM_HTML_MODE_LEGACY);
}
else
{
text = Html.fromHtml(getString(R.string.text), "parameter");
}
textView.setText(text);
seems to be working fine for me.
seems to be working
I have several error messages in an Android Studio app using Java and I cannot figure out how to customize their appearance. I have consulted several answers on this site but none of them have worked for what I want.
This is what the error message looks like.
I want to change the colors of the black background, the white text, and the red line and icon.
This is the code for the error message,
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(email.isEmpty()){
editTextEmail.setError("Please enter an email.");
editTextEmail.requestFocus();
return;
}
and this is the code for the editText object.
<EditText
android:id="#+id/login_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:hint="Email"
android:padding="8dp"
android:drawableLeft="#drawable/email_icon"
android:textColor="#417577"
android:textSize="25dp" />
How do I change the colors displayed in the error message? Also, is there a way to set these color changes for the whole app and not just individually?
Thank you!
After googling little bit I found some answers for you. Here I am implementing those sites.
devdeeds git repo
findnerd
xspdf
I am implementing that source code which looks like good for me
It is easy way to change color. If you have idea of html than you might see here you are just changing color by html format.
editText.setError(Html.fromHtml("<font color="#000000">"error!"</font>"));
Following source code used in java
int errorColor;
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
errorColor = ContextCompat.getColor(getApplicationContext(), R.color.errorColor);
} else {
errorColor = getResources().getColor(R.color.errorColor);
}
String errorString = "This field cannot be empty"; // your error message
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(errorColor);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(errorString);
spannableStringBuilder.setSpan(foregroundColorSpan, 0, errorString.length(), 0);
editTextView.setError(spannableStringBuilder);
I am working on Watson Conversation chatbot. I have been trying to implement the 'options' response type in my chatbot application. My problem is "Creating 'n' number of dynamic buttons in Android where n is the number of label names of options present in the backend(IBM Watson Conversation)"
I have been able to retrieve the label names in form of text. Now I have to put these label names in "clickable buttons". Such that when a user clicks on a button, a value is passed to the backend (Watson Conversation API).
This is how I am retrieving option(response type) from backend. Watson Conversation sends reply in form of JSON.
Label name retrieving code:
str = response.getOutput().getGeneric().get(i).getResponseType();
JSONArray arrayOptions = new JSONArray(response.getOutput().getGeneric().get(i).getOptions());
int j=0; //j is used to count the number of options
while (j<arrayOptions.length()){
final Message outMessage2 = new Message();
outMessage2.setMessage(response.getOutput().getGeneric().get(i).getOptions().get(j).getLabel());
outMessage2.setId("2");
System.out.println(outMessage2);
messageArrayList.add(outMessage2);
j++;
}
Try this to solve your problem
First create a LinearLayout Layout inside xml
<LinearLayout
android:id="#+id/layout_dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
After that, use below code
LinearLayout layout_dynamic =(LinearLayout) findViewById(R.id.layout_dynamic);
for (int i = 0; i < YOURARRAY.length(); i++) {
String label = <Button Name as You Like>;
LinearLayout childLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT LinearLayout.LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
Button btnName = new Button(getActivity());
btnName.setLayoutParams(newTableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
childLayout.addView(btnName, 0);
layout_dynamic.addView(childLayout);}
Hope this will help you.
In my application I want show some text into TextView, and I get this text from server.
I should use just one textView in XML layout, and I should set color for this Texts.
My XML :
<TextView
android:id="#+id/rowExplore_userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/size3"
android:layout_marginTop="#dimen/padding8"
android:layout_toRightOf="#+id/rowExplore_imageCard"
android:fontFamily="sans-serif"
android:gravity="left|center_vertical"
android:text="Adam"
android:textColor="#color/colorPrimary"
android:textSize="#dimen/font14" />
JSON :
"name": "Adam Sandel",
"info": "liked",
"movieName": "SpiderMan",
Java code :
rowExplore_userName.setText(response.getName() + response.getInfo() + response.getMovieName() );
I want set color dynamically such as below image :
How can I it? Please help me
You can use HTML
String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));
A better approach to this is to create a string resource with appropriate HTML tags:
<string name="html_string" formatted="false">
<![CDATA[
<font color="yellow">%s</font> %s <font color="yellow">%s</font>
]]>
</string>
And then get that string with resources:
String htmlString = getString(R.strings.html_string, response.getName(), response.getInfo(), response.getMovieName());
rowExplore_userName.setText(htmlString);
You can use SpannableString if you want to avoid using HTML tags
TextView textview = (TextView)findViewById(R.id.textview);
Spannable wordToSpan = new SpannableString("Your text is here");
wordToSpan.setSpan(new ForegroundColorSpan(Color.BLUE), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordToSpan);
and it is more flexible since you can make more changes to your text (text size, bold/italic, etc)
I used this code:
String colorStr="#FFFFFF" //put your hex color
int color = Color.parseColor(colorStr);
editText.setTextColor(color);
I would have another approach just create one method
public String getColoredString(String pname) {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Spannable wordToSpan = new SpannableString(pname);
wordToSpan.setSpan(new ForegroundColorSpan(color), 0, pname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return wordToSpan.toString();
}
and use above method
rowExplore_userName.setText(getColoredString(response.getName())+getColoredString(response.getInfo())+getColoredString(response.getMovieName()));
this should work.
This question already has answers here:
Android: textview hyperlink
(9 answers)
Closed 8 years ago.
In my android app how can i show hyperlinks as clickable links to open in browser .
For this I am fetching json messages from backend , saving the data in SQLite database of app and then displaying them on screen using TextView -
Fetching json messages using AsyncTask and Progress Dialog -
protected Void doInBackground(Void... params) {
//some code goes here
mMessages = json.getJSONArray(TAG_MESSAGES);
// looping through all posts according to the
// json
// object returned
for (int i = 0, length = mMessages.length(); i < length; ++i) {
JSONObject c = mMessages.getJSONObject(i);
// gets the content of each tag and put in
// database
String content = c.getString(TAG_MESSAGE);
// add field in database and update
db.addFieldInGcm(content);
}
}
In onPostExecute() i am refreshing the screen with all messages saved in database using TextView lblMessage object -
// show messages on screen
TextView lblMessage;
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k) {
lblMessage.append(messages.get(k).toString() + "\n\n");
This is my XML layout for lblMessage -
<TextView
android:id="#+id/lblMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dip"
android:padding="5dip"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="16dip"
android:autoLink="all" ></TextView>
Seems like , XML android:autoLink is not applicable for strings fetched from database .
So , if while displaying messages i use something like -
if( messages.get(k).toString().contains("http://www.") )
How can i change this string in clickable hyperlinks using java ?
Thank you
Use Linkify.addLinks(textView, Linkify.ALL).
This is my working implementation after getting answers -
private void showMessage() {
// TODO Auto-generated method stub'
// show messages on screen
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k) {
String message = messages.get(k).toString();
lblMessage.append(message + "\n\n");
}
Linkify.addLinks(lblMessage, Linkify.ALL);
}
and to change color of hyperlinks , i editted my xml for textview -
android:textColorLink="#69463d"