EditText with permanent indent at start - java

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

Related

How do I set constraints and programmatically increase image in proportion to text size?

This is perhaps a bit hard to explain, so I included a picture to show you what I mean.
I have three images in Android Studio. They should all be alined to match and the middle part should expand as the text on top of the graphic expands (the middle height should equal the text height I suppose). Have been sitting with this for awhile now but don't get anywhere.
Do I add code in xml? Something like android:layout_height="#id/textView"
Is it better to do relative constraints in xml or constraints in UI?
In xml, how do I do relative constraints to other children? I know code like android:layout_alignParentTop, android:layout_alignChildTop, but how do I set distance to children?
If I read this correctly, you want the centre image and text view to adjust height based on the height of the text. The simplest way of doing that would be using your middle image as a stretchable background to the text view and similarly for the header and footer views, preferably using 9-patch PNG images.
Solution 1: Using 9-patch PNG images
Here is an example XML with 9-patches to show how this works.
N.B. The example contains two layouts, each enclosing a single TextView with header and footer views. The first TextView has a single line of text, the second has two lines.
<LinearLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/top" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="32sp"
android:padding="5dp"
android:gravity="center_horizontal"
android:text="A line of text"
android:background="#drawable/middle" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/bottom" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<LinearLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/top" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="32sp"
android:padding="5dp"
android:gravity="center_horizontal"
android:text="A line of text\nAnother line of text"
android:background="#drawable/middle" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/bottom" />
</LinearLayout>
Nine-Patches
top.9.png:
middle.9.png:
bottom.9.png:
Example output
Solution 2: All XML
Alternatively, you could do it all in XML using <shape> and <layer-list> drawables instead of the 9-patch PNG images. Here is a modified version of the above to work with XML drawables.
This example shows rounded corners on the border as an added bonus, but beware it can be tricky to draw complex compound shapes using XML as there are only a small number of primitive shapes you can use.
Note also that this version relies on the addition of dimension resources to ensure everything matches up.
res/values/dimens.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android" >
<dimen name="layout_width">320dp</dimen>
<dimen name="header_height">15dp</dimen>
<dimen name="header_inset_height">5dp</dimen>
<dimen name="inset">5dp</dimen>
<dimen name="inset_width">310dp</dimen>
<dimen name="footer_height">15dp</dimen>
<dimen name="corner_radius">10dp</dimen>
<dimen name="inset_radius">5dp</dimen>
</resources>
res/drawable-nodpi/header.xml
<?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="#ffff00ff" />
<corners
android:radius="#dimen/corner_radius"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp" />
<size
android:width="#dimen/layout_width"
android:height="#dimen/header_height" />
</shape>
</item>
<item
android:top="#dimen/inset"
android:left="#dimen/inset"
android:right="#dimen/inset">
<shape android:shape="rectangle">
<solid android:color="#ffffff00" />
<corners
android:radius="#dimen/inset_radius"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp" />
<size
android:width="#dimen/inset_width"
android:height="#dimen/header_inset_height" />
</shape>
</item>
</layer-list>
res/drawable-nodpi/body.xml
<?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="#ffff00ff" />
<size android:width="#dimen/layout_width"/>
</shape>
</item>
<item
android:left="#dimen/inset"
android:right="#dimen/inset">
<shape android:shape="rectangle">
<solid android:color="#ffffff00" />
<size android:width="#dimen/inset_width" />
</shape>
</item>
</layer-list>
res/drawable-nodpi/footer.xml
<?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="#ffff00ff" />
<corners
android:radius="#dimen/corner_radius"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
<size
android:width="#dimen/layout_width"
android:height="#dimen/footer_height" />
</shape>
</item>
<item
android:bottom="#dimen/inset"
android:left="#dimen/inset"
android:right="#dimen/inset">
<shape android:shape="rectangle">
<solid android:color="#ffffff00" />
<corners
android:radius="#dimen/inset_radius"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
<size
android:width="#dimen/inset_width"
android:height="#dimen/header_inset_height" />
</shape>
</item>
</layer-list>
res/layout/activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="#dimen/layout_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="#dimen/header_height"
android:background="#drawable/header" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="32sp"
android:padding="5dp"
android:gravity="center_horizontal"
android:text="A line of text"
android:background="#drawable/body" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/footer_height"
android:background="#drawable/footer" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<LinearLayout
android:layout_width="#dimen/layout_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="#dimen/header_height"
android:background="#drawable/header" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="32sp"
android:padding="5dp"
android:gravity="center_horizontal"
android:text="A line of text\nAnother line of text"
android:background="#drawable/body" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/footer_height"
android:background="#drawable/footer" />
</LinearLayout>
</LinearLayout>
Example output:

EditText Insertion Point same color as text (#null not working)

I want to make the EditText Cursor color same as text but:
android:textCursorDrawable="#null"
isn't working.
I changed it using the primeryAccent color in the themes(style) but would prefer if it just takes the color of text(so I wouldn't make many styles for each editText). Am I missing something?
The editText:
<EditText
android:id="#+id/search_value"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent"
android:inputType="text"
android:hint="Search People..."
android:layout_toRightOf="#id/arrow_search_indicator"
android:layout_alignTop="#id/search_container"
android:layout_alignEnd="#id/search_container"
android:layout_alignBottom="#id/search_container"
android:layout_marginStart="12dp"
android:textColor="#color/white"
android:textColorHint="#color/search_hint_blue"
android:textCursorDrawable="#null"
android:maxLines="1"
android:imeOptions="actionDone"/>
Edited here:
Image:
That pink thing(Sorry if i misnamed it)
Edittext cursor color you want changes your color.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#drawable/color_cursor"
/>
Then create drawalble xml: color_cursor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>

Java android change color in xml file

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

change button background when clicked (xml)

I want that when the user clicks the button, the background of it will change a bit to get the effect that it has been clicked.
The fragment of my activity XML file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnParse"
android:text="#string/get_games"
android:textColor="#FFF5F2"
android:textSize="20sp"
android:layout_marginBottom="93dp"
android:layout_alignParentBottom="true"
android:background="#drawable/buttonshape"
android:textStyle="italic"
android:textAlignment="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
Button shape XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent">
<corners
android:radius="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<gradient
android:angle="45"
android:centerX="0"
android:centerColor="#A80C16"
android:startColor="#E8E8E8"
android:endColor="#FF0F0F"
android:type="linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<padding
android:left="42dp"
android:top="0dp"
android:right="50dp"
android:bottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<size
android:width="270dp"
android:height="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<stroke
android:width="3dp"
android:color="#878787"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</shape>
How can I achieve this effect? I tried to implement what I found on similar stackoverflow questions pages but it didn't work. I'm pretty new in Android development so I may don't know some basics...
Hy you can refere to this link
http://www.viralandroid.com/2015/09/how-to-change-android-button-on-click-press-color.html
and this one
how to change background image of button when clicked/focused?
Hope this will help you..Thanks

ImageView doesn't show inside LinearLayout with layer-list as background

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.

Categories

Resources