I'm trying to put inline images in a TextView. Here goes my code:
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = ctx.getResources().getDrawable(Integer.parseInt(source));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
CharSequence votesString="124 votes";
votesString=Html.fromHtml(votesString
+"(<img src='" + R.drawable.icon_upvotes + "'/>)", imageGetter, null);
labelVotes.setText(votesString);
It works (see the image below) but I would need the image to be vertically centered. How can I do that?
Thanks
Instead of use HTML, you can use
labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);
and playing a little bit with padding, using setCompoundDrawablePadding
Why you don't use android:drawableRight ?
Related
I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.
Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.
Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:
public static int convertToPixels(int value) {
Resources resources = mContext.getResources();
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
return x;
}
Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.
If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setId(Integer.parseInt(txn.getId().toString()));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
params.setMargins(0, convertToPixels(1), 0, 0);
row.setLayoutParams(params);
row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
LinearLayout imageLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
imageParams.gravity = Gravity.CENTER;
imageLayout.setLayoutParams(imageParams);
ImageView image = new ImageView(mContext);
if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
}
imageLayout.addView(image);
row.addView(imageLayout);
LinearLayout txnMiddleLayout = new LinearLayout(mContext);
txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
txnMiddleLayout.setLayoutParams(txnTopParams);
TextView txnTopContents = new TextView(mContext);
txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnTopContents.setText(txn.getTopLineContents());
txnTopContents.setTextColor(Color.BLACK);
// txnTopContents.setTextSize(convertToPixels(16));
TextView txnBottomContents = new TextView(mContext);
txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnBottomContents.setText(txn.getBottomLineContents());
// txnBottomContents.setTextSize(convertToPixels(12));
txnMiddleLayout.addView(txnTopContents);
txnMiddleLayout.addView(txnBottomContents);
row.addView(txnMiddleLayout);
LinearLayout txnBottomLayout = new LinearLayout(mContext);
txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
txnBottomLayout.setLayoutParams(txnBottomParams);
TextView amount = new TextView(mContext);
amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
amount.setText(txn.getAmountStr());
amount.setTextColor(Color.BLACK);
// amount.setTextSize(convertToPixels(16));
TextView date = new TextView(mContext);
date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
date.setText(txn.getDateStr());
// date.setTextSize(convertToPixels(12));
txnBottomLayout.addView(amount);
txnBottomLayout.addView(date);
row.addView(txnBottomLayout);
txnList.addView(row);
I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.
https://stackoverflow.com/a/3996104/4056947
I have an EditText view which I am using to display information to the user. I am able to append a String and it works correctly with the following method:
public void PrintToUser(String text){
MAIN_DISPLAY.append("\n"+text);
}
The application is an RPG game which has battles, when the user is fighting I would like to display the String in RED. So I have attempted to use a SpannableString. (changed the method)
public void PrintToUser(String text, int Colour){
if(Colour == 0){
//Normal(Black) Text is appended
MAIN_DISPLAY.append("\n"+text);
}else{
//Colour text needs to be set.
//Take the CurrentText
String CurrentText = MAIN_DISPLAY.getText().toString();
CurrentText = CurrentText+"\n";
SpannableString SpannableText = new SpannableString(CurrentText + text);
switch(Colour){
case 1:
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), CurrentText.length(), SpannableText.length(), 0);
break;
}
//You cannot append the Spannable text (if you do you lose the colour)
MAIN_DISPLAY.setText(SpannableText, BufferType.SPANNABLE);
}
This new method works and the new line is displayed is red.
Problems
Only the last line formatted is red. If a new line is set to be red the previous formatting is lost due to the way I retrieve the text
String CurrentText = MAIN_DISPLAY.getText().toString();
When I was ONLY appending Strings to the EditText it was displaying the bottom of the EditText box (the last input) now when using SetText the users see's the Top.
Finally My Question:
Is there a way to Append the formatted text?
OR... Can I retrieve the formatted text from the EditText and then when I use .setText I can keep the current format?
AND if so... Is there a way to focus the Display at the Bottom opposed to the Top?
Thank you! Please comment if additional information is needed I've been stuck on this for a while now.
My suggestion would be to use the method:
myTextView.setText(Hmtl.fromHtml(myString));
Where the myString could be of the format:
String myString = "Hello <font color=\'#ff0000\'>World</font>";
The following modifications to PrintToUser() should work..
public void PrintToUser(String text, int Colour){
...
...
Editable CurrentText = MAIN_DISPLAY.getText();
int oldLength = CurrentText.length();
CurrentText.append("\n" + text);
switch(Colour){
case 1:
CurrentText.setSpan(new ForegroundColorSpan(Color.RED),
oldLength, CurrentText.length(), 0);
break;
}
}
Try to change
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), CurrentText.length(), SpannableText.length(), 0);
on
SpannableText.setSpan(new ForegroundColorSpan(Color.RED), 0, SpannableText.length(), 0);
Scaling with ScaleDrawable is not working for me.
The drawable is remained in the same size.
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
return sd.getDrawable();
What i need to do to fix it?
Thanks.
You need to set the level:
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
sd.setLevel(8000);
return sd;
The level ranges from 0 to 10000: at 10000 it is full size, at 0 it does not appear at all.
If you check the reference for ScaleDrawable, you will see that the getDrawable method returns the base drawable. That is, it returns d in your case. You should just return sd as it is already a Drawable.
I have a problem with the Font Color for the Website link in an Android App. Please see the code below:
Email.setText(Html.fromHtml("W : "+"<u>" +Email1+ "</u>"));
Can i change the Font Color for Underlined Email1 Text without changing the W : Color?
Is there any HTML Tags can be used inside "<u>" +Email1+ "</u>" to change the Font Color. Please help me with your ideas/code. Thanks in advance.
You can use like this
Email.setText(Html.fromHtml("W : "+"<u><FONT COLOR=\"#80776b\" >"+Email1+"</Font></u>"));
Use color code what you want.
Just because we can I added the 'manual' method to generate the exact same output using a SpannableStringBuilder:
String wText = "W : ";
String underlineText = "email#address.com";
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(wText);
ssb.append(underlineText);
ssb.setSpan(new UnderlineSpan(), ssb.length()-underlineText.length(), ssb.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new TextAppearanceSpan("normal", android.R.style.TextAppearance_Medium, 14,
ColorStateList.valueOf(Color.RED), ColorStateList.valueOf(Color.RED)),
ssb.length()-underlineText.length(), ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv1 = (TextView) findViewById(R.id.spannable_text1);
tv1.setText(ssb);
TextView tv2 = (TextView) findViewById(R.id.spannable_text2);
tv2.setText(Html.fromHtml(wText + "<u><font color=\"#FF0000\">" + underlineText + "</font></u>"));
By the way, in stead of only underlining the email address, you could also make it a clickable link. Just so you know. :)
use setTextColor tag to change the color of the text
eg:
Email.setTextColor(Color.RED);
Email.setText(Html.fromHtml("W : "+"<u>" +"this is test"+ "</u>"));
I am sure this should work for you, You want like this?
String styledText = "W: "+"<u>" + "<font color='red'>Email1</font> "+"</u>";
Email.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
This is the simplest solution i guess...
Email.setText(Html.fromHtml("W : "+"<u style=\"color:#80776b\">"+Email1+"</u>"));
My friend made an application using javascript, and uploaded it to his website.
Now I'm trying to wrap it into a webview in android, and that's working fine in some ways.
The page is 480x320
But no matter what screensize I select on Android, there is a white space at the bottom on the webview. I have tried a lot of ways to make it zoom, but nothing worked.
My code at this moment is this
final WebView browser = (WebView)findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl("http://page.xx");
I needed to scale-to-fit for the width and this variation of FunkTheMonk's answer worked well for me:
#Override
public void onPageFinished(android.webkit.WebView view, String url)
{
super.onPageFinished(view, url);
WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
metrics.widthPixels /= metrics.density;
loadUrl("javascript:var scale = " + metrics.widthPixels + " / document.body.scrollWidth; document.body.style.zoom = scale;");
}
wb.loadUrl("javascript:document.body.style.zoom = "+String.valueOf(scale)+";");
Where scale is a float, which you could calculate - I think in your case you want something like the following: browser.getHeight() / 480dp.
Load this Url after your webpage has finished loading.
Can you try to add :
browser.getSettings().setBuiltInZoomControls(true);
browser.setInitialScale(1);
browser.setPadding(0, 0, 0, 0);
And see if it's working ?