layout/ui/design

Advanced 9Patch drawing in android

SDK Version: 
M3

Drawing 9patch can turn into a nightmare easily. I would like to present the other side. The most common problem, to create draws wich are look well in portrait mode and landscape mode too.

Here are some 9patch technics:

(Note: This article don’t tell the basics of 9patching. If you are new in this technics, examine this page first: http://developer.android.com/guide/developing/tools/draw9patch.html)

(From this link, you can download the files: download the 9patch files)

Example One: Simple button
This is a just simple button, with a glowing effect. The 9patching wich I use here, keeps the button’s edges, so it keeps the rounded rectangle’s radius too. With 9patch, the content area of the button can be set easily.

draw9

How to use ViewFlipper

SDK Version: 
M3

You have probably already heard about ViewFlipper. If not, go to website: http://developer.android.com/reference/android/widget/ViewFlipper.html
and check it.

"Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval."

Sounds good. Let's try it!

Create a ViewFlipper in your main xml layout like this:

  1. <ViewFlipper android:id="@+id/flipper"
  2.                 android:layout_width="fill_parent" android:layout_height="fill_parent"
  3.                 android:layout_below="@+id/CockpitLayout">
  4.                 <include android:id="@+id/firstlayout" layout="@layout/first" />
  5.                 <include android:id="@+id/listlayout" layout="@layout/list" />
  6.                 <include android:id="@+id/thirdlayout" layout="@layout/productlayout&quot; />
  7.                 <include android:id="@+id/fourthlayout" layout="@layout/vendorlayout&quot; />
  8.  
  9.         </ViewFlipper>

Gradient dividers

SDK Version: 
M3

Today I’m going to show you an easy way to create gradient backgrounds in your applications.

Create an xml file in drawable directory and modify like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:shape="rectangle">
  4.         <gradient
  5.          android:startColor="#FF0000"
  6.          android:endColor="#00FF00"
  7.          android:angle="90" />
  8. </shape>

This code snippet generate a divider with gradients that fade from green to red.

After that you can use that such a drawable for layout background.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:background="@drawable/gradientcolors">
  7.  
  8. </LinearLayout>

How to add divider items to ListView

SDK Version: 
M3

Today I'm going to show an easy way to add divider items to your ListView.
First off, you need your own ListView Adapter which extends SimpleAdapter. If you don't know how to create a new class for your own SimpleAdapter, please go and visit: http://developer.android.com/resources/samples/ApiDemos/src/com/example/...

Ok, so here's how it looks like:

  1. public class SpecialAdapter extends SimpleAdapter {
  2.         private LayoutInflater mInflater;
  3.         private List<HashMap<String, String>> items;
  4.  
  5.         public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
  6.                 super(context, items, resource, from, to);
  7.                 // Cache the LayoutInflate to avoid asking for a new one each time.
  8.         this.items = items;
  9.         }
  10.  
  11.          @Override
  12.      public boolean areAllItemsEnabled() {
  13.          return false;
  14.      }
  15.  
  16.      @Override
  17.      public boolean isEnabled(int position) {
  18.          boolean enabled = false;

How to create a custom titlebar

SDK Version: 
M3

If you got sick and tired of the default style/behavior of the title bar in your apps, or just need something different, than here is a little snippet for You.

Wallpaper tutorial - Part 2

SDK Version: 
M3

This article was requested by some of our community members. It is based on the Android Gallery, ImageView Example tutorial by Sasikumar (Part 1 is here). I extended his example with a new function. If you long click on the big image, an alert dialog show up and ask “Do you want to use this image as a wallpaper?". If you click yes, the actually image will be your new wallpaper.

I only changed the Activity class:

  1. package org.androidpeople.gallery;
  2.  
  3. import java.io.IOException;
  4.  
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.res.TypedArray;
  10. import android.graphics.Bitmap;

How to update custom listview images simply

SDK Version: 
M3
So in this tutorial I'm going to show you how to refresh imageviews' contents periodically (let say by Handlers if you download the picture from web).
I had painful 2 days figuring out what's a good solution here, I tried to give IDs to imageviews and that sort of sick things, but believe me, it wasn't worth it. The solution is so simply that I hardly can believe.
What we're lookign for here is instead of create new Adapters and HashMaps (which contains ListView data), we just update it's values, and Android will do the trick for us.
The most important thing is DO NOT AT ANY CIRCUMSTANCES CREATE A NEW ADAPTER (or a new data source that holds the Adapter's data).
There's an exception of course, you obviously have to create a new Adapter in OnCreate() { }.

Let me show you how it works:

How to set an image as wallpaper on different api levels

SDK Version: 
M3

Pretty easy! :)
Use the context’s setWallpaper method.

  1. getApplicationContext().setWallpaper(bitmap)

Don’t forget to add the SET_WALLPAPER permission to your AndroidManifest.xml!

  1. <uses-permission android:name="android.permission.SET_WALLPAPER" />

Differences between api versions
On Android 1.5 the wallpaper looks cool, doesn’t it? :) The backgroud fills the screen.

But on 1.6 and higher android strechs the wallpaper like this:

It’s not bad if you use high resolution wallpaper, but the low res wallpapers look crappy.

How to customize Toasts

SDK Version: 
M3

Customizing Toasts

A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.

The Basics

You can create customized Toasts and setting its display position, as you like.
Here's a little code snippet to demonstrate how it works:

  1.        String text = “example toast text!;
  2.        Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
  3.        /* Positioning your Toast */
  4.        int offsetX = 0, offsetY = 0;
  5.        toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);
  6.        toast.show();

Creating a Custom Toast View

How to display a custom dialog in your Android application

SDK Version: 
M3

How to display a custom dialog in your Android application

Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...

Syndicate content