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>
Related
How can I change color of rounded corners textView or button with transparent background like this
I want when clicked again the button unselect , not only select
Add xml file in drawable folder bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<stroke
android:height="1.0dip"
android:width="1.0dip"
android:color="#ffee82ee" />
<solid android:color="#android:color/transparent"/>
<corners android:radius="7dp"/>
</shape>
and in layout
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg"
android:textColor="#ffee82ee"/>
It will work..
You can use a selector with multiple state as drawable for background and text color.
<Button
android:id="#+id/button1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
drawable xml file :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
You have to create a background with a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/likeactivepressed" />
<item android:state_pressed="true" android:drawable="#drawable/likeinitialpressed"/>
<item android:state_checked="true" android:drawable="#drawable/likeon"/>
<item android:drawable="#drawable/likeinitial"/>
</selector>
then set your background to your button :
android:background="#drawable/like_button"
You have different options:
Just use the MaterialButton in the Material Components library with the Widget.MaterialComponents.Button.OutlinedButton style and the app:cornerRadius, app:strokeColor attributes.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="BUTTON"
app:strokeColor="#color/myColor"
app:cornerRadius="16dp"
../>
To achieve the selection of the buttons you can use a MaterialButtonToggleGroup.
Something like:
<com.google.android.material.button.MaterialButtonToggleGroup
app:singleSelection="true"
...>
<com.google.android.material.button.MaterialButton
.../>
</com.google.android.material.button.MaterialButtonToggleGroup>
Use a Chip
Something like:
<com.google.android.material.chip.Chip
style="#style/Widget.MaterialComponents.Chip.Entry"
app:chipCornerRadius="16dp"
android:text="Chip"
.../>
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>
How to use Custom textColor for TextView inside Custom ListView? What i need is i have my listview with dark gray background. If i select any row. That row should become white and TextView Color should become dark gray. Can anybody give me snippet or lead me to some nice tutorial?
I tried this. Below is my custom row for ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_selector" >
<TextView
android:id="#+id/rowListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="#string/app_name"
android:textColor="#color/list_item_text"
android:textSize="10sp"
android:textStyle="bold" />
</RelativeLayout>
And this is xml from res/color/ folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/dark_gray"/>
<item android:state_focused="true" android:color="#color/dark_gray"/>
<item android:state_pressed="true" android:color="#color/dark_gray"/>
<item android:color="#color/white"/>
</selector>
It doesnt workout for me. I am able to set row color to white but i am not able to set text color to gray!
i am new to android. help me with this. Thanks in advance!
Try this color state list for textColor
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_activated="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
Android guide does not mention state_activated attribute but it works for me.
My selector is:
<item android:state_pressed="true" android:color="#color/bg_grey"/>
<item android:state_focused="true" android:color="#color/bg_grey"/>
<item android:color="#color/white"/>
and i have my TextView as:
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/textview_selector"
android:padding="5dp"
android:text="Some Text"
android:textColor="#color/text_grey"
android:textSize="16dp" />
This way i am getting:
android.view.InflateException: Binary xml file line #68: Error inflating class <unknown>
if i am using this selector as android:textColor="#drawable/textview_selector" its working fine
The problem here is that you cannot define the background color using a color selector, you need a drawable selector. So, the necessary changes would look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/selected_state" />
</selector>
You would also need to move that resource to the drawable directory where it would make more sense since it's not a color selector per se.
Then you would have to create the res/drawable/selected_state.xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/bg_grey" />
</shape>
and finally, you would use it like this:
android:background="#drawable/selector"
You are defining Color in place of a background Drawable in Selectors xml,try with backgrounds
drawable
you just need to ensure the colors are in colors.xml file and do not put the hex colors directly into the selector items.
my_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/category_item_selected_color" android:state_selected="true"/>
<item android:drawable="#color/category_item_selected_color" android:state_pressed="true"/>
<item android:drawable="#color/white" />
</selector>
and make your textview look like this:
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:letterSpacing="0.05"
android:textColor="#000000"
android:gravity="center"
android:textSize="17sp"
tools:text="New In"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#drawable/my_selector"
android:layout_gravity="center"
/>
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.