Round shaped button getting distorted with length of text inside. - android

I'm trying to have a perfectly round shaped button.
I have tried with the below code using shape.
but issue i am facing is that when the view has two text in it, it looks almost a circle but as soon as the view has one and three character in it it looks oval shaped.
shape code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="true">
<corners android:bottomRightRadius="20dip"
android:bottomLeftRadius="20dip"
android:topRightRadius="20dip"
android:topLeftRadius="20dip"
/>
</shape>
Please check the nearly rounded shaped button where number 2 and 22 are written in the picture
I have used the same code for all of the buttons(mentioned above)

In your layout file (or in code), keep the dimensions of the Button fixed (width=height=some dp).
In example:
layout_width="48dp"
layout_height="48dp"

Related

How to set background color of an image, but within the border?

My goal is to display a circular image, and allow the user to set its foreground and background colors respectively, for example:
My attempt was to create an image asset with transparent background, then use ImageView::setColorFilter to change its foreground, and use ImageView::setBackgroundColor to set its background. The image asset looks like this:
My problem is that pixels outside of what we humans call 'border' are also transparent, so the result looks like this:
How do people usually deal with this issue? Although I was doing Android development, but any ideas or code snippets in any language are appreciated!
at the first, you must create a new drawable file and write below code for creating the circle with yellow color with blue border:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#cab816" />
<size android:width="50dp"
android:height="50dp"/>
<stroke android:width="2dp"
android:color="#color/darkBlue"/>
</shape>
and now you must set this drawable to the background of your imageview and set your image with src.
if your image covered all of this circle, use padding for decrease image size.

Best way of creating an Animation?

I want to create a simple animation for my Android Application.
I have a very big Image inside an ImageView (1900 x 1200). This Image has small stripes which are transparent so that the background of the View is visible inside of the transparent stripes.
Basically i now need the background to be one color and i need a small white stripe moving from the top of the screen to the bottom of the screen. That should be repeated infinitly. You can imagine of it like the front light animation of the black car in Knight Rider.
I tried to implement that using another image in the background which i moved like that:
ObjectAnimator repeatAnimation = ObjectAnimator.ofFloat(this.imageViewLightFloatLeft, "y", -Helper.getDisplayHeight(this.getActivity()), Helper.getDisplayHeight(this.getActivity()));
repeatAnimation.setDuration(2000);
repeatAnimation.setRepeatCount(ObjectAnimator.INFINITE);
repeatAnimation.setRepeatMode(ObjectAnimator.RESTART);
repeatAnimation.start();
Unfortunately this is not a good solution because it is lagging really hard. As i mentioned i have an ImageView with a big Image (1900 x 1200) in the front and it is slowing down the animation rapidly.
So i need another solution for what i want to do.
How can i implement my animation?
Using a Gradient as the Background of the View and animate that Gradient does the trick:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:centerX="0.5"
android:centerY="0.5"
android:endColor="#000000"
android:gradientRadius="1000"
android:startColor="#85E9FF"
android:type="radial" />
</shape>

Need a gradient on the bottom of the screen

In the image below is the effect I want(notice the subtle green at the bottom of the screen). I also want to use it as multiple screens so I want it to be the background. Is there any way to achieve this?
Set the png as background, or create a shape in xml:
Create an xml file - let's call it "gradient_background.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#ff0000ff"
android:endColor="#00ffffff"/>
<corners android:radius="10dp" />
</shape>
Change the hex color values to the ones you want.
and add it as background to your ViewGroup e.g. LinearLayout:
android:background="#drawable/gradient_background"
EDIT:
To achieve what you mentioned in your comment, that the gradient height should stay fixed while positioned at the bottom, but the white area can stretch vertically, I suggest you use a nine-patch which you can create with the Draw Nine Patch Tool. Launch the tool from your SDK's tools folder - click the nine-patch bat file (and wait a while for it to launch, then import your png). You then draw black lines along the sides of your image to define which parts can be stretched, name the file something.9.png and reference it as background in your ViewGroup. Please see the linked-to documentation for details.

Round ring button in Android

I want to create a button background (or button itself) that look exactly like the below one.
I'm doing it currently with an image. I tried to create a similar one using the following XML, but it doesn't look as expected.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#f0600000"/>
<stroke
android:width="10dp"
android:color="#FFFF6666"/>
</shape>
Actually I want a round button with 3 pixel shadow and 1/4 width stroke in red color around the white circle. I have not succeeded with the shadow part at all. Thanks for any sort of help.
Well, the solid fills the middle and stroke paints the border. Since you want 3 colors you will have to use 2 drawables, painting white circle over the red circle with grey border. You can then use LayerDrawable to keep these as one unit

How do I create the equivalent of a css outset border in Android?

If you create an outset border in CSS the browser varies the border colour for each edge to make the shape appear to protrude from its parent.
Is there an easy way to do this in an android layout or do I need to set each line colour manually?
Update - added example below:
Example http://www.witzelsucht.co.uk/googleplusheader.png
Set the background of an ImageView to a 9 patched drawable resource with the desired shadow/bevel around the edge. Lets say it takes 5 pixels to create the effect you want. Then set the padding of the ImageView to 5 pixels. Then set the bitmap to any image.
ImageView.setBackgroundDrawable
ImageView.setPadding
ImageView.setImageBitmap
Even easier, use a shape with a stroked border and set it as the background of the view:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000" />
<stroke android:width="1px" android:color="#ffffff" /></shape>
I'm not aware of any native support for Views to have borders. Your question reminded me of this question that I was looking at recently:
Is there an easy way to add a border to the top and bottom of an Android View?

Categories

Resources