Button with distinct corners and an Image at centre - android

I need a button with left side rounded corners and right side flat corners.
complete rounded corners is possible with this code:
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp"/>
Resultant image:
Now I need an image similar to following one:
I've tried the follwing code:(Sorry..This is an answer.But it was not reflecting in Xml)
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
But it is the not the correct one.
To centre an image I tried the following code:
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/border2"
android:layout_weight="0.5"
android:layout_margin="10dp"
android:paddingRight="50dp"
android:paddingLeft="50dp"
android:drawableRight="#drawable/facebook" />
This fixed an image at centre.But is there any other way to do it?

If nothing else helps, you can use a 9-patch image.

Related

Button Get Shrink as I Press onClick Android

I am Working with the Buttons Which have a background that I have Created in Drawable xml File. The Buttons look Fine in Designing But on Runtime as I Press Click, its size get Shrink and When I Click on Other Button in Design the Previous button Size goes to initial Android...
here is My Button Xml File
<Button
android:id="#+id/btn_facbookid"
android:layout_width="#dimen/_95sdp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_15sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/button_bg_rounded_corners_main"
android:fontFamily="#font/poppins_light"
android:text="#string/url"
android:textColor="#color/textcolorblack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv_facebook"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/btn_facbookurl"
/>
<Button
android:id="#+id/btn_facbookurl"
android:layout_width="#dimen/_180sdp"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_15sdp"
android:layout_marginTop="#dimen/_15sdp"
android:fontFamily="#font/poppins_light"
android:background="#drawable/button_bg_rounded_corners"
android:text="#string/facebookid"
app:layout_constraintStart_toStartOf="parent"
android:textColor="#color/whitecolor"
app:layout_constraintTop_toBottomOf="#id/tv_facebook"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/btn_facbookid"
/>
The selector drawable Layout layut i have Created for buttons is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#8420CA"
android:endColor="#46CEFC"
android:angle="45"/>
<stroke android:color="#color/transparent" android:width="4dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="#dimen/_50sdp" />
</shape>
</item>
The Scenario is as i Click on Button It Changes its color, It changes the color but Button get Shrinked
btn_facbookurl.setOnClickListener(View.OnClickListener {
btn_facbookurl.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners)
btn_facbookid.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners_main)
btn_facbookurl.setTextColor(getColor(R.color.whitecolor))
btn_facbookid.setTextColor(getColor(R.color.textcolorblack))
url_facebookedit.setHint("Enter Facebook ID Here")
})
btn_facbookid.setOnClickListener(View.OnClickListener {
btn_facbookid.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners)
btn_facbookurl.background=
ContextCompat.getDrawable(this,R.drawable.button_bg_rounded_corners_main)
btn_facbookid.setTextColor(getColor(R.color.whitecolor))
btn_facbookurl.setTextColor(getColor(R.color.textcolorblack))
url_facebookedit.setHint("Enter Facebook URL Here")
})
here is the UI
As I click on Button Facebook it Shrink and Come to origional Size as I Click on URL and Vice Versa...
You're adding a transparent stroke around the button (which makes the filled area smaller)
<stroke android:color="#color/transparent" android:width="4dp" />
I assume that file is R.drawable.button_bg_rounded_corners, and the stroke isn't there in R.drawable.button_bg_rounded_corners_main.
Also check your corner radius, I don't know what sdp is but if the radius doesn't match in both files, and one is slightly larger, it might affect the size since it's defining the entire shape and not just the corners. Maybe!
For your Button Shrink case, You should add android:layout_height value.
Specifies the basic height of the view. This is a required attribute
for any view inside of a containing layout manager
Don't
<Button
android:layout_height="wrap_content"
Try with
<Button
android:layout_height="#dimen/_50sdp"

Android studio TextInputEditText Style with Oval edges is working but when it expands it becomes ugly,how to fix the edges

