I've tried for two day now to append an image to a TextView, but it didn't work. I have tried this for Android 6/API Level 23. ImageSpan, Html.fromHtml and other Methods did not work. So is it possible to append image (Bitmap) between other words in a TextView on Android 6?
EDIT
Here is the XML:
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:text="------"
android:gravity="bottom|left"
android:typeface="monospace"
android:id="#+id/mainTextView1"
android:textIsSelectable="true"/>
And image span Code:
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText()); ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mOutEditText.setText(ssb, BufferType.SPANNABLE);
Add image in xml
<TextView
android:drawableBottom="#drawable/ic_add_circle_black_24dp"
android:drawableEnd="#drawable/ic_add_circle_black_24dp"
android:drawableLeft="#drawable/ic_add_circle_black_24dp"
android:drawablePadding="#drawable/ic_add_circle_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
programmatically
textView1.setCompoundDrawables(left,top,right,bottom);
Or only left
textView1.setCompoundDrawables(left,null,null,null);
I have found the solution now for Api 21, 22, 23+:
private void appendImage(Bitmap bmp)
{
tv.setTransformationMethod(null);
SpannableString ss = new SpannableString(" ");
ss.setSpan(new ImageSpan(bmp, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(ss);
}
And in the XML:
android:textAllCaps="false"
The error was that in api level 21+ you have to set the TransformationMethod null.
If you want to set the background of the TextView use the following methods:
txt.setBackgroundResource(int rsid);
txt.setBakgroundDrawable(Drawable object);
If by append, you mean you want to add it to the end of the TextView, then use an ImageView for the image. Or you can overlay your TextView in an ImageView
It is not possible to add images between text in a TextView. You can add an image a TextView at the start and end. This is done with
android:drawableLeft
Related
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 making a calculator app in Android Studio(Mac OS) in which I'm trying to include square(x^2) Button.
I've set my square as a TextView.
I've tried all the links mentioned but none worked for me to type it as in proper equation format.
<TextView
android:id="#+id/square"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-20dp"
android:layout_marginStart="-5dp"
android:padding="15dp"
android:textColor="#color/black"
android:textSize="18sp" />
I've also tried below code in MainActivity.java file:-
squareButton = findViewById(R.id.square);
squareButton.setText(Html.fromHtml("x<sup>2</sup>"));
Examples tried
Source 1 Source 2 Didn't work!
P.S. I've set minSdkVersion 14 and targetSdkVersion 26!
Android has native support for sub/superscript type text know as SubscriptSpan and SuperscriptSpan
Sample Usage of Superscript (eg X^2)
String text = "X2";
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.setSpan(
superscriptSpan,
text.indexOf("2"),
text.indexOf("2") + String.valueOf("2").length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(builder);
I have found a really good example.
Just do some short research, Html.fromHtml is deprecated in Android N+.
Correct way should be this:
strButton = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
Just do a check like #Vishva Dave 's comment, for android versions:
String strButton;
String html = "x<sup>2</sup>";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
strButton = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
}
else{
strButton = Html.fromHtml(html);
}
squareButton.setText(strButton);
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.
I wanted to find different TextViews, which are depending on an integer.
Let's say I have got three TextViews:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And in my MainActivity I have got following:
String resourceText = "R.id.text";
int textCounter = 1;
TextView text1 = (TextView) findViewById(resourceText + textCounter);
So the result should be that I am able to access different TextViews over the textCounter but it doesn't work.
But using this code gives me an error which says that the findViewByID function expects an integer.
You can use getIdentifier() to convert string version of a resource id .
You just need to modify your code in this way.
String resourceText = "R.id.text";
int textCounter = 1;
textViewResId=getResources().getIdentifier(resourceText+textCounter, "id", getPackageName());
TextView text1 = (TextView) findViewById(textViewResId);
I have a table layout in XML that does exactly what I want:
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/evenrow">
<TextView android:text="Check" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_weight="1"></TextView>
I am trying to add the table row programmatically, but I can't seem to get the layout_weight set. Here is the java code:
TableRow.LayoutParams trlayout = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
TableLayout.LayoutParams tablelayout = new TableLayout.LayoutParams (TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(trlayout);
TextView check = new TextView(this);
check.setText(String.format("%5.2f", i));
tr.addView(check);
I thought the 3rd param to new TableRow.LayoutParams was the default weight for the row's children, but it is not working.
What am I missing?
Well in a regular XML if you set wrap_content on the dimension of orientation you want to expand in then layout weight has no effect.
Change the width parameter to "0dp" and then your layout weight should work.