Set a consistent style to all EditText (for e.g.)
Override the attribute pointing to the EditText
style(named editTextStyle
:) ) in your custom theme:
<style name="App_Theme" parent="@android:style/Theme.Holo">
<item name="android:editTextStyle">@style/App_EditTextStyle</item>
</style>
and make your custom style to extend Widget.EditText
:
<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:background">@drawable/filled_roundededges_box_dark</item>
<item name="android:textColor">#808080</item>
<item name="android:layout_height">45dip</item>
</style>
Edit:
If you're using the much newer AppCompat related themes use the editTextStyle attribute without the android prefix:
<style name="App_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>
How to underline an EditText
It is default, but try to check your styles. Maybe there is a style that will override the style in the whole application. If there is, just remove it
Put constant text inside EditText which should be non-editable - Android
Did u try this method?
final EditText edt = (EditText) findViewById(R.id.editText1);
edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());
edt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().startsWith("http://")){
edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());
}
}
});
How to consistently set EditText Selected Underline Color Programmatically
So the solution for me was to go pure AppCompat
So I'm adding an AppCompatEditText
to the TextInputLayout
protected EditText EditText => Control.EditText;
protected override TextInputLayout CreateNativeControl()
{
var textInputLayout = new TextInputLayout(Context);
var editText = new AppCompatEditText(Context)
{
SupportBackgroundTintList = ColorStateList.ValueOf(GetPlaceholderColor())
};
textInputLayout.AddView(editText);
return textInputLayout;
}
Then from there I was able to set the underline consistently with this.
private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
_hasFocus = args.HasFocus;
SetUnderlineColor(_hasFocus ? GetActivePlaceholderColor(): GetPlaceholderColor());
}
private void SetUnderlineColor(AColor color)
{
var element = (ITintableBackgroundView)EditText;
element.SupportBackgroundTintList = ColorStateList.ValueOf(color);
}
full source code here.
Android - margins specified in custom style not taking effect
Short Answer: If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" style="@style/edit_text_default" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" style="@style/edit_text_default" />
</TableRow>
</TableLayout>
Explanation: Attributes which begin with layout_
are LayoutParams, or one of its subclasses (e.g. MarginLayoutParams). LayoutParams
are used by views to tell their parent ViewGroup how they want to be laid out. Each and every ViewGroup
class implements a nested class that extends ViewGroup.LayoutParams
. Therefore, LayoutParams
are specific to the ViewGroup
's type. What this means is that while a TableLayout
and a LinearLayout
may both have layout_margin
as one of it's LayoutParams
, they are considered to be completely different attributes.
So layout_margin
is not just general attribute that can be applied anywhere. It must be applied within the context of a ViewGroup
that specifically defines it as a valid argument. A view must be aware of the type of its parent ViewGroup
when LayoutParams
are applied.
Specifying layout_margin in a style, including that style in a theme, and attempting to apply that theme to an application/activity will result in the layout attributes being dropped, because no ViewGroup parent has been specified yet and so the arguments are invalid. However, applying the style to an EditText
view that has been defined with a TableLayout
works, because the parent ViewGroup
(the TableLayout
) is known.
Sources:
Android documentation on Layout Parameters.
Answer given to this question by Android framework engineer and StackOverflow user adamp.
Also, answer given to this question by StackOverflow user inazaruk.
Set text alignment property for TextViews application wide
Create a styles.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>
<style name="MyTextViewStyle"
parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
Then just apply that theme to your application in AndroidManifest.xml
:
<application […] android:theme="@style/MyTheme">…
And all your text views will default to the style defined in MyTextViewStyle
(in this instance, bold and red)!
This was tested on devices from API level 4 onward and seems to work great.
Related Topics
How to Detect Android Listview Scrolling Stopped
Android Studio and Gradle Build Error
How to Access an Existing Sqlite Database in Android
Cannot Find R.Layout.Activity_Main
Failed to Find Build Tools Revision 21.1.1 - Sdk Up to Date
How to Disable Viewpager Adapter on Touching Specific Views
How to Access Resource with Dynamic Name in My Case
What Is The Different Between Explicit and Implicit Activity Call in Android
How to Enable Night Mode Programmatically
Using Onbackpressed() in Android Fragments
How to Get Loaded Web Page Title in Android Webview
How to Use Standard Attribute Android:Text in My Custom View
How to Know If Android Talkback Is Active
How to Change Option Menu Icon in The Action Bar
How to Enable Autostart Option for My App in Xiaomi Phone Security App Programmatically in Android
Set Textcursordrawable Programmatically
When Should I Do Certain Sqlite Operations on Another Thread(Not The Main Thread)