Cannot increase the change of opaque value of track in android Switch - android

How can I increase the opaque of track in Switch Android .
I have used style as below .
But the opaqueness is not 100% when the switch is on .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_checked="true" />
<item android:color="#66FFFFFF" />
</selector>
Style
<style name="style_switch" >
<item name="android:trackTint">#drawable/selector_switch_track</item>
</style>
Switch Code
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/style_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
>
</Switch>

Related

Create custom flat switch android

I want to make one custom switch as shown below.
I have created two drawable resource for that one is switch_bg and another is tracker_bg.
switch_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/thumb_on" android:state_pressed="true" />
<item android:drawable="#drawable/thumb_off" android:state_enabled="false" />
<item android:drawable="#drawable/thumb_on" android:state_checked="true" />
<item android:drawable="#drawable/thumb_off" />
</selector>
tracker_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_tracker" android:state_enabled="false" />
<item android:drawable="#drawable/ic_tracker" android:state_pressed="true" />
<item android:drawable="#drawable/ic_tracker" android:state_checked="true" />
<item android:drawable="#drawable/ic_tracker" />
</selector>
and in main xml I put following code.
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginRight="20dp"
android:thumb="#drawable/switch_bg"
android:track="#drawable/track_bg"/>
Please guide me What I am doing wrong or What things are missing from my side.
Try this sample it may help you.Click here

Why is this ripple effect is not working as expected?

I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />

ColorStateList doesn't work

I am beginner in android, and of course i have a problem with ColorStateList.
I want to set it to ListView, but it doesn't work, maybe someone know why?
it is declaration of TextView
<TextView
android:textColor="#color/text_color"
android:id="#+id/tv"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/in"
android:onClick="onClick"
android:clickable="true"
android:duplicateParentState='true'/>
and content of text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
It is code from StackOverflow reply, should work :/
Try by setting listview.setItemsCanFocus(true);

Creating "ChristopheVersieux / HoloEverywhere" Seekbar

I tied to follow the example in
https://github.com/ChristopheVersieux/HoloEverywhere
to create Seekbar that is shown in below picture. However, it was too advanced compared to my beginner level,
I would appreciate if somebody could show me how to do this, I need the relevant code only?
i got it, i was missing the android:thumb below:
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/topBar"
android:layout_marginTop="26dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:max="10000"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/scrubber_progress_horizontal_holo_light"
android:thumb="#drawable/scrubber_control_selector_holo" >
</SeekBar>
in drwable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/scrubber_control_disabled_holo" android:state_enabled="false"/>
<item android:drawable="#drawable/scrubber_control_pressed_holo" android:state_pressed="true"/>
<item android:drawable="#drawable/scrubber_control_focused_holo" android:state_selected="true"/>
<item android:drawable="#drawable/scrubber_control_normal_holo"/>
</selector>
and:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#drawable/scrubber_track_holo_light"/>
<item android:id="#android:id/secondaryProgress">
<scale
android:drawable="#drawable/scrubber_secondary_holo"
android:scaleWidth="100%" />
</item>
<item android:id="#android:id/progress">
<scale
android:drawable="#drawable/scrubber_primary_holo"
android:scaleWidth="100%" />
</item>
</layer-list>
of course you need also the image from there.
In order to use the theme you must have a "themes.xml" file in your res/values folder with the following code (assuming the library is imported...):
<style name="YourTheme" parent="Theme.HoloEverywhereLight">
<!-- You can customize the theme here! -->
</style>
And in your application's AndroidManifest file, apply the theme:
<application
android:icon="#drawable/app_launch_icon"
android:label="#string/app_name"
android:theme="#style/YourTheme" >
And then finally in your activity's main.xml layout, add the seekbar somewhere, as follows:
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="75" />

Unable to resolve color value

I am trying to make my buttons change the color of their text when it is pressed but i ran into a problem that i cant solve. I get an error saying "unable to resolve color value" then it gives me the path to the file. here are my files im using
This one is in a new folder named color under resources and its called button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffff0000" />
<item android:state_focused="true" android:color="#ff0000ff" />
<item android:color="#ff000000" />
</selector>
And
<Button android:text="Main Menu" android:textColor="#color/button"
android:layout_width="200px" android:id="#+id/mainmenu"
android:layout_height="55px" android:layout_x="5dip"
android:layout_y="174dip"
android:textSize="18px">
</Button>
its driving me crazy if anybody could help me.
I successfully did it like this:
Files:
/drawable/button_states.xml
/layout/main.xml
/values/colors.xml
button_states.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/red" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/blue_background" /> <!-- focused -->
<item android:color="#color/white" /> <!-- default -->
</selector>
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue_background">#FF2f74c3</color>
<color name="white">#fefefe</color>
<color name="red">#ff0000</color>
</resources>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:textColor="#drawable/button_states" />
</LinearLayout>
Try android:background instead.

Categories

Resources