I use shape as background in TextView.
But I have no idea how can I center text.
screen
My shape:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/colorPrimary" />
<corners android:radius="30.0dip" />
<size
android:height="50dp"
android:width="190dp" />
</shape>
</item>
</selector>
Styles:
<style name="AppTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:background">#drawable/text_view_shape</item>
<item name="android:textColor">#android:color/white</item>
</style>
My Layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tutorial_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/img_two"
tools:context="com.....TutorialFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tutorial_new_job"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView"
style="#style/AppTheme.TextView"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tutorial_its_easy"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView2"
style="#style/AppTheme.TextView"
android:layout_marginTop="34dp"
android:layout_gravity="center_horizontal" />
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/colorPrimary" />
<corners android:radius="30.0dip" />
<size
android:height="50dp"
android:width="190dp" />
<padding
android:left="50dp"
android:right="50dp">
</padding>
</shape>
</item>
</selector>
plus android:gravity="center" for TextView
Use android:gravity="center_horizontal" or android:textAlignment="center_horizontal"
Related
Can anyone please suggest about below issue.
I tried to use custom button style having drawable(curve_button.xml) file.
Colors has applied and looking nice but it seems to be issue with
<item name="android:background">#drawable/curve_button</item> as it didn't applied.
style.xml
<style name="CustomButton" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="colorButtonNormal">#color/book_appointment</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/curve_button</item>
</style>
curve_button.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<padding
android:paddingLeft="34dp"
android:paddingRight="34.4dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
/>
<corners android:radius="8dip" />
</shape>
</item>
</selector>
button layout
<Button
android:id="#+id/idBookAppontment"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Book Appointment"
android:theme="#style/CustomButton"
android:layout_marginBottom="12dp"
android:layout_marginTop="6dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="37dp"
android:inputType="textCapWords"
android:layout_gravity="bottom"
local:MvxBind="Typeface StringToFont('Effra_Rg')"
android:layout_below="#id/providerTabbarHost"
android:layout_alignParentBottom="true" />
Use this
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<padding android:paddingBottom="16dp" android:paddingLeft="34dp" android:paddingRight="34.4dp" android:paddingTop="16dp" />
<solid android:color="#color/colorAccent" />
<corners android:radius="8dip" />
</shape>
</item>
</selector>
Instead of this
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<padding
android:paddingLeft="34dp"
android:paddingRight="34.4dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
/>
<corners android:radius="8dip" />
</shape>
</item>
</selector>
I am trying to customize Switch button in Android. I have a problem that I want to show ON-OFF text on the Track not on the Thumb as Default by Android. Can we customize this.
Did you try Toggle Button? you can customize like a switch button. Im using like this.
1- Open xml in drawable folder
Your Drawable xml #switch_thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/on" />
<item android:state_checked="false" android:drawable="#drawable/off" />
</selector>
2-set background to togglebutton in layout
Your Layout xml
<ToggleButton
android:layout_width="60dp"
android:layout_height="30dp"
android:textOn=""
android:textOff=""
android:background="#drawable/switch_thumb"
android:id="#+id/toggle"
/>
3-main activity on off check and if you want to set checked
mToggle.setChecked(true);//mToggle.setChecked(prefs.getBoolean("toggle", false)
mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//put in SharedPreferences
editor.putBoolean("toggle", mToggle.isChecked());
editor.apply();
}
});
Custom Switch / Modify Switch / Write on Track / Thumb same in both case
<RelativeLayout
android:layout_width="118dp"
android:layout_height="45dp"
android:orientation="horizontal">
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/switch_room_availability"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:checked="true"
android:textOff="Taken"
android:textOn="Available"
android:thumb="#drawable/thumb_selector"
app:switchMinWidth="118dp"
app:track="#drawable/track_selector" />
<TextView
android:id="#+id/tv_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/circular_std_medium"
android:gravity="center|left"
android:paddingStart="12dp"
android:text="#string/available"
android:textColor="#color/white"
android:textSize="15sp"
android:visibility="visible" />
<TextView
android:id="#+id/tv_text_taken"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/circular_std_medium"
android:gravity="center|right"
android:paddingEnd="23dp"
android:text="Taken"
android:textColor="#color/white"
android:textSize="15sp"
android:visibility="gone" />
</RelativeLayout>
thumb_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape
android:dither="true" android:shape="rectangle"
android:useLevel="false" android:visible="true">
<solid android:color="#ffffff" />
<corners android:radius="100dp" />
<padding android:top="4dp" android:bottom="4dp"/>
<size android:width="45dp" android:height="25dp" />
<stroke android:width="15dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
track_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle"
android:useLevel="false" android:visible="true">
<solid android:color="#color/green_toggle" />
<corners android:radius="50dp" />
<size android:width="200dp" android:height="40dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle"
android:useLevel="false" android:visible="true" >
<corners android:radius="50dp" />
<size android:width="200dp" android:height="40dp" />
<solid android:color="#color/red_toggle" />
</shape>
</item>
</selector>
Here is my editext, i want to change bottom line color of edittext. i use android:backgroundTint but its only work in API 21 and above it.
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:backgroundTint="#color/edittintcolor"
android:drawableLeft="#drawable/lock"
android:hint=" Password"
android:padding="15dp"
android:textColor="#color/edittintcolor"
android:textColorHint="#color/edittintcolor"
android:textSize="18sp"
android:typeface="serif" />
I want to change this color using XML or style not in java code
bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FDEEF4" />
<corners
android:radius="15dp" />
<stroke android:width="1dip" android:color="#F9966B" />
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/bd=g"
android:hint="#string/password"
android:inputType="textPassword" >
</EditText>
Create a drawable xml for your edittext background.. Use this following code..
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<!--background color of box-->
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#android:color/white" />
<!-- color of edittext line -->
</shape>
</item>
</layer-list>
and set that drawable xml as your edittext background. :)
use command like below in edit text
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/edittextborder"
android:hint="#string/password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
then proceed with below code inside drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FDEEF4" />
<corners
android:radius="15dp" />
<stroke android:width="1dip" android:color="#F9966B" />
EDEEF4 represents the tint color
We can achieve by using two ways,
Method 1:
We can design background image using selector drawable,
create backgroundEditText.xml in drawable,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item
android:state_pressed="true"
android:drawable="#drawable/textfield_pressed" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#drawable/textfield_selected" />
<item
android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item
android:state_focused="true"
android:drawable="#drawable/textfield_disabled_selected" />
<item
android:drawable="#drawable/textfield_disabled" />
textfield_pressed.9.png
[![textfield_pressed.9][4]][4]
textfield_selected.9.png
[![textfield_selected.9][5]][5]
We need to set backgroundEditText for EditText in layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/edit1_text"
android:inputType="text"
android:background="#drawable/edit_text"
/>
</LinearLayout>
[![Final Output ][6]][6]
Method 2:
Using android.support.v4 support library, we can give backward compatibility.
You can use this site to generate your own EditText style.
Generate drawable and set it as background, like:
android:background="#drawable/generated_theme_edit_text"
I want to create a button as it is in the first picture. But my output is like in the second picture. How can i achieve the correct spacing between image and text in the button and also the shadow as it is in the first picture. How can i achieve this.
My code is as follows
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:left="2dp" android:top="4dp">
<shape>
<solid android:color="#1E90FF" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<!-- SHADOW LAYER -->
<item android:left="4dp" android:top="4dp">
<shape>
<solid android:color="#C0C0C0" />
</shape>
</item>
<!-- CONTENT LAYER -->
<item android:bottom="5dp" android:right="4dp">
<shape>
<solid android:color="#DADADA" />
</shape>
</item>
</layer-list>
</item>
try with this code this is running code you can use this code ..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/lla"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#CCCCCC"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="FIND PRODUCTS"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="#drawable/ic_launcher"
/>
</LinearLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><layer-list>
<item android:left="2dp" android:top="4dp"><shape>
<solid android:color="#1E90FF" />
</shape></item>
</layer-list></item>
<item><layer-list>
<!-- SHADOW LAYER -->
<item android:left="3dp" android:top="3dp"><shape>
<solid android:color="#C0C0C0" />
</shape></item>
<!-- CONTENT LAYER -->
<item android:bottom="3dp" android:right="3dp"><shape>
<solid android:color="#DADADA" />
</shape></item>
</layer-list></item>
</selector>
xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ImageView
android:id="#+id/iconLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:src="#drawable/ic_photos" />
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/iconLeft"
android:text="Find Product"
android:textSize="16sp" />
<ImageView
android:id="#+id/iconRight"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:src="#drawable/ic_photos" />
</RelativeLayout>
How do I change the color of a Relative Layout Shape ? To work it like a button.
I tried to do it using a selector.
This is the code i used.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlGPS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="#drawable/relative_selector"
>
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tvs1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_below="#+id/tvl1"
android:layout_toLeftOf="#+id/iv1"
android:text="you can here protect ... "
android:textColor="#color/gray" />
<TextView
android:id="#+id/tvl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/iv1"
android:text="GPS"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</RelativeLayout>
relative_selector.xml In here I'm doing the changes using selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/relative_focus"/>
<item android:state_pressed="true" android:state_enabled="false"
android:drawable="#drawable/relative_clicked" />
<item android:drawable="#drawable/relative_background"/>
</selector>
relative_background.xml This is the shape.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke android:width="1dp" android:color="#636161" />
<padding android:left="5dp"
android:top="2dp"
android:right="2dp"
android:bottom="5dp" />
<corners android:radius="6dp" />
<solid android:color="#88ffffff"
/>
</shape>
In "relative_clicked.xml" and "relative_focus.xml" I only changed the colors.
relative_focus.xml
<stroke android:width="1dp" android:color="#ffff00" />
relative_clicked.xml
<solid android:color="#88ffff00"/>
All .xml files are in drawable folder.
I want to work this like a button.Is someone having any idea?
From your selector xml:
<item android:state_pressed="true"
android:state_enabled="false"
android:drawable="#drawable/relative_clicked" />
Will it work if you remove state_enabled?
<item android:state_pressed="true"
android:drawable="#drawable/relative_clicked" />