Multiple text lines on a button. Each line has different text styles - android

Hi i'm trying to create a button with multiple lines of text. See below example. Each line of text has a different text size and colour applied to it. The button also needs to be able to send the user to the next page. Is this possible in XML? I've looked everywhere and can't find a solution. Thanks.
BUTTON HEADING TEXT(large green font)
1st button text (small grey font)
2st button text (small white font)
See example:

Picture is not accessible. but according to your problem, you can implement a LinearLayout as main componenet and set its property clickable and focusable, and set its background to android.R.drawable.btn_default. Set orientation of this layout to Vetical.
Add as many TextViews of any style in this linearlayout in xml or dynamically as you want. But set all the TextViews focusable and Clickable property to false.

Html.fromHtml(string) can interpret some of the hmtl tags and you can hence use html to style the text of your buttons.
Assuming that you have defined your buttons in a XML file you can then set the text of the buttons like this in the Java code.
Button button = (Button) findViewById(R.id.button1);
String styledText = "<big> <font color='#008000'>"
+ "My orders" + "</font> </big>" + "<br />"
+ "<small>" + "You have no current orders" + "</small>";
button.setText(Html.fromHtml(styledText));
// Attach a listener to the button that will make something
// happen when the button is clicked
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Put your Intent code here
Log.d("onclick", "Button click registered");
}
});

You can stack <big> and <small> HTML tags to multiply their effect:
private final String btnInnerHTML = "<big><big><big><font color='#000000'><b>%s</b></font></big></big></big><br/><small>%s</small>";
btn0.setText(Html.fromHtml(String.format(btnInnerHTML, "Line1", "Line2")));

Related

How to add second text view just after completion of the first textview

I want to add second textview, just after completion of the first one. Linear layout with orientation horizontal didn't help.
Any other alternatives? Please suggest!
Generally, we use HTML text for this information.
We can use as many text styles as we want for different texts. With this way, you can set properties for each texts differently.
For Example: <HTML><text1><text2></HTML>
In the above, you can define text properties independently. Finally place this text in Textview. So we can achieve this using single TextView itself.
Example:
String str = "<a>This is a <font color='#800000FF'> blue text</font> and this is a <font color='red'> red text</font> </a>";
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));

Best way to make "fill the gaps" quiz?

I've used three TextViews. one for "-", second for the empty field and the third for the rest of the sentence.
enter image description here
but the problem is that, this is quite buggy and when I add more text to the last TextView (sentence TextView) the text wraps to the bottom of the last TextView instead of the first TextView. Help Please ):
enter image description here
You can use one TextView and at runtime set text of it like below:
myTextView.setText("-"+" " + your text);
That is all thing you need.

Android set color and listner on part of text

Suppose I have TextView and I want to change color of some part in it. Like this
And I must set OnclickListener on text " Terms Of Use ", so How can I do that but using two textViews. Thanks
Regards
Use a simple Checkbox with no text, and for text clicking event
this is what you are looking for Spannable
You can youse HTML in TextView:
myTextView.setText(Html.fromHtml("<h2>Test</h2>"));

Android Add imagine icon as a "hint" in a textview?

is it possible to add a image icon inside a textviews Hint?
at the moment i can only add text in the hint properties but what i want to do is add a small image icon and a text.
is this possible?
You will need to write your own View which could perhaps be based on a FrameLayout which contains both a TextView (with an empty hint) and an ImageView. Alternatively you could simply use a TextView and when it's empty you set a background image to your "hint icon", and clear the background when text has been entered.
You could user setError to display some text and and an image in the textfield but that would be after the user inputs something and you check it.
Alternatives:
Use setBackground with your hint icon on the background image
Subclass TextView and override the drawing to draw your hint icon
String hint = "<center><img src=\"" + R.drawable.search_hint + "\"/>搜索感兴趣的内容, hello world , I'm form China.</center>";
Html.fromHtml(hint, new ImageGetter()
{
#Override
public Drawable getDrawable(String source)
{
int img = Integer.parseInt(source);
Drawable drawable = getResources().getDrawable(img);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
}, null);
searchTxt.setHint(hint);

(Android button) how to implement Two line text with different colors on one button

In my android app development, I have one button, the button text is not a single color text, it is two lines text, with each line uses different color for line text. How to implement this? "Two lines" can be simply implemented by adding "\n" in the text, I don know how to set different colors for each line text on the button. Anyone can help?
Try to use Html.fromHtml when setting text to a button:
Button button = (Button) getElementById(R.id.some_button);
button.setText(Html.fromHtml("<font color='red'>First line</font><br/><font color='blue'>Second line</font>"));

Categories

Resources