I have this xml where I already set stroke color but I sometimes I want to change stoke color programmatically. The file name is dummy.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff" />
<stroke
android:width="1dp"
android:color="#ff000000" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
I want to change color (stroke color) programmatically, how I can do this?
I use this xml here :
<LinearLayout
android:layout_marginTop="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a"
android:layout_gravity="center"
android:background="#drawable/dummy"
android:layout_marginBottom="2sp"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:id="#+id/serverStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="#drawable/shadow_green"
android:padding="2dp">
<ImageView
android:id="#+id/serverStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_settings_input_antenna_white_24dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/locationStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shadow_red"
android:padding="2dp">
<ImageView
android:id="#+id/locationStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_warning_white_24dp" />
</RelativeLayout>
</LinearLayout>
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a);
You can change the stroke as
GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setStroke(3, Color.RED);
And can change solid color as
GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setColor(Color.RED);
Hope this helps
Related
I am trying to create an app and I have a design already as an image, but I can not clone the exact shape of the button which is above the tabs
I expect the same design in this image: below
Note:
0 and 0.00EGP should be variables
Create a XML file bottom_corner.xml for background
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#0091fa"/>
<stroke android:width="1dp"
android:color="#0091fa" />
<corners android:radius="10dp"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
Now create layout floated_button.xml and use this background here in the layout:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/bottom_corner"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tv_total_value"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="0"
android:textSize="18dp"
android:gravity="center"
android:layout_centerVertical="true"
android:textColor="#fff"/>
<TextView
android:id="#+id/tv_egp_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00 EGP"
android:textSize="22dp"
android:gravity="center"
android:paddingRight="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#fff"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#fff"
android:layout_toRightOf="#+id/tv_total_value"/>
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Charge"
android:textSize="22dp"
android:gravity="center"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/tv_total_value"
android:textColor="#fff"/>
</RelativeLayout>
For your specific problem, you can move forward like this:
First of all, you have to create a drawable file of round background.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="15dp" />
</shape>
Then, you can add the tag of image and place it, on the corner of the image where you want to place it. Then use drawable as background then adjust the TextView on it.
<ImageView
android:id="#+id/ivLauncher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/ivBg"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="#drawable/round_text_bg"
app:layout_constraintTop_toBottomOf="#id/ivLauncher"
app:layout_constraintBottom_toBottomOf="#id/ivLauncher"
app:layout_constraintLeft_toRightOf="#id/ivLauncher"
app:layout_constraintRight_toRightOf="#id/ivLauncher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#FFFFFF"
android:textSize="10dp"
app:layout_constraintTop_toTopOf="#id/ivBg"
app:layout_constraintBottom_toBottomOf="#id/ivBg"
app:layout_constraintLeft_toLeftOf="#id/ivBg"
app:layout_constraintRight_toRightOf="#id/ivBg"/>
Here is the result:
Flotted Button design on image corner
My EditText has a background that makes it round and with a stroke of 3dp, I am trying to have a permanent indent implemented so that the text doesn't start in the stroke of the background. All ive come across is android: paddingStart/paddingEnd and I can not seem to get it to work. Is there any other way of achieving this programmatically or no? I would want it about 1dp past the stroke or maybe 3dp but I can't find a way to achieve this.
xml
<EditText android:id="#+id/w_SearchBarFullScreen"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="355dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/web_search_bar"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:text=" "
android:elevation="5dp"
android:focusable="true"/>
background xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff" />
<stroke
android:width="3dp"
android:color="#e0e0e0" />
<corners
android:radius="15dp"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
Remove padding from background.xml
use paddingLeft="10dp"
in Edittext
Try this...
no need to set fixed height and width for edittext here.
.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.silambarasan.test.MainActivity"
>
<EditText
android:id="#+id/w_SearchBarFullScreen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:background="#drawable/web_search_bar"
android:elevation="5dp"
android:focusable="true"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
web_search_bar.xml (Drawable)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff" />
<stroke
android:width="3dp"
android:color="#e0e0e0" />
<corners
android:radius="15dp"/>
<padding
android:left="15dp"
android:top="15dp"
android:right="15dp"
android:bottom="15dp" />
</shape>
Result:
The padding in the background was stopping me from doing this so in android:paddingLeft = 10dp is the correct not in the layoutXML file rather the background
Hi I try do this I have a LinearLayout and how I can set two colors background inside a layout. I want to that half is a white and half is a black.
I tried this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<layer-list>
<item>
<shape>
<corners android:radius="5dip"/>
<gradient
android:endColor="#fff"
android:startColor="#000" />
</shape>
</item>
</layer-list>
</item>
</selector>
This is my code from xml ;
<FrameLayout 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:id="#+id/frame"
android:background="#color/background"
tools:context="pl.eltegps.smokkomunikator.ui.fragment.TopBarFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/rel11"
android:layout_weight="0.5">
<ImageView
android:id="#+id/iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:src="#drawable/profil" />
<TextView
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/iv"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:clickable="true"
android:text="aaaaaaa"
android:textColor="#color/textColor" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/settings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/currentLocation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left"
android:textColor="#fff" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:antialias="true"
android:background="#drawable/menu"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lin"
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="center"
android:background="#color/textColor" />
<LinearLayout
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="2sp"
android:layout_marginTop="2dp"
android:background="#drawable/dummy"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:id="#+id/serverStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:background="#drawable/shadow_green"
android:padding="2dp">
<ImageView
android:id="#+id/serverStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_settings_input_antenna_white_24dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/locationStatusWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shadow_red"
android:padding="2dp">
<ImageView
android:id="#+id/locationStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:antialias="true"
android:src="#drawable/ic_warning_white_24dp" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
dummy.xml :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff" />
<stroke
android:width="1dp"
android:color="#ff000000" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
And I try do this : what is under the view I want to setbackground color black
Try
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Follow this steps :
Step 1: Create one background.xml file and put that file in drawable folder.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="20dp">
<shape android:shape="rectangle" >
<size android:height="100dp" />
<solid android:color="#ffffff" />
</shape>
</item>
<item android:top="300dp">
<shape android:shape="rectangle" >
<size android:height="10dp" />
<solid android:color="#000000" />
</shape>
</item>
Step 2 : In your Main.xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keypad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/background">
Output :
Each ViewGroup can only have one background color, but you can easily achieve what you want by creating two ViewGroup elements.
Like.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff00"/>
I have the following layer-list
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF000000"/>
<stroke android:width="0dp" android:color="#FF000000" />
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
</item>
</layer-list>
This resource is a black background with some rounded border (Top left and top Right). I want put it in LinearLayout as background property and inside LinearLayour I want put a white Textview and a ImageView. I have the following code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll"
android:orientation="horizontal"
android:background="#drawable/profile_background_head_item">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/origin"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_config"/>
</LinearLayout>
The problem is that ImageView doesn't show, I think that it is due that my layer-list code, but I don't know how to solve it.
Any ideas?
Thanks
Your TextView's width is match_parent, and in a horizontal LinearLayout, that would push the ImageView offscreen.
I suggest using weights, try:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll"
android:orientation="horizontal"
android:background="#drawable/profile_background_head_item">
<TextView
android:id="#+id/txt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/origin"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_config"/>
</LinearLayout>
This should have the TextView take up the rest of the horizontal space the ImageView leaves.
I have a little problem with my edittext fields, when you type in it, the whole field turns black when I type # . - or any other symble.
<TableRow
android:id="#+id/tableEmail"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/cell_shape">
<TextView
android:id="#+id/textViewEmail"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:ems="10"
android:gravity="top"
android:text="#string/email"
android:textColor="#000000" />
<EditText
android:id="#+id/inputEmail"
android:layout_width="370dp"
android:layout_height="35dp"
android:background="#drawable/edit"
android:layout_marginBottom="2dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:ems="10"
android:inputType="text" >
</EditText>
</TableRow>
I did try to chage the input type to textEmail, but it is the same.
My cell_shape looks 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="#fff"/>
<stroke android:width="1dp" android:color="#000"/>
</shape>
any ides?
edit
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#ffffff" />
</shape>