Android UI Layout - android

I'm trying to achieve the layout in the screenshot, the portion in red.
I think a table layout would be appropriate but I don't mind any really, I just need to be able to achieve that pretty connecting dotted lines with a table like display.
I have tried using a view with a dotted background but it does not feel right (Android Drawing Separator/Divider Line in Layout?).

I can see two options available to you:
You could try this answer (How do I make a dotted/dashed line in Android?)
Create a custom View overriding the method View#onDraw(Canvas) - where you can draw whatever you want.
Taken from referenced answer:
Without java code:
drawable/dotted.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:color="#C7B299"
android:dashWidth="10px"
android:dashGap="10px"
android:width="1dp"/>
</shape>
view.xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/dotted" />

Related

How to draw outline stroke on text in TextView? (Kotlin)

I need to draw outline stroke on text in MaterialTextView. I saw that it is not that easy to do in older questions.
(In this for example: How do you draw text with a border on a MapView in Android?).
Is something changed right now? If we need to set it programmatically, can anyone help me with Kotlin method for this and explain how to make it? I saw answers but I don't know where to create attrs.xml file I never used it before.
You could define a rectangle shape in the res/drawable folder, let's call it rect_border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp"
color="#000" />
</shape>
And use it as android:background for your MaterialTextView:
<MaterialTextView
...
android:background="#drawable/rect_with_border" />

How to draw a rectangle around multiple views

I’d like to achieve the following look in my app to try and group the views into a more logical layout.
I was thinking of creating a Shape Drawable for the rectangle something along the lines of this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="12px"/>
<stroke android:width="2dip" android:color="#000000"/>
</shape>
But what I’m not sure about how to place the rectangle in the view. Most of the examples I’ve seen are placing a drawable as a background for a single view, not multiple views as per the above.
Do I need to create some sort of ‘blank’ view which holds no purpose but to house the rectangle? How would I go about this?
Everything is currently defined in a constraint layout.
Create a layout around the views and set its background as the drawable you created.
You will have to add this as a background to the Viewgroup where all these views are.
For eg. add this drawable as background to the relative layout.
Thanks for the comments all. Thought I'd update this with what I implemented as although the other answers were correct, I still didn't quite grasp what was required, so here's some additional information:
Create a file called 'rectangle.xml' in the drawable folder, with the following content:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="12px"/>
</shape>
Then in the layout file in which I wanted to place the rectangle, I added the following view. The key bit that I didn't understand was how to specify the size/position of the rectangle. This was achieved by defining the following constraints to wrap it around the items I wanted.
<View
android:id="#+id/myRectangleView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#id/unit_spinner"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="#id/time_label"
android:background="#drawable/rectangle"
android:elevation="2dp"
/>
Couple of other points I struggled with:
Even though I defined this view before all the views it surrounds,
the arrows on the spinners were displaying behind the rectangle. To fix this, I had to use the elevation property on the view, and set all the other views to be higher.
I struggled to get the position of
the rectangle where I wanted it using the margin/padding of the
rectangle view, so instead had to add padding to the views it was
surrounding. Not sure if this is the right approach - I'm still
toying with it.

Inheritance in Android Shapes

So, I need to create three different buttons which look almost the same: all of them have rounded corners but different background colors. What I did is to have three different shapes created and give them a particular color. Although this approach gets the job done, I think there's gotta be a better and less cumbersome way to do this.
I've found a workaround to this by changing the background dynamically, but I was wondering whether there's an easier way to have this done without having to write any Java code.
This is the code for all three of the drawables:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<padding
android:bottom="15dp"
android:top="15dp"
android:left="15dp"
android:right="15dp" />
<solid
android:color="#color/green" />
</shape>
Is there any way to use "some kind of inheritance" to extend the code above to have three different "children" of this shape by only adding / overriding some of the parent's properties?
Thanks in advance!

Add Frame or Border to ImageView and Drop-Shadow

What I'm trying to do will work better with an example image. As you can see below I have a grey background, ontop of that sits a container with some padding containing an image. The container also has a slight dropshadow to it.
What I want to know, is if there's so non-painstaking way of doing this in my layout.xml? In a normal HTML document this would've been easy. But since this is for a mobile app and for a number of screen resolutions and so on, it's proving a bit difficult.
Any advice?
Edit: I eventually settled using a 9patch image. Everyting went really smooth in the creation of it but when I actually use it in my app I see these dark stripes on the right and bottom of the image. The dropshadow seems to work, it's a very light dropshadow.. but those darn stripes??
You can provide a border to a view by writing an xml file (say editBorder.xml) in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="5dp"
android:color="#EBDDE2" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="4dp" />
<gradient
android:centerColor="#color/white"
android:endColor="#color/white"
android:startColor="#color/white" />
<corners android:radius="8dp" />
</shape>
and to provide this border, use statement in ImageView as android:background="#drawable/editBorder"
This should solve your problem. :)
This can be done with proper padding and 9 patch image. See this link, may be it can help you.
ImageView having two property android:background and android:src
http://developer.android.com/reference/android/widget/ImageView.html
Create a blank white frame with drop shadow(Photoshop recommended).
So just do this
<ImageView android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
android:background="#drawable/whiteFrame"
android:padding="10dp" >
</ImageView>
I found this imageView . It's may good for you

