Hint and InputType on Included EditText - android

I have a custom EditText declared in an XML file and I'm including it like so:
<include layout="#layout/my_edit_text"
android:id="#+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/passwordHint"
android:inputType="textPassword" />
and here is my_edit_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:textColor="#color/gray"
android:textSize="#dimen/editTextFontSize"
android:padding="#dimen/editTextPadding"
android:background="#drawable/edit_text_background"
android:ellipsize="end" />
However, I can't set the hint or inputType this way, for some reason. If I set it in my_edit_text.xml, it works fine, but I would like to be able to set each reference individually.
The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Do I have to do something similar to what this person has? If I do, will I need to actually build a .java subclass and extract the attributes that way? That just seems excessive.

The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Step #1: Delete my_edit_text.xml.
Step #2: Delete all references to my_edit_text.xml.
Step #3: Define a style resource (e.g., in res/values/styles.xml) akin to:
<style name="rileyText">
<item name="android:textColor">#color/gray</item>
<item name="android:textSize">#dimen/editTextFontSize</item>
<item name="android:padding">#dimen/editTextPadding</item>
<item name="android:background">#drawable/edit_text_background</item>
<item name="android:ellipsize">end</item>
</style>
Step #4: Add style="#style/rileyText" to all EditText widgets that you want to have those particular attributes applied to.

Related

Can not get TextInputLayout & TextInputEditText to style correctly on disabled

I use multiple of the new TextInputLayout & TextInputEditText objects in my Android project, as described here: https://material.io/develop/android/components/text-input-layout/
But, no matter what I do, I cannot get the field to appear 'grey' when it is disabled. In fact, it looks the same either way.
Here is how I define it in my Layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/username_wrapper"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header_container"
android:layout_marginLeft="#dimen/list_verticle_margin"
android:hint="#string/username">
<android.support.design.widget.TextInputEditText
android:id="#+id/username_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
I also had to overwrite a color in my colors.xml file:
<color name="mtrl_textinput_default_box_stroke_color">#fff</color>
Although, removing this does not make much of a difference.
Can anyone please help me figure out how to make this appear disabled?
Here is the resulting textfield:
I forgot I asked this question...
Here is how I solved it (but it affects ALL the TextInputs).
Overwrite the disabled color in your colors.xml file:
<color name="mtrl_textinput_disabled_color" tools:override="true">#919191</color>
Set the TextInputLayout and the TextInputEditText as disabled from the code. Hope this helps someone!!
UPDATE: YOU NO LONGER HAVE TO OVERWRITE 'mtrl_textinput_disabled_color'.
With new material components, you can just set a style like:
<style name="example.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/example_color</item>
//// Other stuff here if you want.
</style>
example_color.xml in your colors directory, looking like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorAccent" android:state_focused="true"/>
<item android:color="#color/disabled" android:state_enabled="false"/>
<item android:color="#color/textDefault"/>
</selector>
Then you can apply the style to any InputTextLayout, using
style="#style/example.TextInputLayout"
in its xml. This gives a lot more flexibility and allows you to be able to style each TextInputLayout individually if you so wished.

DRY approach on android

During development of Android application, I noticed that I use multiple buttons, that the different between them is the Id, the text and the onclick function.
So I implemented a different XML file holding 1 styled button, and when I want to use a button I just import it into the required layout.
But then I realized I have a new problem: I don't know how to set all the different parameters at each imported button...
Is there a method to perform this and stick to DRY approach?
Well, if you need multiple buttons, you will need to give them different identifiers. You can do this in your import declaration like this:
<include layout="#layout/your_button"
android:id="#+id/loginButton" />
Then, you can reference them by ID like usual:
Button loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setText("Login");
However, I am not sure this is a better approach than just declaring your buttons as needed in XML. If you want to avoid repeating styling, you can define a custom style for your button and apply it in the XML declaration. See the docs on styling for more information.
Below is an example from the docs with an EditText.
In the styles:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="#style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
In your layout:
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="#style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />

Android: keep blue background after ListView selection

I have a ListView, and it works great on a phone. Now I am making a tablet UI, with the ListView on the left and details on the right.
When I touch an item, it flashes blue as long as it is pressed. I want to keep that blue color until another item is selected, just like the Gmail app on the Nexus 7.
What is the cleanest way to achieve that? I'd rather avoid setting backgrounds manually, I assume there is a way to mark an element as the "active" one and theme it accordingly.
What is the cleanest way to achieve that?
What you are looking for is known as the "activated" state. To make this work:
Step #1: In res/values-v11/, have a style resource that implements activated. For example, for a new project that has the AppTheme declaration defined there, go with something like:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
Step #2: Define the same style in res/values/ for any older devices, just as a stub style resource, so references to it continue to work:
<resources>
<style name="AppTheme" parent="android:Theme.Light"/>
<style name="activated" parent="AppTheme"/>
</resources>
Step #3: In your layout XML resource for the row in the ListView, add style="#style/activated" to the list of attributes of the root element
Step #4: Set the ListView to be a single-choice list, such as the following line in a ListFragment:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
You can see this in action in this sample project, this sample project, and this sample project. For more background on those first two samples, see this SO question: Complete Working Sample of the Gmail Three-Fragment Animation Scenario?
Using
android.R.layout.simple_list_item_activated_1
instead of
R.layout.simple_list_item_checkable_1.
Just for somebody checking someday.
after days of search and pulling my hair i just found out that activatedBackgroundIndicator is also available in ActionBarSherlock styling system. Most of the devs which develop backwards compatible apps, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases. So instead of using android:background="?android:attr/activatedBackgroundIndicator" which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator"
here is the example row layout xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textSize="15sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="5dip"
android:textSize="20dip" />
</LinearLayout>

How to set style for spinner programmatically?

I have spinner with a style selection (I set the spinner style in strings.xml) and it's works well if I set the style in main.xml. But I want to know how to set the style programmatically in which the style already defined in strings.xml.
Refer my code below
main.xml:
<Spinner
android:id="#+id/PassengersSpinner1"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_span="3"
android:layout_marginTop="6dp"
style="#style/SpinnerStyle" />
strings.xml:
<style
name="SpinnerStyle"
parent="#android:style/Widget.Spinner">
<item name="android:background">#android:drawable/btn_default</item>
</style>
Now, I want to know that how can I set this strings.xml programmatically without main.xml?
From all my reading, you cannot do it, because the style has to be set before the view is created.
You can do it if you create the view in code (since then you can set the style before creating the view).
If you want to do that, a good example is here. Both the question and the answer give you methods to achieve your goal.

How to change typeface of a TextView in the xml?

I have been googling around, but haven't found a solution for this.
I know I can change the font of a TextView programatically like this view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));.
But, isn't there a way to do it directly in the xml file? I have found android:typeface attribute but it only have the options of "normal", "sans, "serif" and "monospace". How can I use this font I have in /assets/fonts/Roboto-Regular.ttf?
Thanks.
I wrote a library project that can do this. It's called Nifty: https://github.com/tom-dignan/nifty
With nifty, you can specify typefaces in your assets dir from your styles.xml file or as an attribute directly in your XML layout. It provides a custom NiftyTextView and NiftyButton that inherit all the functionality of their parent classes except provide the ability to specify the typeface in XML.
Example Style:
<style name="MyStyle" parent="#android:style/Theme">
<item name="typeface">my_font_that_is_in_assets.ttf</item>
</style>
Example XML:
<com.tomdignan.nifty.NiftyTextView
android:id="#+id/title"
style="#style/MyStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
Alternatively, if you are not using a style:
<com.tomdignan.nifty.NiftyTextView
xmlns:nifty="http://schemas.tomdignan.com/nifty"
nifty:typeface="my_font_that_is_in_assets.ttf"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
Get nifty
Caveats: Custom views are not previewable in the layout editor. My way of working around this is using a search/replace.

Categories

Resources