How to get favorites star - android

I would like to add a favorites star like the one in the contacts list on android. Is there any way we can get that star or will I have to write my own? I can't seem to find any standard GUI Elements from android other than the options menu icons. Are there any more that I have not found?
Look at the one the right next to George Anderson. When you click it it turns yellow.
(source: mail2web.com)

The source code to the Contacts application is available online, since Android is open source.
Some poking around in there will lead you to the contact_header.xml file, found in your SDK installation. It indicates that the star is implemented via a CheckBox:
<CheckBox
android:id="#+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone"
android:contentDescription="#string/description_star"
style="?android:attr/starStyle" />
That, in turn, routes you to an entry in a theme:
<item name="starStyle">#android:style/Widget.CompoundButton.Star</item>
which in turn resolves to:
<style name="Widget.CompoundButton.Star">
<item name="android:background">#android:drawable/btn_star_label_background</item>
<item name="android:button">#android:drawable/btn_star</item>
</style>
So, use those images with a CheckBox, and you should get the same behavior. Those images are also available in your SDK installation.

Some standard android images are available from the android sdk, which you can either browse on your computer on online here. (As CommonsWare said).
I also find this website super handy, as it shows me what each image looks like and tells me the name of the image so I can find it in the android sdk.

#android:drawable/btn_star (this one turns yellow)
#android:drawable/star_off
and variations on those (big, on, off)

Related

Strings from strings.xml available in application but not used by widgets

I have been making an Android app. Android Studio rightly scolds me if I hardcode strings in the layout editor, so I put them in res/values/strings.xml. For a simplified example:
<resources>
<string name="thing">Do a thing</string>
</resources>
I then set a button's text property to #string/thing. This correctly displays "Do a thing" in the layout editor, as expected. However, when I actually load my app on my phone with the default Run or Debug commands in Android Studio, the button is blank. This is interesting, as when I manually invoke resources.getString(R.string.thing), I do get "Do a thing" back.
I can manually set the widgets' text fields this way, by doing acrobatics like:
findViewById<Button>(R.id.myButton).text = resources.getString(R.string.thing)
but this is a lot of work that as far as I know should be done automatically. This has happened to me in a Java app too, so the problem isn't Kotlin-specific. I'm using Android Studio 3.6 on Linux and the device is a Redmi Note 3 Pro with LineageOS 16 (Pie).
The button is also blank when the app is launched in a virtual AVD, so clearly I must be doing something wrong.
Here are the full activity layout and string XML files.
Each of your Button widgets has tools:text instead of android:text:
<Button
android:id="#+id/nextButton"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:text="#string/nav_next" />
android:text is used for fixed text to be displayed at runtime and, by default, in the development tools.
tools:text is for sample text to be displayed in development tools only. Mostly that is for cases where the text that you really want to use is not known until runtime (e.g., needs to be loaded from a database). Using tools:text lets you get a sense of what the layout will really look like in the tools, while not pre-populating that text when your app runs.
So, switch from tools:text to android:text and you should get the results that you seek.

Implementation of Google design guidelines for Sliders

