Styled EditText inside a compound view doesn't gain focus - android

I have made a very simple compound view:
<LinearLayout>
<ImageView />
<EditText />
</LinearLayout>
Everything worked fine until I have decided to style the Edit text throught my project:theme:
<style name="Theme.MyProject" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Theme.MyProject.ActionBar</item>
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/Widget.Playout.ActionBar</item>
<item name="android:buttonStyle">#style/Theme.Theme.MyProject.Button</item>
<item name="android:editTextStyle">#style/Theme.Theme.MyProject.EditText</item>
</style>
and
<style name="Theme.Playout.EditText">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#color/field_text</item>
<item name="android:background">#color/field</item>
<item name="android:minHeight">50dp</item>
<item name="android:padding">5dp</item>
<item name="android:gravity">center</item>
<item name="android:fontFamily">sans-serif-light</item>
</style>
Now my EditText is beautiful but is not gaining focus so text cannot being entered (so useful). It works fine adding values directly to the Compound view from layout but I whould like to do it from a confortable theme.
Thanks in advance!

Try this:
<style name="Theme.Playout.EditText">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#color/field_text</item>
<item name="android:background">#color/field</item>
<item name="android:minHeight">50dp</item>
<item name="android:padding">5dp</item>
<item name="android:gravity">center</item>
<item name="android:fontFamily">sans-serif-light</item>
<!-- I added these attributes: -->
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:clickable">true</item>
</style>

Related

TextInputLayout Hint gravity bottom

The Hint is always vertically centred and I want it to be aligned to the bottom of the view close to the border with a bit padding. I managed to do it for the text of the TextInputEditText but I cannot do it for the hint.
Here's how it looks
And here is my layout XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/first_name_layout"
style="#style/My.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/first_name"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/first_name_edit_text"
style="#style/TextInputEditTextForm"
android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>
And my styles.xml
<style name="My.TextInputLayout.FilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="materialThemeOverlay">#style/MyThemeOverlayFilledPadding</item>
<item name="android:gravity">bottom</item>
<item name="android:textColorHint">#color/gray</item>
<item name="boxBackgroundColor">#android:color/transparent</item>
<item name="boxStrokeColor">#color/grey_little</item>
<item name="boxStrokeWidth">0.5dp</item>
<item name="hintTextAppearance">#style/FormInputLabel</item>
<item name="hintTextColor">#color/gray</item>
</style>
<style name="MyThemeOverlayFilledPadding">
<item name="colorControlActivated">#color/gray_light</item>
<item name="editTextStyle">#style/MyTextInputEditText_filledBox_padding</item>
</style>
<style name="MyTextInputEditText_filledBox_padding" parent="#style/Widget.MaterialComponents.TextInputEditText.FilledBox">
<item name="android:paddingBottom">5dp</item>
</style>
<style name="TextInputEditTextForm" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:gravity">bottom</item>
<item name="android:lines">1</item>
<item name="android:textColor">#color/gray_x_dark</item>
<item name="android:textSize">#dimen/text_xx_small</item>
</style>
<style name="MyThemeOverlayFilledPaddingHint">
<item name="android:paddingBottom">5dp</item>
<item name="android:gravity">bottom</item>
</style>
<style name="FormInputLabel" parent="TextAppearance.Design.Hint">
<item name="android:textSize">#dimen/text_xxxx_small</item>
<item name="materialThemeOverlay">#style/MyThemeOverlayFilledPaddingHint</item>
<item name="android:gravity">bottom</item>
</style>
The hint is drawn by helper class CollapsingTextHelper which is available in private-package scope.
You can access the field via reflection but accessing private field is only available on pre Orea devices

Handles of EditText are underlined

I encountered a problem recently, which was to change the default underline bar of an edittext from a default blackish color to white.
I used this solution, which seemed to me the best practices by changing the style of the EditText :
EditText
<android.support.v7.widget.AppCompatEditText
android:id="#+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Activity_Main_EditText_Nom"
android:imeOptions="actionNext"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:textColor="#android:color/white"
android:textColorHint="#color/activityBackground"
android:theme="#style/EditTextStyle"
android:visibility="gone"
/>
style.xml
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDisablePreview">true</item>
<item name="android:windowBackground">#color/activityBackground</item>
<item name="windowActionBar">false</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<item name="colorControlNormal">#android:color/white</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
</style>
It worked ! But it also added to the handles of the EditText an underline bar...
Any ideas ?
Answer
By discussing with #rafsanahmad007, we finally got to a final solution.
style.xml
<resources>
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDisablePreview">true</item>
<item name="android:windowBackground">#color/activityBackground</item>
<item name="windowActionBar">false</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyTheme.EditText" parent="MyTheme">
<item name="colorControlNormal">#android:color/white</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
</style>
</resources>
Apply MyTheme to application, as usual. When you want to tweak an EditText just add to it :
EditText
<EditText
...
android:theme="#style/MyTheme.EditText"
...
/>
Problem is with your style themes.. parent
use below code:
<android.support.v7.widget.AppCompatEditText
android:theme="#style/MyStyle.EditText"/>
Now in your styles.xml
<style name="MyStyle.EditText">
<item name="editTextStyle">#style/MyEditTextStyle</item>
</style>
<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="colorControlNormal">#android:color/white</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
</style>
try not to use the theme parent directly
EDIT
add the color property in base theme also
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#android:color/white</item>
<item name="colorControlActivated">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
</style>
make sure you select the theme in your activity in manifest...
Use this doc.I think this URL will helpfull for your problem.
http://www.materialdoc.com/edit-text/

