Android: Where to find the RadioButton Drawable? - android

Ok, I am trying to create a custom view called CheckedRelativeLayout.
It's purpose is the same as a CheckedTextView, to be able to use it in a list of items you want selected or in a Spinner.
It's all working fine now, I extended RelativeLayout and implemented Checkable interface.
However, I am stuck on a quite simple problem: Where can I find the Drawable that CheckedTextView and RadioButton use?
I looked at the sourcecode of both, and they seem to use com.android.internal.R. Well... that's internal stuff. So I can't access it.
Any way to get these Drawables or solve the problem somehow?

look under SDK folder /platforms/android-2.0/data/res/
you can access them by either android.R.drawable ( if public ) or need to copy them as drawable to your project

For sake of completeness:
Here some code pieces that show how you I got it working with above accepted answer.
//Image Setup (Once when creating this view)
ImageView indicator = (ImageView) findViewById(R.id.RadioStatusImage);
indicator.setImageDrawable(getResources().getDrawable(android.R.drawable.btn_radio));
//State Change (In some other method)
android.R.attr.state_checked
if (isChecked)
{
indicator.setImageState(android.R.attr.state_checked, false);
}
else
{
indicator.setImageState(View.ENABLED_STATE_SET, false);
}
invalidate();

To use the new default animated radio button drawable, the correct answer is in the comment of #sidon:
?android:attr/listChoiceIndicatorSingle
Using this in the custom position relative to text:
<RadioButton
...
android:gravity="center_horizontal|bottom"
android:drawableTop="?android:attr/listChoiceIndicatorSingle"
android:button="#null"
/>

Related

HeaderListView: How to change background color in RowView

