What is default background for AppCompatEditText? - android

I want to know what is default background of AppCompatEditText because I want to use its as default background of my selector state in xml but now I still not know what is it default backgrounf of this view.
This is what I currently did
private val defaultBackground = background
......
override fun setEnabled(enabled: Boolean) {
if(enabled) this.background = defaultBackground
else this.background = null
}

The default style for an AppCompatEditText is Widget.AppCompat.EditText.
Navigating through the styles you can find that the background is defined with:
<item name="android:background">?attr/editTextBackground</item>
Looking for this attribute in the appcompat theme you can find:
<item name="editTextBackground">#drawable/abc_edit_text_material</item>

Related

Setting ActionMenuView font family/typeface

I am currently working on an application on Xamarin & I am trying to set the ActionMenuView's font family (on the navigation bar), but I have found nothing to set the font family on this object.
Moreover, I wanted to set the title's font family & I made a custom renderer that is working perfectly. So I tried the same for the ActionMenuView but it does not have any typeface/font-family attribute on it.
My custom renderer (working) :
private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
{
// here I can pass on the ActionMenuView object but it does not have any typeface property?
if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
{
Android.Support.V7.Widget.AppCompatTextView AppCompatTextView = ((Android.Support.V7.Widget.AppCompatTextView)e.Child);
AppCompatTextView.SetTypeface(Typeface.CreateFromAsset(Context.ApplicationContext.Assets, "MontserratRegular.otf"), TypefaceStyle.Normal);
_toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
}
}
You can try to create style:
<style name="Style_font">
<item name="fontFamily">#fonts/PTSerif-Bold.ttf</item>
</style>
Then use this style in Menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
In MainActivity.cs:
Android.Support.V7.Widget.ActionMenuView bottombar =FindViewById<Android.Support.V7.Widget.ActionMenuView>(Resource.Id.toolbar_bottom);
IMenu bottomMenu = bottombar.Menu;
MenuInflater.Inflate(Resource.Drawable.menu_bottom_view_pager,bottomMenu);
I don't test, but you can try

How to return MaterialButton default border color?

I have two buttons, and i want to realize blue border on pressed one. When button pressed, it't border become blue, border of another button return to default.
I can't return default border correct. I write function, but is works wrong.
My button(second is the same with another id and text):
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_schemes_1"
style="#style/Buttons.Schemes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_4"
android:layout_weight="1"
android:text="#string/schemes_1"
app:icon="#drawable/ic_schemes_1" />
Style has nothing interesting:
<style name="Buttons.Schemes" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:typeface">sans</item>
<!--android:fontFamily="sans-serif-condensed-medium"-->
<item name="android:textColor">#android:color/black</item>
<item name="android:letterSpacing">0</item>
<item name="iconTint">#null</item>
<item name="backgroundTint">#android:color/white</item>
</style>
My last realization was about to find and remember default border value before first click happens, so at the first i write:
private var defaultColor: Int = 0
Then
defaultColor = btn_schemes_1.strokeColor.defaultColor
And function, called when one of button clicked:
private fun setButtonsBorder(buttonNumber: Int) {
when (buttonNumber) {
1 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_1.strokeWidth = 2
btn_schemes_2.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_2.strokeWidth = 1
}
2 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_1.strokeWidth = 1
btn_schemes_2.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_2.strokeWidth = 2
}
}
}
But this failed too, because default color not "those" default at fact. When i set it, i found it's just colorPrimary.
How can i find this default color or write method to retutn border color to default?
Ok, i get it myself.
After goes deeper i call it before button clicked:
Timber.e(btn_schemes_1.strokeColor.toString())
And get:
ColorStateList{mThemeAttrs=nullmChangingConfigurations=1073742848mStateSpecs=[[16842912], [-16842912]]mColors=[-16743049, 520093696]mDefaultColor=-16743049}
So 520093696 is my color. Google says it black12 constant, presented by 0x1F000000
And after
private var defaultColor: Int = 0x1F000000
All works.

Changing Android's EditText cursor pointer color programmatically

