What is the easiest way to create a Circle in Android Studio (1.2)?
I understand I can do it through using canvas code or writing XML code somewhere, but this seems really complex. None of the guides on the internet seem to provide a simple way. Simple would be drag-and-drop from the menu in Design view of Android Studio, jus tlike widgets.
Been searching for all of today, you can imagine how frustrating this incredibly simple thing is.
The best way to create a circle in Android is using XML drawable.
They don't have an exact circle shape, but oval shape will do the same job
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid android:color="#000000"/>
</shape>
Related
When making a website with html and css you can simply make a box by giving a couple of div tags some attributes like color and such, what is the Android xml equivalent to this? I need to make a layout with simple elements like this example, whether it be just a rectangle or a circle, that can rescale with the screen. Is there a way to do that? Even a link to a tutorial would be greatly appreciated, i could not find anything myself.
what is the Android xml equivalent to this?
To the extent that there is an equivalent, you could use a a ShapeDrawable, used in an ImageView or possibly as a background to something else.
Please understand that not everything that is easy in Web development will be easy in other platforms, and not everything that is difficult in Web development will be difficult in other platforms.
You can use View and give it a background.
If you want a rectangle, you can just assign the color you want:
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" />
If you want to have a circle as background, or something other, you can create a drawable file using shapes:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#000000" />
</shape>
and assigning it to the view:
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/circle" />
where "circle" is the name of the file with the shape
The Fastest way to make custom buttons and any kind of background.
Oval, Rectangle etc.
In your case as i am getting is that you want an rectangle like we do in HTML/CSS.
Here is an video with very much clarity, i found :
https://www.youtube.com/watch?v=WkJTunLf5iE
In this video he described about custom buttons but you can use it same with your required background and set it to you view like this :
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rectangle" />
That's it !!
Ok, so I'm working on a very simple Android homescreen widget that fetches the latest Image from an imageboard and displays it in a imageview.
I'd like to have the option to Style this widget, to be in keeping with Googles Material design guidelines, however, I hit the following problem whilst developing and testing on my Nexus 5 running Lollipop 5.1 -
I can't seem to use a cardview in a widget. It gives me a class not found exception however I do not get such exceptions previewing the layout and my gradle has the right dependancies so I suspect it is more to do with widget compatibility?
So I did some further reading and on the Google API page about widgets it has a very specific list of views that widgets support, which did not, to my surprise include cardviews. So I've gone back to my plain old Imageview
I guess my question then sits with, how would I get this imageview to look like a material design card? EG, rounded corners, the subtle 3d effect, drop shadow, ETC. I do not wish to give the imageview a border, the edge of the image should extend to the edges and get clipped by the rounded corners.
I have tried by simply setting the style of the Imageview to be that of a cardview, but that seems to have done absolutely nada :)
Anyone has some ideas? Thank you!
you might want to have a look at the source of the google i/o app : https://github.com/google/iosched/blob/master/android/src/main/res/layout/widget.xml
This is from a long time ago, but it's very doable to make a simple card background in XML.
The way to do this is to create an cardview-esque .xml file in your drawables app resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:radius="4dp" />
</shape>
and set the background view of your Widget's XML layout as
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/widget_card_background"
android:orientation="vertical">
<.../>
</LinearLayout>
You are correct in that CardView is not an available View type for Android Widgets, but this should create the functionality you were looking for 2 years ago
I've attached a screenshot. As per the screenshot I want to make the horizontal bar chart graph to my activity same as the snapshot. I've done half of it's contents using MP Chart Library. I can't add the x axis values starting from 3 to 9. Also not unable to add the vertical grids.
Please suggest me what I should do?
In advance thanks for your help and your little help will be appreciated.
Use GraphView
http://www.android-graphview.org/
I hope it would be helpful.
Create a xml file for a gradient in the /drawable folder and apply it to a LinearLayout as the background. The angle has to be 0.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:endColor="#ff3399"
android:angle="0"/>
</shape>
<LinearLayout
android:id="#+id/bar_1"
android:layout_width="wrap_content"
android:background="#drawable/my_gradient">
</LinearLayout>
Regarding the background: Try the GridLayout which enables to 'aggregate' cells. But this might be tricky.
I have custom EditTexts and I noticed their backgrounds draw wrong when they leave their parents. So I used android:clipChildren="false" on their parent. That works fine. They draw correctly when partially out of their parent now.
This gave me a new problem though. On older devices (< Android 2.3? Not confirmed what the max version is for this issue), the background doesn't get clipped to it's padding. The EditText backgrounds are now drawing to the full height/width of the screen. This only happens on the initial layout.
Has anyone experienced this before? It's really weird. I don't get why the background only draws wrong when using android:clipChildren="false" and only on some devices. I need that though since my EditTexts can be dragged around and need to keep drawing outside their parent container.
I just ran across the same problem. It was caused by having ColorDrawables as background (a StateListDrawable (<selector>) containing several #color/... items, to be exact).
It looks like this was fixed in Android 3.2.4-r1 (commit 95930e1).
Before that, the class comment used to say:
Note that a ColorDrawable [...] ignores the Bounds, meaning it will draw everywhere in the current clip even if setBounds(...) was called with a smaller area.
This was removed, and the draw(Canvas) method changed to respect the bounds.
As a workaround, if you need to support older Android versions, you can use a ShapeDrawable with a solid color to get the same behaviour:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/your_background_color"/>
</shape>
We had a 2.3.4 device where this issue was causing an ImageView to cover everything above it in the LinearLayout it was contained in.
The fix was to create the ShapeDrawable mentioned above and use that as the background to the image.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/your_background_color"/>
</shape>
<ImageView
android:id="#+id/this_is"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/a_rectangle_shape_drawable"
android:contentDescription="#string/an_image_view"
android:scaleType="centerInside"
android:src="#drawable/an_icon" />
I'm defining a ScaleDrawable in XML according to the Android developers site example:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/sign_ui_text_line"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
I define this as the background of an EditText
for some reason the drawable isn't displayed (but the EditText view changes it's size according to the definitions).
why doesn't it display ?
how can I scale a drawable to be used as a background ?
ScaleDrawables seem to require some workarounds in order to work properly, from the sounds of things.
See Zeh's answer to a similar question.