Android Draw on top of an EditText, Spinner or Button (Draw on Screen)

what I want to achieve is this:
As you can see there is are two tiny, yellow triangles (warning signs) in the EdiTtexts. I read, it's possible to achieve that by using Relative layout and simply have the images overlap the EditText. This however is not an option as I am writing a library project and can't limit the user to one particular layout type (such as RelativeLayout). Alternatively I read, it can be done in the onDraw method of a view. However, I will have no access to the onDraw method (or rather now way of overriding it). The one thing I have access to, is the Views itslef (like Spinner, EditText) or alternatively their wrapping view (Like LinearLayout etc...). I need to set this tiny icon not only on EditText but generally on Views such as Spinner, Button, EditText, etc....
One idea was to simply obtain the x/y coordinates of the View on which I want to draw that warning sign, but although I am able to get the coordinates I am quite at a loss as to how to actually do the drawing on the obtained view.
On an additional note: I am using code to generate the layout from non-android layout-xml files. That only matters in so far, that I am afraid android-layout-xml-based solutions are not an option. Furthermore I have to be able to dynamically add/remove those image overlays.
Please let me know, if there are further questions.
Thank you very much in advance & Best regards,
Ready4Android
EDIT
I had one idea but I am not sure how to carry it out: I could determine the on-screen coordinates of my Widgets (like Spinner, EditText, Button...) and then I could draw that little warning symbol on top of my canvas - without even touching the Widgets. So would it be easier to solve, if I just wanted to draw on the screen? It should be possible, non?
EDIT 2 (20.09)
I followed the advice from superM but since the Icon was stretched I tried to use a bitmap inside the item. Thing is: It places the icon at the correct place (left edge) but the skin of the button disappears - I guess because it's replaced by the bitmap...
Here is the original Button:
Here the Xml (the invisible shape is the same as in superM's post):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sign_warning_overlay_shape"
android:top="5dip" android:right="5dip" android:bottom="5dip"
android:left="5dip" />
<item>
<bitmap android:src="#drawable/sign_warning" android:gravity="left" />
</item>
</layer-list>
and this is how it looks:
With the original posted layer list by superM - this one:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sign_warning_overlay_shape"
android:top="5dip" android:right="5dip" android:bottom="5dip"
android:left="5dip" />
<item android:drawable="#drawable/sign_warning" android:top="5dip"
android:right="5dip" android:bottom="5dip" android:left="5dip" />
</layer-list>
the Button looks like this(seems like here too the icon replaces the button skin):
Could it be, that I am setting the layer list in a wrong way? What would be the correct code to set the layer list on a button? I am using this one:
button.setBackgroundResource(R.drawable.sign_warning_overlay_layer_list);
Edit 3 - Modified solution
This edit is based on the accepted answer - I decided to go for a bitmap embedded in an item because I had issues with the image being stretched - here is the layer_list:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/btn_default"
android:top="5dip" android:right="5dip" android:bottom="5dip"
android:left="5dip" />
<item>
<bitmap android:src="#drawable/sign_warning" android:gravity="left" />
</item>
</layer-list>
Which can be set on the button like this:
button.setBackgroundDrawable(button.getResources().getDrawable( R.drawable.layer_list));
and produces the following result:
That's it folks :) !
Thanks for all the suggestions & Cheers,
Ready4Android
In the case you mentioned, I think that what you want can be easily done by setting android:drawableLeft.
For editText it is editText.setCompoundDrawables(left, top, right, bottom) where left, top, right and bottom are either drawables or null. For buttons you can set the image as background by using a layer-list and shape, where the first layer is transparent and should "wrap" your button and the second layer is the image. This is can be done with other views too.
Here's an example of layer-list with transparent background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#android:drawable/btn_default" <!--any default background from android:drawable-->
android:top="5dip"
android:right="5dip"
android:bottom="5dip"
android:left="5dip" />
<item
android:drawable="#drawable/my_drawable"
android:top="0dip"
android:right="0dip"
android:bottom="0dip"
android:left="0dip" />
</layer-list>
where background_transparent is:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#android:color/transparent"/>
<padding
android:top="0dip"
android:left="3dip"
android:bottom="5dip"
android:right="3dip" />
<corners
android:radius="4dip" />
</shape>
Buttons:
Simply set the text
using code:
button.setText("<img src=\"image_location\"/>Login");
using xml:
android:text="#string/mystring"
//mystring must be a string in strings.xml
//with a value of <img src="image_location"/>Login
EditTexts:
Your question is not clear 100%...
If you want the image to be behind the text, then the best way to achieve this is by setting a background with that image to the left and let it be a 9patch image that extends the empty space.
If you want the image to move to the right with the text entered, then you have a couple of ways to achieve it, one simple would be to use a listener to the text input and always make sure there is an at the end of the text. I have not tested this approach but I guess it works fine.
If you really need to be able to place arbitrary views over anything, you can take a look at PopupWindow and use View.getLocationInWindow to get co-ordinates for the views you want to overlay. You can take a look at an android standard usage for putting a little error marker floating over a textview in TextView.setError

Categories

Resources