This question already has answers here:
How to set property "android:drawableTop" of a button at runtime
(8 answers)
Closed 9 years ago.
(java/xml for android 2.1 or higher)
I'm creating button using style:
<style name="MainButton">
<item name="android:textColor">#000000</item>
<item name="android:text">test</item>
<item name="android:drawableRight">#drawable/ic_launcher</item>
<item name="android:drawablePadding">10dip</item>
<item name="android:paddingLeft">10dip</item>
</style>
I know that I can change text (from my java code), for example like this:
Button but = (Button)findViewById(R.id.button2);
but.setText(Html.fromHtml("<b>" + "title" + "</b>" + "<br />" +
"<small>" + "description" + "</small>" + "<br />" +
"<small>" + "DateAdded" + "</small>"));
And it looks very well, but
How can I change my drawable object inside of that button from my java code?
I dont want to use "button.setBackgroundResource();" method.
Any ideas? :>
I think you want a custom selector
Create a custom selector and set it as a drawable resouce in xml.
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/cell_top_selected" />
<item android:drawable="#drawable/cell_top" />
</selector>
and
use this statements
yourbutton.setSelected(true) // for selection image
yourbutton.setSelected(false) // for alternate image
if you don't want this to
I dont want to use "button.setBackgroundResource();" method.
If you don't want to use setBackGround Resouce then
Take a TextView instead, Super class of Button is TextView, by default TextView don't have any background drawable resource.
make another xml with the same attributes but change '#drawable/' resource. and on the onclick method set the background resources of the button to this new xml
button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
You can either use resource ids or drawables
If I have understood the question correctly and I am assuming you want to completely change the button and draw your own then you will have to create your own Button. This can be done by creating a new class and then extending it with Button and then override the onDraw() method of the View class. Here in this method you can draw what ever you like i.e. background, text, borders..etc.
For example
class NewButton extends Button{
protected void onDraw (Canvas canvas){
//Draw here
}
}
I hope this helps.
Related
I created alert dialog with TextInputLayout programmatically in fragment. I need to hide password by default, TextInputLayout has toggle button and it is working as expected hide/show on click. I tried to keep password hidden by default by settingsingleline to true and other hack i got from other SO answers but i still not able to get desired result. Any idea why i am not getting desired result?
TextInputLayout passwordParent = new TextInputLayout(getActivity());
passwordParent = new TextInputLayout(getActivity());
TextInputEditText passWord= new TextInputEditText(getActivity());
passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
passWord.setBackgroundResource(R.drawable.background);
passWord.setBackgroundColor(getResources().getColor(R.color.white));
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
passwordParent.addView(passWord);
Background XML Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_remove_red_eye_black_24dp" android:state_checked="true"/> // Visible Eye Icon
<item android:drawable="#drawable/ic_visibility_off_black_24dp"/> // Hide Eye Icon
</selector>
Edit: It's working by adding InputType.TYPE_CLASS_TEXT into input type like this passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); but the icon still stay the same, its still showing wrong eye icon.
Try moving addView(passWord) up by two lines, like this :
passwordParent.addView(passWord);
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
Components (text and edit boxes) defined in layout XML looks very different from those i add programatically.
I've tried to apply the same textAppearance style programatically as the ones from my XML has. I tried calling setTheme() after adding the components too. No difference.
TextView tv10 = new TextView(getApplicationContext());
tv10.setText("Back width");
EditText tv11 = new EditText(getApplicationContext());
tv11.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
TextView comes out with small font and gray text, EditText comes out with black background and gray text.
The components must be added programmatically because of options the user choose. Those user choices are defined in the XML and follow the expected color scheme, which is the Android Studio defaults. (Black text on white background)
You shouldn't use Application Context for views. Only Activity context. Also you can pass style as parameter, when create view in code.
As Yamko said, style can be passed in the constuctor
var textView = TextView(context, null, R.style.LoginBodyTextViewStyle)
where the style can be something like
<style name="LoginBodyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">#dimen/text_size_default</item>
<item name="android:textColor">#color/grey</item>
</style>
How can I edit or disable the 'pin' for editTexts that appears when marking text? I was able to edit the underline & the cursor, but not that 'pin' because I didn't know how to call it.
Image of the pin (pink) added.
I think the colourAccent influences the colour of that pin, try changing the colour of colourAccent in color.xml
This 'pin' is called "text select handle"
To change it you need to use these attributes:
text_select_handle_left and text_select_handle_right
Add these drawables with customized design/color to your drawable folder and add to style
<style name="MyTheme" parent="#style/MyCustomTheme">
<item name="android:textSelectHandle">#drawable/text_select_handle_middle</item>
<item name="android:textSelectHandleLeft">#drawable/text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/text_select_handle_right</item>
</style>
I would like to generate a pressend button design and load it on the fly.
The static version it is an xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="true"/>
<!-- unselected state (default) -->
<item android:drawable="#drawable/bt_back_normal"/>
</selector>
which is located in the /res/drawable folder. When I want to use it just a line of the code:
android:background="#drawable/bt_back"
Now, the current project loads the design for the button from the server side, let it be the bt_back_normal.png loaded from www.somehost.com/some/resource/bt_back_normal.png.
It would be nice if I could get an API to generate the "pressed" version ( some darker) and link it at events chain to show, when needed.
Right now there is no visual effect, when the user press the button.
How can I generate that xml equivalent on the fly? -generate a pressed version and set to show when needed.
Thanks.
I think you're looking for the StateListDrawable class. You can create these in code, add states to it (e.g. your downloaded pressed png file) and then set it to your button with button.setBackgroundDrawable(stateList).
No, you can't do this on the fly. If you wanna use dynamically generated pressed drawables you should implement OnTouchListener and set needed background inside of it.
This is kind of a workaround for that, but you could just override the OnClickListener for the button, and change the background of the button inside there. ie.
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundDrawable(R.drawable.button_pressed);
}
});
EDIT:
I didn't realize you wanted to change the states; thought you just wanted to show it was pressed. In that case, use StateListDrawable: http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
I was wondering whether there is a simple way to tell Android to always use a custom style to display Dialogs.
Using themes and styles you can change the look and feel of all, say, TextViews using this piece of code when defining a theme in themes.xml:
<item name="android:textViewStyle">#style/Blue.TextView</item>
with Blue.TextView being defined in styles.xml.
Is there some way to do so for Dialogs as well ?
yes, there is. for example,
AlertDialog.Builder b = new AlertDialog.Builder(context, R.style.MyTheme);
or, if you are using a DialogFragment, just call the setStyle() method,
<style
name="Theme_Dialog_Translucent"
parent="android:Theme.Dialog">
<item
name="android:windowBackground">#null</item>
</style>
Below is the working code::
Dialog mDialog = new Dialog(this,R.style.ThemeDialogCustom);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCanceledOnTouchOutside(true); //But here It is not workin
mDialog.setContentView(R.layout.popup);