Android - Disable ListView Selection Highlight but Keep OnClick enabled [duplicate] - java

This question already has answers here:
Android: disabling highlight on listView click
(15 answers)
Closed 6 years ago.
I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).

Specify android:listSelector="#android:color/transparent" in your ListView XML.

Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.

I have done this way:
By adding two properties of ListView.
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
Your ListView should looks like below:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent">
</ListView>
Done

try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));

The highlight effect is a style on the listSelector. You can override the listSelector style.
This is a example with a listview : Android: disabling highlight on listView click

Related

How to gray a seekbar when disabled?

I want to make grey my seekbar when I disable it.
I know I can programmatically make this changes by changing color but I'd like to know if it is possible to do this directly in my xml activity layout.
In fact in my code I'd like to just write this:
mySeekBarNuance.setEnabled(true);
And automatically my seekbar becomes grey.
Thanks
Do it like this
In your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> // or the themw you are using
...
...
<item name="android:seekBarStyle">#style/MySeekBarStyle</item>
</style>
Add also a new style
<style name="MySeekBarStyle" parent="android:Widget.SeekBar">
<item name="android:colorControlNormal">#color/gray</item> // put here the color for disable state
</style>
controlColorNormal is used for disabled state and colorAccent for enabled state
You can use a selector as background for the Seekbar
A selector set what you want if condition are met (here : android:state_enabled="true")
In your activity/fragment layout you put the seekbar with a background that is a selector :
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_seekbar"/>
You make the selector in your drawable folder :
selector_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#android:color/darker_gray" />
<item android:state_enabled="true"
android:drawable="#android:color/holo_green_dark" />
</selector>
If enabled, the seekbar will be holo green, otherwise it will be gray
You can do the same thing if the seekbar is focused, pressed etc thaht way.
And you can not only set the color but an entire drawable instead

Set button style programmatically

So I'm programmatically creating new buttons and adding them to a LinearLayout, however I want to initialize these buttons with a predefined style. I've spent some time searching for a solution and trying out answers, but I still can't seem to get it to work.
When I add a new button to the layout, it should look like the buttons (near the top) in this picture.
I've tried creating an xml file in res/values/ and initializing a button with new Button(context, null, R.style.ChoiceButton), but it doesn't work resulting in this happening.
I've also tried the workaround of creating a new layout xml for the button and using (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but that also didn't work, resulting in this (two buttons to show lack of margin).
res/values/choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Snippet from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit long to paste in here.
Is there just something I'm missing? Is this even possible?
Ok #kin3tik, I found an old application I made with some custom button.. see what it looks like :
there is my xml for one button :
<Button
android:id="#+id/num1"
android:layout_width="110dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="#string/num1"
android:textSize="20sp" />
I created file .xml in my drawable folder custombutton.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/bleuperso"/> // you can put #XXXXXX for the color you want
<corners android:radius="4dp"/>
And I just put the style in java :
Bfrancois.setBackgroundResource(R.drawable.custombutton);
With this you should be able to find yourself ;)

