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.
Related
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.
I have a RadioGroup which consists of five RadioButtons. What I'd like to achieve is setting an image as a RadioButton argument. So far found this way:
radio0.setBackgroundResource(<drawable ID, int>);
but it gives something like this (descriptions "isSelected" added in an image editor):
Is there any way to programmatically set background properties and move the image to the right?
Or maybe there is another way to set an image next to RadioButton instead of using it as a background image?
I'm interested only in programmatical solutions, as the RadioGroup is cleared up and filled again during loop work.
A friend of mine helped me to figure out this issue.
Instead of the code line posted in the question I use this one now:
radio0.setCompoundDrawablesWithIntrinsicBounds(0, 0, <drawable ID, int>, 0);
The third parameter corresponds to android:drawableRight XML parameter. More on this: click.
Thanks to it a picture is set to the right of the RadioButton and finally it looks like it should.
I want to remove focus in gallery for a selected item. I am getting an output like this, Here is an image :
Here first item selected. So it is looking darker than 2nd image. I want to display an image as it is not focused or selected.
Please anyone help. I'll really appreciate that.
Thanks
Create a statelist drawable (see http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html) and customize the "selected" state to whatever you want there, in your case transparent . Then you provide this drawable as the background for the item.
EDIT:
Also look in to gallery
android:unselectedAlpha
try to apply below two property in your xml to your gallery.
android:focusableInTouchMode="false"
android:focusable="false"
you can use something like
View.clearFocus()
LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout);
myLayout.requestFocus();
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.
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"
/>