I'm using HeaderListView(https://github.com/applidium/HeaderListView) and
Navigation-drawer-page-sliding-tab-strip(https://github.com/Balaji-K13/Navigation-drawer-page-sliding-tab-strip). In last library, I've PageContentStripFragment class that I implemented onCreateView method content:
HeaderListView list = new HeaderListView(getActivity(),null);
list.setAdapter(new ListViewSectionAdapter());
It's working!! Hence, ListViewSectionAdapter is a subclass of SectionAdapter. In override getRowView method I inflated my layout called "custom_row_list".
So, I want to set custom background color when RowItem selected.
I did to set drawable selector and custom_row_list.xml:
android:background="#drawable/list_selector"
But not working!! What I doing wrong??
My environment is ADT eclipse + android 4.4.2.
Thanks!
as of now you cant inflate xml layout using this library as applidium people clearly mentioned in Future works.
if you want to set a custom color to rowitem. implement it on onRowItemClick method.
getChildAt(position).setBackgroundColor(android.R.color.black);
you can use StickyListHeaders library too.

Custom a CheckedTextView in android

I'm using a code that is not mine in my work.
This code uses CheckedTextView, do this:
...
AlertDialog.Builder alert;
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
switch (soundPreference.getType()) {
case BOOLEAN:
CheckedTextView checkedTextView = (CheckedTextView) v;
boolean checked = !checkedTextView.isChecked();
((CheckedTextView) v).setChecked(checked);
switch (soundPreference.getKey()) {
case SOUND_ACTIVE:
sound.setAlarmActive(checked);
break;
...
works perfectly, the problem is that I want I want to change the checkbox graphic.
Right now it looks like (defualt, I think):
and I want to get something like this:
if the CheckedTextView were defined in xml, know how to do it.
My problem is not being defined in xml, do not know how to do it.
I found this:
Customize the CheckedTextView for ListView
but I do not know how to implement it
appreciate any help.
Thank you very much in advance
http://developer.android.com/reference/android/widget/CheckedTextView.html#setCheckMarkDrawable%28int%29
Example:
myCheckedTextView.setCheckMarkDrawable(R.drawable.icon);
This method allows you to set the check mark image dynamically. You should call it when setting up the CheckedTextView.
The method in the developer reference just needs the id of the image resource you want to use.
There is also a version that takes a Drawable as a parameter instead of an id.
You need to create a custom list_item layout.
For full explanation go through the below link...
http://amitandroid.blogspot.in/2013/09/android-custom-multiple-choice.html
Hope this will help you.

Best way to program buttons/images

I am designing something that will have a homepage close to the Google+ android app.
I'm having trouble figuring out the best way to do this? Should I create a button with text and set the background as the image? Should I create an image with the text already programmed in the actual picture or should I program the text and picture to be buttons.
Any suggestions from you guys on past projects?
You can use a Button with text to whatever you like and then place the image for that button above the text (using android:drawableTop)like so:
<Button
android:id="#+id/imageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Photos"
android:drawableTop="#drawable/button_image" />
replacing buttton_image with your actual image. If you want the image in a different position (i.e. below text etc) use:
android:drawableLeft
android:drawableRight
android:drawableBottom
This would be how I would and do do it...
Since I am a newbie in android development I may be wrong but I suggest why not use a Grid View with each grid item haaving a textview and imageview.
My suggest to layout:
If you want to try something new in Android 4.0 , you can try GridLayout to layout , it can reduce the complicate of the nested layout , check out the blog about GridLayout:
http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
You should write Buttons this way: http://developer.android.com/resources/articles/layout-tricks-merge.html
I normally use RelativeLayout but this is not important.
Write a class myClass extends RelativeLayout and inflate the XML with
LayoutInflater.from(context).inflate(R.layout.myCustomLayout, this, true);
I think this is best practice by Google.
use a regular button and set the android:drawableTop="#drawable/icon_...." value
Having the text burnt into you image is most certainly not the way to go.
One option for you is a GridView. You could also do this through a combination of Image button and scrollview. In my opinion, GridView is best and involves the least amount of code with most flexibility.
Remember that you can replace the button's background image with a state list drawable.

Getting ListView drawables and applying it manually

i am trying to apply the style that a list item has, when it is selected, to a View (In my case a TextView)
Example
here it would be the orange-painted style. I found that this look is defined by a Drawable Object. So i tried, so get the drawable and applied it to my view
TextView tv = new TextView(this);
tv.setText("Hello, Android");
tv.setBackgroundDrawable(new ListView(this).getSelector());
setContentView(tv);
but it doesn't work.
Does anybody have an idea how to do that?
Ok I figured out how to do that. I found out hat ListView uses the ColorStateList list_selector_background which is defined in android.R.drawable. Using ResourceBrowser I got to know that the fourth color is the selected drawable so i added the following to my code
StateListDrawable listDrawables= (StateListDrawable)getResources().getDrawable(android.R.drawable.list_selector_background);
listDrawables.selectDrawable(3);
Drawable highlightDrawable = listDrawables.getCurrent();
Now I can set my Background using higlightDrawable. I don't know if it is possible to access the drawable in xml I did not try it yet. Thanks for help!
It would be a pain to do it by code.
You need to implement a selector and let android know which resource needs to be used when the view is pressed, enabled or focused.
Here is sample example on how to achieve the same.

setBackgroundColor(drawable) != android:background=(drawable)?

I've got a custom drawable resource to display my items in a ListView, actually two because I wanted my results to have alternating background colors but both to respond to clicks by changing their color. The problem is even when assigning even one of these drawables to my LinearLayout container via the layout XML, it works fine, but via Java code it doesn't. So to be exact, this works:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/result_white"
android:id="#+id/result"
>
but this (in my ResultAdapter which extends ArrayAdapter) doesn't:
LinearLayout result = (LinearLayout) v.findViewById(R.id.result);
result.setBackgroundColor(R.drawable.result_white);
My final objective is of course to have alternating 'result_white' and 'result_ltgray' drawables for results so the first XML solution does not really satisfy my needs. What am I missing in the Java code please?
Well, assuming you are only using one-color backgrounds, you should use Colors instead since drawables can be shapes, gradients and more. Now, to actually use color, your code will look something like:
result.setBackgroundColor(mContext.getResources.getColor(R.color.result_white));
where mContext is the context, and you have a color (such as 0xFFFFFFFF) in your res/values/colors.xml file.
Also take a look at Color State Lists for changing colors when pressed / selected / etc
Thanks for your help guys, but what I needed to do is this:
result.setBackgroundResource(R.drawable.result_white);
This way I could easily implement this into my ResultAdapter for alternating results reacting to clicks with changing backgrounds:
LinearLayout result = (LinearLayout) v.findViewById(R.id.result);
if (position % 2 == 0)
result.setBackgroundResource(R.drawable.result_white);
else
result.setBackgroundResource(R.drawable.result_ltgray);
Make sure you imported the right reference for R (android.R for android drawables, your_app_path.R for your own).

Categories

Resources