So, i've searched a lot but i couldn't find anything, if this ends up being a duplicate i'm sorry, but i want to cange the color of the EditText's cursor pointer that is used to move the cursor
I already managed to change the cursor's color itself but it doesn't affect the pointer which seems to only respond to accentColor.
I'm pretty sure it's possible because i recall seeing an app that did it, it had the pointer and all the elements in a dialog change color based on a color that you choose without changing the accent color of the view below the dialog.
Please help :)
You can create your own style/theme for only this EditText and change the ColorAccent :
<style name="EditTextColorCustom" parent="#style/AppBaseTheme">
<!-- Customize your theme here. -->
<item name="colorAccent">#color/colorAccent</item>
</style>
You can do this by creating new style in style.xml like this
<style name="CustomEditTextTheme" parent="TextAppearance.AppCompat">
<item name="colorAccent">#color/primary_dark_material_dark</item>
</style>
Then in EditText tag use this
style="#style/CustomEditTextTheme"
you can use textSelectHandle to replace it by your drawable like this:
android:textSelectHandle="#drawable/your_drawble"
You can use reflection to set selectHandle.
EditText has Editor class that has the mSelectHandleCenter field (if you need selection use mSelectHandleLeft and mSelectHandleRight)
private fun setCursorPointerColor(view: EditText, #ColorInt color: Int) {
try {
//get the pointer resource id
var field = TextView::class.java.getDeclaredField("mTextSelectHandleRes")
field.isAccessible = true
val drawableResId = field.getInt(view)
//get the editor
field = TextView::class.java.getDeclaredField("mEditor")
field.isAccessible = true
val editor = field.get(view)
//tint drawable
val drawable = ContextCompat.getDrawable(view.context, drawableResId)!!
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN)
//set the drawable
field = editor.javaClass.getDeclaredField("mSelectHandleCenter")
field.isAccessible = true
field.set(editor, drawable)
} catch (ex: Exception) {
}
}
You can add colorControlActivated to your AppTheme in style :
<item name="colorControlActivated">"Your_color"</item>

How can I get current theme's action bar background color?

I was trying to make my navigation drawer's background always match with the action bar's background color.
So that, every time, if the theme changes both background will change automatically.
I looked into R.attr, but didn't find anything.
The ActionBar API doesn't have a way to retrieve the current background Drawable or color.
However, you could use Resources.getIdentifier to call View.findViewById, retrieve the ActionBarView, then call View.getBackground to retrieve the Drawable. Even so, this still won't give you the color. The only way to do that would be to convert the Drawable into a Bitmap, then use some sort of color analyzer to find the dominant color.
Here's an example of retrieving the ActionBar Drawable.
final int actionBarId = getResources().getIdentifier("action_bar", "id", "android");
final View actionBar = findViewById(actionBarId);
final Drawable actionBarBackground = actionBar.getBackground();
But it seems like the easiest solution would be to create your own attribute and apply it in your themes.
Here's an example of that:
Custom attribute
<attr name="drawerLayoutBackground" format="reference|color" />
Initialize the attribute
<style name="Your.Theme.Dark" parent="#android:style/Theme.Holo">
<item name="drawerLayoutBackground">#color/your_color_dark</item>
</style>
<style name="Your.Theme.Light" parent="#android:style/Theme.Holo.Light">
<item name="drawerLayoutBackground">#color/your_color_light</item>
</style>
Then in the layout that contains your DrawerLayout, apply the android:background attribute like this:
android:background="?attr/drawerLayoutBackground"
Or you can obtain it using a TypedArray
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TypedArray a = obtainStyledAttributes(new int[] {
R.attr.drawerLayoutBackground
});
try {
final int drawerLayoutBackground = a.getColor(0, 0);
} finally {
a.recycle();
}
}

How to change textcolor of switch in Android

I'm creating an application which uses Android 4.0.
I'm wondering if it is possible to change the text color of the text in a switch.
I've tried setting the text color, but it doesn't work.
Any ideas?
Thanks in advance!
You must use android:switchTextAppearance attribute, eg:
android:switchTextAppearance="#style/SwitchTextAppearance"
and in styles:
<style name="SwitchTextAppearance" parent="#android:style/TextAppearance.Holo.Small">
<item name="android:textColor">#color/my_switch_color</item>
</style>
you can also do it in code, also using above styles:
mySwitch.setSwitchTextAppearance(getActivity(), R.style.SwitchTextAppearance);
...and as for setTextColor and Switch - this color will be used if your SwitchTextAppearance style doesn't provide a textColor
you can check it in Switch source code in setSwitchTextAppearance:
ColorStateList colors;
int ts;
colors = appearance.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColor);
if (colors != null) {
mTextColors = colors;
} else {
// If no color set in TextAppearance, default to the view's textColor
mTextColors = getTextColors();
}
ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, 0);
if (ts != 0) {
if (ts != mTextPaint.getTextSize()) {
mTextPaint.setTextSize(ts);
requestLayout();
}
}
I think you have to look at the theme which you are using for your application. Because the color of the switch is the responsibility of the theme, afaik. So I would suggest you have a look on how you can change the settings of a theme. Or you could create a custom theme with the new colors.
TextView.setTextColor() takes an int representing the color (eg. 0xFFF5DC49) not the resource id from the xml file. In an activity, you can do something like:
textView1.setTextColor(getResources().getColor(R.color.mycolor))
outside of an activity you'll need a Context eg.
textView1.setTextColor(context.getResources().getColor(R.color.mycolor))
For more refer this

Categories

Resources