How can I increase text size in Button, without changing Button size. In other words i want to display text in whole Button surface. And i want to achive that in java code not in XML file.
In java class you can do that as follows
Button bt = new Button(this);
bt.setTextSize(12);
and in xml
<Button
android:textSize="10sp"/>
Note: Recommended dimension type for text is "sp" for scaled-pixels.
Button button = new Button(this);
// set the text size in the second parameter. I have set 20 for the sake of your understanding.
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
Let me know if it works.
Related
I would like to add a picture before the text "Accessar" within the button. As the picture below.
I got the above effect in java class, I wonder if it is possible in xhtml.
<Button
android:id="#+id/login.button.cadastrese"
style="#style/ButtonsLogin"
android:background="#drawable/btn_login_cadastrese"
android:drawableLeft="#drawable/icon_login_cadastrese"
android:text="#string/login.btn.cadastre" />
work aligned to the button, not the text
Spannable buttonLabel = new SpannableString(getResources().getString(R.string.login_btn_acessar));
buttonLabel.setSpan(new ImageSpan(getApplicationContext(),R.drawable.icon_login_acessar, ImageSpan.ALIGN_BASELINE), 0, 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
btnLogin.setText(buttonLabel);
Resolves, but is much line to little effect. Thus, I have to add the string underlin _Acessar
I'm very new to android and I want to reduce the size of EditText field in my layout.
For example if the user only need to enter 3 digits to the text field, then the size of the EditText should be small enough not the one provided by default.
Is that possible to do so?
Thanks in advance.
Try like this..
android:layout_width="wrap_content"
or
android:layout_width="20dp" //size your choice
In your Layout file set the attribute to that text field width as
android:layout_width="wrap_content"..This will take width as the text content entered into the field
You can use
android:layout_margin="size you want"
use can also use
android:layout_margin_left="size you want"
instead of "left" you can also right, top, bottom.
I'm making a grid of cells and each cell is a button that is clickable. I want to add a horizontal line to the button when it is clicked like that:
Could any one help me?
Thanks in advance
The right way to do this would be to create your own 9-patches for the Button. You can create them with any image editor (I usually use Gimp), but make sure you resave them with the draw-9-patch tool in your android-sdk directory because it adds the necessary formating to the 9.png. Check out here for more information regarding 9-patches. Once you've finished creating the two 9-patches for the Button you will need to create an xml file in your drawable folder which will select which image to choose based on the Button's current state, check out here for more info on creating a selector for a button. It's a decent amount of work, but it's worth it to do it the correct way.
private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) button.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, button.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Have a look at these link for better understanding::
StrikethroughSpan
Spannable.setSpan
I have a set of buttons in a table each with some text. I now wish to have some of the buttons include a drawable icon on top (obviously with smaller text) without disrupting the size of the buttons. Is there a standard way of achieving this?
Note: I wish to do this all programmatically rather than via an xml file.
Have you tried?
Button b = new Button(this);
b.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_launcher), null, null);
b.setText("Button");
Try this and let me know what happen..
My button's layout_width set to match_parent.
In order to display multi lines on the button, I tried:
insert '\n' into the text on button
set Singleline false set Maxlines to 2 or 3
convert html from Html.fromHtml
Nothing worked. '\n' showed up as a small square on the button while showing single line of text.
Does anybody have any idea why this is happening and how I can fix this?
UPDATE: I just found out I was using custom button that has its own text drawing. That's the reason. Sorry for the confusion. I just punished myself by banging my head.
If you're trying to add a new line in a layout XML file:
Use
(new line)
android:text="Hi
Hello"
If you're trying to add a new line in code, just use '\n', same as in any other text.
If you can't see the second line, it may be that your Button doesn't have enough height. IE, in my case, the layout containing the button had a fixed height that just happened to make my button perfectly display one line of text.
I just tried and it worked:
1) Define in ../res/values/strings.xml:
<string name="multilines">Line1Line1\nLine2Line2</string>
2) Refer it in the layout file:
<Button
android:id="#+id/btn_multilines"
android:text="#string/multilines"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</Button>
In case you want to do that programmaticaly you can use System.getProperty("line.separator") in the string to change lines.
Like this:
String mybuttontext=line1+System.getProperty("line.separator")+line2;
and then set this String as buttons text.