Programmatically set the EditText background resource to a transparent field

This is the default method that looks like EditText when I create it:

Here's what it looks like after changing the background:

enter image description here

To change the background to the image above, I will add the following EditText property to my EditText in the XML file:

android:background="@android:drawable/edit_text" 

I want to do what I did above in my activity class (giving the user the option to choose between classic style and transparent / new style).

So, I need people to fill in /*...*/

 if (classicTextbox()) { // the blocky white one editText.setTextColor(Color.BLACK); editText.setBackgroundResource(android.R.drawable.edit_text); } else { // the new transparent one editText.setTextColor(Color.WHITE); editText.setBackgroundResource(/*...*/); } 

So, how do I change it to a new, transparent EditText style?

+6
source share
2 answers

In the end, I tried to keep the original Drawable when opening the layout, and then set the background resource this way whenever I want. Like this:

In my onCreate method:

 Drawable originalDrawable = editText.getBackground(); 

And then install it:

 // need to use .setBackground and .setBackgroundDrawable depending on the // android version because .setBackgroundDrawable is depreciated int sdk = android.os.Build.VERSION.SDK_INT; int jellyBean = android.os.Build.VERSION_CODES.JELLY_BEAN; if(sdk < jellyBean) { editText.setBackgroundDrawable(originalDrawable); } else { editText.setBackground(originalDrawable); } 

I still offer more answers because I don't really like this solution.


Sources:

How to return to the default style in EditText if applying a background?

setBackground vs setBackgroundDrawable (Android)

+7
source

Background to EditText golon by default: edit_text_holo_dark . This is a dark version. There is also an analogy with the light version: edit_text_holo_light .

edit_text_holo_dark.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_multiline_default_holo_dark" /> <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_multiline_disabled_holo_dark" /> <item android:state_multiline="true" android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_activated_holo_dark" /> <item android:state_multiline="true" android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_multiline_focused_holo_dark" /> <item android:state_multiline="true" android:state_enabled="true" android:drawable="@drawable/textfield_multiline_default_holo_dark" /> <item android:state_multiline="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_disabled_focused_holo_dark" /> <item android:state_multiline="true" android:drawable="@drawable/textfield_multiline_disabled_holo_dark" /> <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" /> <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled_holo_dark" /> <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_activated_holo_dark" /> <item android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_focused_holo_dark" /> <item android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" /> <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_focused_holo_dark" /> <item android:drawable="@drawable/textfield_disabled_holo_dark" /> </selector> 

Keep in mind that these head elements are only available on Android HC +.

+1
source

All Articles