Custom ToggleButton setChecked(..) issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
After setting toggle button to checked state, every time I click on it, its in same state.
custom selector :
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/list_view_icon" android:state_checked="true"
android:state_pressed="true" android:state_enabled="true"/>
<item android:drawable="#drawable/list_view_icon" android:state_checked="true"
android:state_focused="false" android:state_enabled="true"/>
<item android:drawable="#drawable/map_view_icon" android:state_checked="false"
android:state_pressed="true" android:state_enabled="true"/>
<item android:drawable="#drawable/map_view_icon" android:state_checked="false"
android:state_focused="false" android:state_enabled="true"/>
toggle button :
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_selector"
android:textOn=""
android:textOff=""
android:background="#drawable/map_view_icon"
/>
java code :
In button onClick listener, isChecked is always false
boolean isChecked = ((ToggleButton) view).isChecked();
Log.i(TAG, "isChecked : "+isChecked);
// **its always false and image also not changing.**
Please suggest whats wrong here.
I had a look at this answer, but no use.
Edit : I did a sample project with same scenario. Its working fine but in my project Fragment, its behaving differently.
Here it is for you:
<?xml version="1.0" encoding="utf-8"?>
<item android:drawable="#drawable/list_view_icon"
android:state_checked="true" />
<item android:drawable="#drawable/map_view_icon"
android:state_checked="false" />
You've put a lot of conditions for the different states and that's what causing the problem (see here for more information).
Following your EDIT:
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_selector"
android:textOn=""
android:textOff=""
android:background="#drawable/selector_name"
/>
This will fix it for you FOR SURE! You're using the map icon directly and never go through the selector.
Selectors have an order (see Drawable States).
Remove the android:button attribute.
Set the Selector as background to your ToggleButton:
<ToggleButton
android:id="#+id/toggle_button_map_or_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:background="#drawable/custom_selector"/>
And the custom_selector.xml looks like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Checked and pressed -->
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="#drawable/list_view_icon" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/map_view_icon" />
<!-- Checked -->
<item
android:state_checked="true"
android:drawable="#drawable/list_view_icon" />
<!-- Default (not checked) -->
<item
android:drawable="#drawable/map_view_icon"/>
</selector>

Android, How to change the actionbars logo button on state change?

So I'm wondering how one could implement to an action bar with a logo button, a different image per state.(pressed, released etc).
I know how to do this for a normal button, but I'm not sure if it's possible with the actionbars logo button?
And if it's not, how could one implement am action bar that supports this? externals libs?
Thank you.
Create a drawable xml file in res/drawable such as res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal" />
</selector>
where button_pressed and button_normalare just regular png files in one of your drawable directories (e.g/ drawables-hdpi)
Then in your menu.xml or in your code you can refer to #drawable/button_selector or R.drawable.button_selector and the image will change automatically when the button is pressed.
If you need to change the ActionBar button background you need to override the theme in your res/values/style.xml.
<style name="YourTheme.Sherlock" parent="#style/Theme.Sherlock">
<item name="actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
<item name="android:actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
</style>
Both lines matter: one for the SherlockActionBar and one for the standard ActionBar
Then set the theme to your activity in the manifest:
<activity
android:name=".yourActivity"
android:theme="#style/YourTheme.Sherlock" >
</activity>
And here is actionbar_item_background_selector.xml to be placed in res/drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:drawable="#drawable/abs__list_focused_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
replace the background with the drawable of your choice where android:state_pressed="true"
It's a lot to do for just a background but that is how the ActionBar works.
you can use a selector to pick different image.
Android ImageButton with disabled UI feel
may be help
I think you are looking for this:
final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
This automatically makes the logo you have set look like its being pressed when its clicked. No need to have different images to show state change. Thats a pretty easy way. Though if you are using ActionBarSherlock this won't work, I think. Strictly 4.0 I believe. Check out the doc on ActionBar.

Android: Specify two different images for togglebutton using XML

I'm attempting to override the default ToggleButton appearance. Here's the XML that defines the ToggleButton:
<ToggleButton android:id="#+id/FollowAndCenterButton"
android:layout_width="30px"
android:layout_height="30px"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginLeft="5px"
android:layout_marginTop="5px" android:background="#drawable/locate_me"/>
Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:
centeredOnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (centeredOnLocation.isChecked()) {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
} else {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
}
}
});
Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/locate_me" /> <!-- default -->
<item android:state_checked="true"
android:drawable="#drawable/locate_me_on" /> <!-- pressed -->
<item android:state_checked="false"
android:drawable="#drawable/locate_me" /> <!-- unchecked -->
But this does not work; reading the ToggleButton API (http://developer.android.com/reference/android/widget/ToggleButton.html), it appears that the only inherited xml attributes are
XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.
There does not seem to be the android:state_checked attribute, despite the class having the method isChecked() and setChecked().
So, is there a way to do what I want in XML, or am I stuck with my messy workaround?
Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
</selector>

Categories