Skip to content

Sample SUGAR CMR

Sugar CRM

OS->GPS: Convert Ordnance Survey Codes to GPS Android

OS->GPS converts any UK OS Code to its respective GPS coordinates allowing you to navigate to your destination using your GPS device. A Google Map shows the location of the crag. OS-GPS also provides directions to the crag using Google Maps and Goole Maps Navigation.


Available in Android Market

Android SlidingDrawer with Custom View

I was recently interested in using the SlidingDrawer widget in my Android application rather than having to start a new Activity. Having searched I found very little concrete and simple step by step examples of how to use the SlidingDrawer. Below is a simple example of using the SlidingDrawer widget and applying a custom view as the content of the SlidingDrawer. The code below is adapted from the SlidingDrawer example by commonsguy found here

Note you will need to get the drawer tray handle image from the above link

 

Download the full source

The SlidingDrawer Activity
public class SlidingDrawerActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.main);
			LinearLayout lin = (LinearLayout) findViewById(R.id.linear);
			lin.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {

					return true;
				}
			});

			SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.drawer);
			slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

				@Override
				public void onDrawerOpened() {

				}
			});

			slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

				@Override
				public void onDrawerClosed() {

				}
			});

			// Get the content of the SlidingDrawer i.e Our CustomView
			CustomView customView = (CustomView) findViewById(R.id.content);
			customView.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {

					return true;
				}
			});

		} catch (Exception e) {

		}
	}
}

The CustomView Class

	public class CustomView extends View{
		public CustomView(Context context,AttributeSet attrs){
			super(context,attrs);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			try {
				Paint paint = new Paint();
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(Color.BLACK);
				canvas.drawPaint(paint);
				paint.setColor(Color.WHITE);
				canvas.drawCircle(100,100,30,paint);
			} catch (Exception e) {

			}
		}
	}

The Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">

<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:handle="@+id/handle"
android:content="@+id/content">

<bear.exmaple.CustomView
android:id="@+id/content" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</bear.exmaple.CustomView>

<ImageView android:id="@id/handle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/tray_handle_normal" />

</SlidingDrawer>
</LinearLayout>

package bear.exmaple;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

public class SlidingDrawerActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.main);
			// get the root linear layout
			LinearLayout lin = (LinearLayout) findViewById(R.id.linear);
			lin.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {

					return true;
				}
			});

			// get the Sliding Drawer
			SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.drawer);
			slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

				@Override
				public void onDrawerOpened() {

				}
			});

			slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

				@Override
				public void onDrawerClosed() {

				}
			});

			// Get the content of the SlidingDrawer i.e Our CustomView
			CustomView customView = (CustomView) findViewById(R.id.content);
			customView.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {

					return true;
				}
			});

		} catch (Exception e) {
		}

	}
}