So, I created a TextInputEditText with oval edges using this code:
XML Oval Shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp"/>
<stroke android:width="1dp"
android:color="#44433A"/>
<solid android:color="#FFF"/>
here is the TextInputEditText:(I know the style should be inside the style.xml file)
<android.support.design.widget.TextInputEditText
android:id="#+id/Ti1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginEnd="60dp"
android:layout_marginTop="10dp"
android:background="#drawable/oval"
android:elegantTextHeight="true"
android:hint="#string/hint_Note"
android:inputType="textCapSentences|textMultiLine"
android:maxLines="8"
android:paddingBottom="5dp"
android:paddingEnd="50dp"
android:paddingStart="20dp"
android:paddingTop="5dp"
/>
here you see that when a person goes to the 2nd line it expands to a maximum set as 8 lines,but my problem here is when it expands it starts forming a full oval but i want it to become this:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<stroke android:width="1dp"
android:color="#44433A"/>
<solid android:color="#FFF"/>
my first assumption was that I should change the background when the lines expand further than 1 line, but I don't know if this is a good solution or not,is there a better solution, I am trying to mimic the new Whatsapp bottom EditText behavior when it comes to expanding the EditText.
I am able to reach this shape on the edges
as the line expands the edge creates a small vertical line between the round edges of the edittext
as you might see that upon reaching 3 lines the line is still increasing in length on both left and right sides
another example upon reaching 4 lines
but when I try to write something in my TextInputEditText it expands but it does not create a straight vertical line on the left and right edges, it starts to form an oval.

How to design button with gradient and shadow for Android?

How do I create buttons like the one below in Android? For the most part I have the main section of the button down. I can't seem to figure out the shadow and the clicking animation.
Button
<Button
android:text="Connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connectionStatus"
android:layout_centerHorizontal="true"
android:id="#+id/button"
android:background="#drawable/button_background"
android:minWidth="300dp"
android:layout_marginTop="32dp"
android:onClick="onClick"
android:textColor="#fff"
/>
Here is the drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#21D4FD"
android:endColor="#B721FF"
android:angle="45"
/>
<corners android:bottomLeftRadius="20dip"
android:topRightRadius="20dip"
android:topLeftRadius="20dip"
android:bottomRightRadius="20dip"
/>
</shape>
This is what I currently have:
This is what I want to have:
You should try This to make gradient on your button.
to know more please click Here
I found a solution for that 4 Buttons in your image. I combined a library with my own drawable and a trick and reached a Button like you were searching for. Take a look at this and here is my Spoiler:

Is it possible to specify border in android button?

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).

How to get a thumbnail to fit inside an imageview?

I am getting thumbnails from a video that i just recorded and I need to get the thumbnail to fit into an imageview. I am using this to call the thumbnail :
Bitmap curThumb = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
And I am using this to call the imageview:
image.setImageBitmap(curThumb);
My xml for my imageview looks as such:
<ImageView android:id="#+id/iView" android:paddingRight="10px" android:layout_height="280dp" android:layout_width="320dp" android:layout_gravity="center_horizontal" android:paddingTop="5dp" android:paddingBottom="5dp" android:background="#drawable/border"></ImageView>
The Border that I am using for my background of the image view is an xml which gives the view a nice white border frame, this is follwing xml for it:
<shape xmlns:android="http://schemas.android.com/apk/res/android" shape="oval">
<stroke android:width="3dp" android:color="#ffffff" />
<padding android:left="0px" android:top="1dp" android:right="0px"
android:bottom="1dp" />
</shape>
I don't know if its because I am using a MINI_KIND thumbnail but i need to cut it down by a little so it can fit into the imageview perfectly. Any help will suffice. Thanks in advance.
Try adding android:scaleType="fitCenter" to the ImageView's attributes.
This will center the image and ensure that (assuming it's larger than the ImageView's area) the image covers the entire area and that the image is completely visible along at least one axis.
Set your scaletype to centercrop http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

Categories

Resources