Checkbox gets disabled if Spinner Theme is used

I created a theme and set its:
SpinnerStyle
SpinnerItemStyle
SpinnerDropDownItemStyle
Here is the code:
<style name="MySpinnerTheme" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:spinnerItemStyle">#style/MySpinnerItem</item>
<item name="android:spinnerStyle">#style/MySpinner</item>
<item name="android:spinnerDropDownItemStyle">#style/MySpinnerDropDown</item>
</style>
<style name="MySpinner" parent="#android:style/Widget.Spinner">
<item name="android:background">#drawable/base1</item>
<item name="android:clickable">true</item>
<item name="android:enabled">true</item>
</style>
<style name="MySpinnerItem" parent="android:Widget.TextView.SpinnerItem">
<item name="android:background">#drawable/base1</item>
<item name="android:textAppearance">#style/MyTextAppearanceSpinnerItem</item>
<item name="android:padding">2dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:gravity">center</item>
</style>
<style name="MyTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:textColor">#FFF</item>
<item name="android:textSize">20sp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
</style>
<style name="MySpinnerDropDown" parent="android:Widget.Spinner.DropDown">
<item name="android:textColor">#FFF</item>
<item name="android:background">#drawable/base1</item>
<item name="android:gravity">center</item>
</style>
I set the theme for one Activity. When I added a CheckBox to it, it gets disabled and I can't click it.
If I set text to it, it gets written inside the clickable area.
Here is its code:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cboxData"
android:padding="4dp"
android:clickable="true"
android:enabled="true"
android:focusable="true"
android:text="sdsd"
/>
The phone is Android 2.3.
What's wrong with my CheckBox?
It was my mistake: I used setEnabled() instead of setChecked(). No wonder I began to use C#-like properties instead of setXY :)

How to set default height to edittext view

I want to apply 60dp height by default to all my entire app EditText view. Where did i make mistake?
styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:editTextStyle">#style/custom_EditTextStyle</item>
</style>
<style name="custom_EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:background">#drawable/edittextbox</item>
<item name="android:height">60dp</item>
<item name="android:layout_marginTop">4dp</item>
<item name="android:layout_marginBottom">4dp</item>
<item name="android:singleLine">true</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
</style>
Thanks
Use
<item name="android:layout_height">60dp</item>
Not
<item name="android:height">60dp</item>

android: format textview

is it possible to format a textview for a widget so that it looks like the text below the apps.
like in this screenshot:
i am looking for the exact textcolor, the exact dropshadow and the rounded background.
When in doubt, check with the source, which shows that Android is using a custom TextView to implement the bubbled background around the text.
It appears that the text itself is styled using styles in the launcher app:
<style name="WorkspaceIcon">
<item name="android:textSize">13dip</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">marquee</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:shadowRadius">2.0</item>
<item name="android:textColor">#FFF</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#drawable/shortcut_selector</item>
<item name="android:paddingLeft">5dip</item>
<item name="android:paddingRight">5dip</item>
</style>
<style name="WorkspaceIcon.Portrait">
<item name="android:drawablePadding">5dip</item>
<item name="android:paddingTop">4dip</item>
<item name="android:layout_marginLeft">3dip</item>
<item name="android:layout_marginRight">3dip</item>
<item name="android:layout_marginTop">13dip</item>
<item name="android:layout_marginBottom">8dip</item>
</style>
<style name="WorkspaceIcon.Landscape">
<item name="android:drawablePadding">3dip</item>
<item name="android:paddingTop">2dip</item>
<item name="android:layout_marginLeft">10dip</item>
<item name="android:layout_marginRight">10dip</item>
</style>
Those styles are called from the launcher's layouts as appropriate.

Categories

Resources