Yesterday I was looking for sliders in Android and found this website with the Google search: https://material.io/guidelines/components/sliders.html#sliders-discrete-slider
I know that I can use a SeekBar in Android to implement sliders. However, Google seems to have very nice examples of discrete sliders but I cannot find any code examples.
I already implemented a normal SeekBar that is looking like this:
How can I make it look like this?
(Difference: When I move my slider, there is no big drop that shows the current value)
I think I might just have missed the code documentation for these design guidelines. Does anyone know where to find it? Or is the design difference because I got Android 5.0.2 on my phone?
sadly google just provided how it should look like, but there seems to be no class provided by the android support libraries :(
but for now you can try this library: https://github.com/AnderWeb/discreteSeekBar
or this for even more material elements:
https://github.com/navasmdc/MaterialDesignLibrary
hopefully google adds this in later releases...
Now you can use the official Slider in Material Components Library.
Just use something like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false">
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_gravity="center"
app:labelBehavior="withinBounds"
android:value="60"
android:valueFrom="0"
android:valueTo="100"
..../>
</LinearLayout>
NOTE: it requires the version 1.2.0 (currently 1.2.0-beta01) of the library.
If you want to customize the tooltip shape with a circle marker instead of the default label you can use the labelStyle attribute:
<com.google.android.material.slider.Slider
app:labelStyle="#style/tooltip"
with:
<style name="tooltip" parent="Widget.MaterialComponents.Tooltip">
<item name="shapeAppearanceOverlay">#style/tooltipShOverylay</item>
<item name="backgroundTint">#color/....</item>
</style>
<style name="tooltipShOverylay">
<item name="cornerSize">50%</item>
</style>
AnderWeb's discrete seekbar has a few problems. And for the other one(MDL), you may not want to compile the entire material design library just for a descrete seekbar/slider.
But there is a nice github repository you may find useful: BubbleSeekBar
I tried to find a nice solution for the same problem. In my case I was also trying to find a seekbar that will always show the bubble. After two hours of research I found BubbleSeekBar library, which provides every single attribute you can think of. It was hard to find this library since the readme file doesn't even use the word "material".
EDIT:
After six months, now there is another good Discrete Seek Bar repo that you may find useful. IndicatorSeekBar seems to have everything covered, except a few Issues. You can check it here.
Continuous slider
Continuous sliders allow users to make meaningful selections that don’t require a specific value.
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_gravity="center"
android:value="8.09"
android:valueFrom="0.0"
android:valueTo="11.0" />
Discrete slider
Discrete sliders display a numeric value label upon pressing the thumb, which allows a user to input an exact value.
<com.google.android.material.slider.RangeSlider
android:id="#+id/range_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_gravity="center"
app:values="#array/initial_slider_values"
android:valueFrom="0.0"
android:valueTo="10.0"
/>
<!--array.xml-->
<array name="initial_slider_values">
<item>4.0</item>
<item>8.0</item>
</array>

Which is the best drawable resource to use for close button?

Simple enough: I just want to put a little button with an [x] in it in the upper-right corner of my dialog.
I tried:
<ImageButton
android:src="#android:drawable/btn_close_normal"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
but was informed by Eclipse that this drawable is not public.
error: Error: Resource is not public. (at 'src' with value '#android:drawable/btn_close').
Is there a "best practices" way to implement this, or am I stuck doing it from scratch?
ETA: Here is what my dialog currently looks like. I want to change that square in the upper-right corner to the [x] glyph.
If you're not married to using exactly that icon, there is another one android provides. I used the built in button resource without having to unpack anything.
<ImageView
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="25dp"
android:background="#android:drawable/ic_menu_close_clear_cancel"
android:layout_gravity="end" />
I've got this imageView in a FrameLayout and have it appear in the upper right corner of the container. Clearly on the backend, I'd wire up an OnClick listener to implement the action.
They are marked as non-public, you can unzip the android.jar file and import them to your project. Here I have extracted from SDK-19, XHDPI versions for you:
These are in order - normal, pressed, selected:
and here is source for btn_close.xml which you can put in your /drawable folder and reference as #drawable/btn_close:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_close_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/btn_close_selected" />
<item android:drawable="#drawable/btn_close_normal" />
</selector>
If you wanted to go further you can extract the earlier SDK versions and place them in appropriate /drawable folders.
The best thing to do is to copy the files to our application. I was told by some Google employees that you should not reference the resources, rather copy them to your application.
One of the reasons for this is that if you reference a whole bunch of icons for your application, you are not guaranteed that every one of these is updated at the same time when new versions of the SDK is released. You might end up with some up-to-date fancy icons and some old ones :)
Already discussed alot about this stuff:https://groups.google.com/forum/#!topic/android-developers/2rvmKqG1TBM
The btn_close_normal is actually one of the states of the btn_close drawable: see here
Try using #android:drawable/btn_close

Why my list preference doesn't have a arrow-down icon beside it?

I have a list preference in my preference category, everything looks good except that there is no arrow-down icon beside it.
I changed my theme from light to dark, but no change regarding this arrow icon. I think this is a feature of list preference, but don't know why just not shown!
Any help is great appreciated!
I want to upload images about it, but with no reputation, it is not allowed! what a shame!
I found a list preference example on internet
http://viralpatel.net/blogs/android-preferences-activity-example/
this example also doesn't have arrow-down icon, exactly the problem I have.
Unfortunately, they removed the Arrow in HoneyComb (API Level 11).
And never put it back!!
Anyway, there's a trick you can apply to overcome this limitation:
In your ListPreference attributes, add a line like this:
android:widgetLayout="#layout/arr_dn"
having previously defined a file called arr_dn.xml in your res/layout path, containing the following lines:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:src="#drawable/arr_dn"
/>
Of course, you need to put your graphics (I called it arr_dn.png) in your res/drawable path.
I tested under several API levels, including 2.2 and 4.0.3 and it's working well.

Android Layout Editor Freaks Out on Question Mark

This is strange, yet I see it all the time as I have lots of reasons to display just a simple question mark in this app.
When editing with the Eclipse graphical layout editor, everything is fine...until I tell a TextEdit or a Button to display just a question mark. Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/question_mark" />
</LinearLayout>
And the string is defined in res/values/strings.xml. The relevant line is:
<string name="question_mark">\u003f</string>
The error message I get is:
Missing styles. Is the correct theme chosen for this layout? Use the
Theme combo box above the layout to choose a different layout, or fix
the theme style references.
Couldn't find theme resource for the current theme
Change the text, and the error message goes away.
You can see that in my struggles, I'm even trying to use the unicode version of a question mark. And yes, \? doesn't work either.
Note that this only happens when the graphical layout editor is set to API 7 or greater.
Now the graphical layout editor displays the question mark properly, and the emulator and my phone display the question mark without any problems. I'm just annoyed with the error message taking up 1/4 of my screen for all my layouts (and obscuring other error messages that may crop up).
Any suggestions?
First, test this again on the ADT 21 release that shipped today.
If the problem continues, create a sample project that demonstrates the issue, and post it along with step-by-step instructions on the Android issue tracker.
Well, this is a really crappy hack, but it kind of works--if you stand on your head!
You can use the unicode \u00bf as in
<TextView
android:id="#+id/test_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u00bf" />
It's not a normal question mark, but it's close enough for a hack and gets that annoying error message out of your hair. And it might even increase the humor quotient of your app!
But PLEASE, if anyone out there has a real fix, post it!
Wow, I entered a bug report...[time marches on]...finally, I get a few emails as the Google team starts to look at it.
Today, I received some good news. It looks like the bug has been fixed (and they found a few related bugs, which have been fixed as well). The fix will be in the next release, Version 21.1 Preview 2. You can read the official details here.
Looking forward to it!

Categories

Resources