Is it possible to specify border in android button? - android

Is it possible to specify border in Android button in the main.xml?
[p.s.
without the 'separate xml file containing stroke tag' but in the original file where I define the button and also
without the 'dynamically by programming' solution
and 'images' solution]
<Button
android:background="#FFFFFF"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Player 3"
android:layout_x="0px"
android:layout_y="0px"
android:id="#+id/p3"
android:layout_weight="1"
/>
here I am changing the background dynamically but the problem is, for 2 buttons there is no border.

Try to use shape
my_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#E8E6E7" />
</shape>
And Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_shape"
android:text="Button" />
Screenshot
I hope this will help you.

This is not the recommended way to do it because it causes overdraw and adds unnecessary views, Gunaseelan has the proper method.
There's no concept of borders as an attribute. The accepted way is to use a separate drawable as the background to the View (using stroke as you've mentioned and as #gunaseelan writes).
The other (not recommended) way is to enclose your Button in another View like a LinearLayout, set the background color to the desired border colour on the encapsulating View and padding on the outer View too.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#342334"
android:padding="5dp"
>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="whatwhaaat"
/>
</LinearLayout>
The value of the padding will indicate the thickness of the border. This method is not recommended as you end up with an extra View in your layout, and another layer of overdraw (it draws the LinearLayout then the Button on top).

Related

Creating a circle in xml file is not actually a circle

I have a gradient view which animates from left to right. I have a XML that describes the circle inside, but the borders of XML are actually rectangular as you can see, how can I make the overflow hidden of the outside of the XML.
It looks like that only the background is a circle but not the the shape itself ,I thought that the solution is by using PorterDuff.Mode but it doesn't help.
this is my circle.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/white" />
<corners android:radius="7.5dp" />
<size android:width="327dp" android:height="211.5dp" />
</shape>
And in my layout i use it like this :
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="327dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
i don't want the scanner to go outside the borders
You need to set square dimensions for it to appear as a circle.
Your width is larger than your height. Try this
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="211.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="123.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
The solution for me was to use CardView and give it cardCornerRadius.
found it here How to make a view in android with rounded corners
try this library. Use same height and width for view
https://github.com/hdodenhof/CircleImageView

edittext cursor is not visible when width is wrap content

I have edit text width background drawable and its width is wrap content. When I changed widht wrap to match parent then the cursor will visible.
I have tried android:textCursorDrawable="#null" and editext .setRawInputType(InputType.TYPE_CLASS_TEXT); but it is not working for me.
Note: Background color will change dynamically
Editext code:
<EditText
android:id="#+id/edt_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/rounded_border_text_view"
android:gravity="center"
android:inputType="textMultiLine"
android:maxLength="75"
android:padding="#dimen/dp_08"
android:textColor="#color/white"
android:textSize="#dimen/sp_30"> <requestFocus /> </EditText>
rounded_border_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="#dimen/dp_02"
android:color="#color/color_00000000" />
<solid android:color="#color/transparent_totally" />
<padding
android:bottom="#dimen/dp_05"
android:left="#dimen/dp_05"
android:right="#dimen/dp_05"
android:top="#dimen/dp_05" />
<corners android:radius="#dimen/dp_05" />
When using wrap_content there is a risk the component is just too small for you to see it.
In that case you could set a min width ? Using styles would be a good practice if you have several components in that case.

Align TextView and EditText left in vertically oriented LinearLayout

I am using a LinearLayout as view group which holds two children (TextView and EditText). My XML code looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textSize="20sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Edit Text"
android:textSize="20sp" />
</LinearLayout>
which will produce the following design:
As depicted in the zoomed-in view on the left, TextViewand EditText are not equally aligned vertically (EditText is indented a bit more as shown by the small red arrows).
It seems there is a bit of a padding (a few dp) around the hint and the line underneath which prevents them from "touching" the left edge of their view field. Is there any way to force the hint within EditText to squeeze to the left of its view field?
How can I get rid of this indentation, other than by adding paddings and margins?
Thanks for any ideas and advice!
Without add any paddings and margins you can use a custom drawable for the background of your Edittext to remove the default padding
in drawable/edittext_bg.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="#android:color/transparent" />
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
</item>
</layer-list>
Now use it in your editext:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"
android:hint="Edit Text"
android:textSize="20sp" />
Output:
It should not be in this way... You should check your style.xml, maybe you set additional margins there.
Also, try to specify layout_gravity:"left" attribute.
try typing letters on Edit Text then you can see both textview and edit text was aligned equally
Did you try Constraint layout? It's removing the need to have so much hierarchy in xml layouts and is easy to use.
this preview, run the simulator for correct preview.
But, for best result use https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

Background in a layout not appearing unless I put something in the layout

I'm trying to draw a box around two TextView objects in Android. Prior to this I had the two TextViews in a LinearLayout, but I'm working on flattening my view hierarchy to improve performance. When they were in the LinearLayout, the background showed up as desired, but when I create an empty layout with the same dimensions, I don't see the background show up at all.
My guess is this is because there isn't anything in the FrameLayout (I also tried an empty LinearLayout). I also tried setting this value at run-time, but same result.
This works fine in the layout editor in Eclipse, but when running on my tablet, the background just isn't there.
Any suggestions to accomplish the same thing or any suggestions for what I'm missing would help. Here's the shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#508E8F8E" />
<stroke
android:width="1dip"
android:color="#8E8F8E" />
<corners android:radius="4dp" />
</shape>
And here's the layout:
<FrameLayout
android:id="#+id/box_around_text"
android:layout_width="35dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#drawable/background_gray"/>
<TextView
android:id="#+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="first_text" />
<TextView
android:id="#+id/second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/first_text"
android:text="second_text" />
The issue was that the shape must have real dimensions defined somewhere. I can make it show up if I define a size in the shape itself or if I use numbered dimensions in the layout. It would never show up if I used the 'wrap_content' or 'match_parent' options.

calling setCompoundDrawablesWithIntrinsicBounds on multiple views with the same background gives inconsistent sizes

I've got a LinearLayout with two EditText descendants (not direct children). I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) on each to put a drawable (the same drawable) on the right side of each of the EditTexts. The two edit texts are identical, except for their order in the ListView. However, the size of the drawable in the first EditText becomes larger than the second after adding the drawables. Specifically, the first EditText resizes to accomodate the intrinsic size of the drawable, whereas the second EditText does not (although it does grow some small amount).
Why do two of the same drawables appear differently in the two near-identical views?
Also, I could solve this problem by figuring how to make the drawable not force the EditText to resize. As you can see, I've followed the directions at https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList regarding making the background scale, but that doesn't seem to do the trick.
the code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
style="#style/fieldLabel"
android:text="phone number"/>
<EditText
android:id="#+id/contactPhoneField"
android:inputType="phone"
android:layout_width="fill_parent"
style="#style/editableField"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
style="#style/fieldLabel"
android:text="e-mail address"/>
<EditText
android:id="#+id/contactEmailField"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
style="#style/editableField"/>
</LinearLayout>
the drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFD060"
android:endColor="#FFBB33"
android:angle="270"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
<corners android:radius="5dp" />
<stroke android:width="2dp" android:color="#FF8800"/>
</shape>
</item>
<item android:drawable="#drawable/warn"/><!-- a 35x35 px png which is taller than the native height of an EditText-->
</layer-list>
then at some point I call:
contactPhoneField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
contactEmailField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
Additionally, every subsequent time I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) (after calling it the first time) causes first EditText's size to toggle between matching the 2nd EditText and matching the drawable size!

Categories

Resources