Single xml for multiple buttons with similar style - android-studio

I want to make 3 similar buttons, the difference between them is that they are different colors. I had found out earlier that the way to give the buttons rounded corners is through creating an xml file for it, but in that case I had added the color for the button into the xml file. In this case I don't want to do that, since then I would have to make 3 virtually identical xml files to change the color for each button. Is there a way to split the code to have all three buttons reference a single xml for the button style, and have a separate line of code to just modify the color?

Within the values folder of your project, create a styles.xml file (if it doesn't already exist). Then, add code similar to below to set the common features that you want each button to have:
<!-- Style for a button -->
<style name="buttonStyle">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textAllCaps">false</item>
</style>
Then, in your layout file, reference this style that you just created in all three of your buttons similar to the following:
<Button
android:id="#+id/restore_button"
style="#style/buttonStyle"
android:layout_width="200dp"
android:textSize="18sp"
android:layout_height="wrap_content"
android:text="#string/restore_button_text" />
Then, change the color of each button individually with the following code in each button layout:
android:background="#android:color/white"
Finally, if you want to also create a button with rounded corners, reference this StackOverflow post for directions:
How to make the corners of a button round?
You will need to create a custom layout and reference it within each button as a Drawable.

Related

How to change today's date color android CanlendarView

I have created some custom styles for CalendarView to change the text colors:
<style name="CalenderViewCustom" parent="Theme.AppCompat">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccentYellow</item>
<item name="android:textColorPrimary">#color/colorPrimaryDarker</item>
</style>
And applied them into the calendar like so:
<CalendarView
...
android:theme="#style/CalenderViewCustom"
android:dateTextAppearance="#style/CalenderViewDateCustomText"
android:weekDayTextAppearance="#style/CalenderViewWeekCustomText"
...
/>
I would like to change the text color of '9', to a different one from the background of '16'.
Currently, both of them are determined by the colorAccent attribute.
Is there any other attribute that would change only one of them separately?
Found a pretty cool solution:
We can use the CompactCalendarView package which is a little old but still works perfectly fine (Android 26+)
For example to set the "today" color to black:
<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:layout_width="match_parent"
android:layout_height="280dp"
...
app:compactCalendarCurrentDayTextColor="#android:color/black"
...
/>

Android app preferences page doesn't show toolbar

I'm trying to add a preferences page to my Android app.
I've added an <item> to my <menu> that gets "inflated" in onCreateOptionsMenu to make it appear in the <androidx.appcompat.widget.Toolbar> in main_activity.xml.
(1) it shows up as the cog icon that I added, not as the three dots -- why? How do you get the three dots?
When I go to the settings page my toolbar disappears, but in all the examples the toolbar shows the title and a back button.
I have edited the manifest.xml to add the android:parentActivityName=".MainActivity" stuff.
(2) why does my toolbar disappear?
I've defined a list of values another list of labels to use in a <ListPreference> but it doesn't work.
<ListPreference
android:dialogTitle="Select units"
android:entries="#array/units_names"
android:entryValues="#array/units_values"
android:key="list"
android:summary="Click to show a list to choose from"
android:title="Units" />
<resources>
<string-array name="units_values">
<item>miles</item>
<item>km</item>
</string-array>
<string-array name="units_names">
<item>miles</item>
<item>km</item>
</string-array>
</resources>
3 Why isn't this working?! I really can't see what I've got wrong.
So it turns out that the <item> in the <menu> have their icons shown in the toolbar, but if there isn't enough room then the three dots appear automatically and the overflowed items go in there.
This can be controlled to some extent with the attribute app:showAsAction on the <menu> items.
The problem with items not appearing is that that the text is black.
It can be changed in styles.xml with:
<item name="android:textColorAlertDialogListItem">#color/text</item>
and some others, although it appears not to be document what the names of those other colours are -- typical.

Change android editText highlight color

Does anyone know how can I change this green color that appears in my editText ?
I would like to do it from the layout XML if it is possible
just below the editText image.
Any help is appreciated.
I could resolve it adding: android:backgroundTint="#color/color_you_wat" to the EditText in the layout file
Programmatically:
editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);
Using theme:
<style name="AppThem" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- change values below to make same eefect for all EditTexts ->
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">#color/accent</item>
<item name="colorControlHighlight">#color/accent</item>
</style>
<!-- also you can create different styles with values above for many different views ->
Edit:
You can always use in xml file:
"app:backgroundTint="#color/myColor" in xml.
Don't use android:. Otherwise you would lose backward compability with Android 4.4 and older.
Hope it help

Setting a spinner divider for one specific spinner

I've recently been looking into customizing the divider colour between items in a Spinner's drop down list, and I found the way to do it for all spinners at once via the application theme. For example:
<style name="custom_divider">
<item name="android:divider">#FFFFFF</item>
<item name="android:dividerHeight">5dp</item>
</style>
<style name="holo_light_dark_action_bar_custom_dropdown" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:dropDownListViewStyle">#style/custom_divider</item>
</style>
What I want to do is style the dividers for one specific spinner (in this case, the spinner in the action bar), rather than all spinners. I haven't seen any solutions for that, so I'm hoping it's something obvious and that I just missed it. :)

Android: Changing color of text in ListView without custom adapter

Is it possible to change textColor in ListView without defining custom adapter for it? As in TextView we can apply style = "#style/mystyle" in which we can define textcolor.
<ListView
android:id="#+id/listview"
style="#style/WhiteText"
/>
In values, the style is defined as:
<resources>
<style name= "WhiteText">
<item name ="android:textColor">#FFFFFF </item>
<item name="android:background">#000000 </item>
</resources>
I tried it but it's not giving me desired output. So I want to ask, is it a valid way or not?
might be possible with to define the style for the ListView and set the Text color for that.
See below SO Answer:
First
Second
Hope it will help you.
If any query then let me know.

Resources