Textview onclick bg color change in android - android

If we click any of the Textview within a Listview, background color should have to change and onclick release, it should have to change to transparent color in android.Is there anyway?

Create a textview_selector.xml in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_color"
android:state_pressed="true" />
<item android:drawable="#drawable/normal_color" />
</selector>
Define colors in color.xml in res>values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="pressed_color">#FF0000</drawable> <!--custom color for pressed state -->
<drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
</resources>
Now,you need to use it where you define textviews as a part of your listitem.
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/textview1"
android:gravity="center"
android:text="This is sample!"
android:background="#drawable/textview_selector"
android:clickable="true"
/>

Visit http://developer.android.com/guide/topics/ui/themes.html !
There is very good explanation regarding concept you looking for.

Related

How to set button background drawable image with ripple effect in android

My Xml code:
<Button
android:id="#+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/google_btn" />
I am applying Default ripple effect
<Button
android:id="#+id/link_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground" />
but I need button background "#drawable/google_btn" with
"?android:attr/selectableItemBackground". it's means i need ripple effect with custom background.
In your drawable-v21 folder you can write code for ripple effect by your own. Make a drawable xml file and set starting tag by ripple. Like this :
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#color/colorAccentDark">
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#color/button_accent_dark"
android:state_checked="false"/>
<item
android:drawable="#color/button_accent"
android:state_checked="true" />
<item
android:drawable="#color/button_accent_dark" />
</selector>
</item>
</ripple>
When the button has a background from the drawable, we can add ripple effect to the foreground parameter.. Check below code its working for my button with a different background
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="#drawable/shape_login_button"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:text="#string/action_button_login"
/>
Add below parameter for ripple effect
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item
android:id="#android:id/mask"
android:drawable="#drawable/btn_rectangle"/>
<item android:drawable="#drawable/btn_rect_states"/>
</ripple>
Try the above code here in drawables give your own custom drawable backgrounds as you need it.
Another option is to create a layer list drawable and set that as a background. For example:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_background"/>
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>

Change background color of list item in listview when it is selected

How can i Change background color of list item in listview when it is selected.
In res/drawable folder i have create listselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#drawable/focused"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#drawable/selected" />
</selector>
then in values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>
This is listview.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_below="#+id/textView6"
android:layout_height="75dp"
android:textColor="#FFFFFF"
android:layout_marginBottom="0dp"
android:layout_marginLeft="33dp"
>
</ListView>
</LinearLayout>
but it is not changing the background color of list item when selected...
There's no link between your ListView and it's selector. You need to add the following line of code to your listview.xml.
android:listSelector=“#drawable/listselector"
I hope this helps.
Look here. Althought its older, there is implementiation of custom color on items click. I think it has to do something with android:drawable. Try to use
android:color="#color/yourcolor"
And put your drawables to colors:
<color name="focused">#ff5500</color>
<color name="selected">#FF00FF</color>
Its in /res/values/colors folder.

Change button style radio group in Android

I would like to change the default button button style of Android radio buttons to suit my theme. How could I achieve that?
Create a selector drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/radio_selected" android:state_checked="true" />
<item android:drawable="#drawable/radio_normal" />
</selector>
then in your xml of the radio button
android:button="#drawable/(your drawable name)"
example:
<RadioButton
android:id="#+id/radio_button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/your_drawable_name"
android:text="your_text" />
See Adding custom radio buttons in android. Hope this helps you.

How to change the shape of the Textview using themes and styles?

Is there any possible ways to change the shape of the TextView using themes and styles?
I have changed textview shape as rectangle but I failed to change the hexagonal shape of textview. How could I do this?
So far tried searching the web and tried the code, but got no solution for this.
Try this code,
shape.xml ( save this in res/drawable/ )
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="3dp/>
</shape>
</item>
</selector>
Then Change background of TextView.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="black_text_view_medium" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/black</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">16sp</item>
</style>
</resources>
This is the resource file make in the res/values/red.xml and define this style in your textview like this.
<?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"
>
<TextView
style="#style/black_text_view_medium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is your Textview Style"
/>
</LinearLayout>

How to change textColor on press Even in Android?

Is it possible to change the text color of a textview if the textview is pressed?
I want to achieve a flashing effect with the color change only lasting as long as the button is pressed.
I know how to change the background of the textview with a selector list and the correct state but how can I change the color of text if the user pushes a button or a simple textview?
You can define a selector for colors as well. A short example that only distinguishes between pressed and all other states is:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:color="#FFFFFF" />
<item
android:color="#4C566C" />
</selector>
For a full documentation on the selector see this unofficial documentation.
Put every color selector in a single file and put this files in a directory called color in the resources folder of your project.
Taken from official documentation:
XML file saved at res/color/button_text.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>
This layout XML will apply the color list to a View:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:textColor="#color/button_text" />
search for color selector to use in the
android:setTexColor
attr
you can change it using the setTextColor(ColorStateList) method
myTextView.setTextColor(myColorStates);
myTextView.setTextColor( 0xFFFF0000 )

Categories

Resources