Skip to main content

Drawables in android - Part I

Have you ever gushed over beautiful , very beautiful UI of your android apps? Sorry, most of the times these are iphone apps. That does not mean you can try to create beautiful UIs in android app.

You can create a gradient background to your button of layout using xml easily. But today let us consider how it is done using code.




int colors2[] = new int[]{Color.CYAN, Color.WHITE,Color.CYAN};

GradientDrawable grDr =new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors2);

grDr.setCornerRadius(5);

grDr.setGradientType(GradientDrawable,LINEAR_GRADIENT);

grDr.setStroke(1,Color.WHITE);
btn.setBackgroundDrawable(grDr);





This is how the button would look.

The code is almost self explanatory. The constructor of GradientDrawable takes the orientation of gradient and array of colors as parameter. Orientation can be top to bottom, bottom to top, left to right etc, or TL_BR or TR_BL (top left to bottom right - diagonal and top right to bottom left). Unlike xml, the colors can be more than 3. Stroke is the line surrounding the gradient with first parameter as thickness of the line.

If you are more creative, you need to restrict yourself with java colors, you can even use html color codes.

e.g.

int

colors[] = new int[]{Color.parseColor("#451d42"),Color.parseColor("#efa5bc") ,


Color.parseColor("#b67caf"),Color.parseColor("#1e3e65"),Color.parseColor("#3224af")};


The parseColor function will convert html color code to java int color. Its string parameter is the code in the form #RRGGBB. If you provide improper code, it throws invalid argument exception at runtime.

Instead of linear gradient, you can have radial gradient or sweep gradient. The shape of gradient can be rectangle, line, oval or ring.

To use radial or radial gradient, you have to mention center of gradient and the radius. The center 1,1 is taken as lower right corner, 0,0 as upper left corner of your widget. Look at this radial gradient

 And the code for this radial gradient shown in the begining of post is

 int colors[]= new int[]{Color.parseColor("#cff2f5"), Color.parseColor("#23c5ca"),Color.parseColor("#21b1b9")};
 GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
grDr.setGradientType(GradientDrawable.RADIAL_GRADIENT);
grDr.setGradientCenter(0,0.5f);
grDr.setGradientRadius(600);

By setting gradient type to sweeping, (and you need not set radius or center), you can create even more colorful gradients as shown below

int colors2[] = new int[]{Color.BLUE,Color.YELLOW,Color.RED,Color.GREEN,Color.MAGENTA,Color.WHITE};
 GradientDrawable grDr = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);  
grDr.setGradientType(GradientDrawable.RADIAL_GRADIENT);

In the next post, we will see how to use layer drawable. 

Comments

Popular posts from this blog

Drawables in Android - Layer drawable

Let us see how to use layer drawable. You can have two or more bitmaps on different layers to create such a drawable Using xml : You should use layer-list in your xml file to create layerdrawable. Here is layer.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/whiteicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/blueicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/redicon" android:gravity="top|left"/> </item> </layer-list> We are using three different bitmaps whiteicon.png, redicon.png and blueicon.png which are present in /res/drawable/mdpi folder. All these are of different sizes and aligned to top left. Thi

Copy to clipboard

In my upcoming app, I have codes which I display. These are some times lengthy, and I want the app to be able to copy this to clipboard. Once it is in clipboard, users can paste it anywhere. So how do you copy some text from your app to clipboard. You need to use clipboard manager. Clipboard Manager This class sets and gets data for the clipboard using Clipdata objects.  You can get the object of this class using system service.  - using statement context.getSystemService(Context.CLIPBOARD_SERVICE) Example I have a dummy project with a button, onclick of which copies content to clipboard. Here is my activity file package com . hegdeapps . testapp ; import android.content.ClipData ; import android.content.ClipboardManager ; import android.support.v7.app.AppCompatActivity ; import android.os.Bundle ; import android.view.View ; import android.widget.Button ; import android.widget.TextView ; public class MainActivity extends AppCompa

Simple ListView Adapter and list item select

When you are using a listview in your applications many a times you will write your own adapter to display item. But if your list is very simple showing a list of strings, you can use inbuilt adapters like ArrayAdapter, SimpleCursorAdapter etc. ArrayAdapter Let us look at an example <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent" android:layout_margin="20dp"> </ListView> </LinearLayout> And add these lines to the onCreate method of the activity. super.onCreate(savedInstanc