Friday, December 27, 2013

Creating Custom Toggle button

1) create [drawablename].xml under /res/drawable
eg: toggle_bt.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- deselection -->
   <item android:state_checked="false" android:drawable="@drawable/delete_icon" />
   <!-- Selection -->
   <item android:state_checked="true" android:drawable="@drawable/add_icon" />
</selector>


2) create [drawablename].xml under /res/drawable for setting background toggle button.
eg: toggle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+android:id/background" android:drawable="@drawable/toggle_bt" />
   <item android:id="@+android:id/shoptoggle" android:drawable="@android:color/transparent" />
</layer-list>


3) added toggle button to layout and set background drawable.
eg:
<ToggleButton
            android:id="@+id/toggle_btn"
            android:background="@drawable/toggle_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn=""
            android:textOff=""
            android:text=""
            />

Creating List in Android


1) Create layout with List view and Textview for empty text. set id to @android:id/list & @android:id/empty

eg: activity_list_test.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".ListTestActivity" >
   
   <ListView
       android:headerDividersEnabled="true"        
       android:id="@android:id/list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       />

   <TextView
       android:id="@android:id/empty"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:gravity="center"
       android:text="@string/listempty" />
</RelativeLayout>



2) Create Activity extending ListActivity
package com.test.list;

import com.test.R;
import com.test.R.id;
import com.test.R.layout;
import com.test.R.menu;

import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.Context;

public class ListTestActivity extends ListActivity {
public String todos[] = new String[]{"todo1", "todo2", "todo3", "todo4", "todo5","todo6", "todo7", "todo8", "todo9", "todo10"};
ArrayAdapter<String> adapter;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set listview layout
setContentView(R.layout.activity_list_test);
lv = getListView();
//Adding header for the list
RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.list_item, null);
TextView v  = (TextView) layout.findViewById(R.id.listitem);
v.setText("Name");
lv.addHeaderView(layout);
//setting adapter for the list
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listitem, todos);
setListAdapter(adapter);
}
//Action called when list item is selected
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d("onListItemClick", "position "+ position +" id "+ id);
super.onListItemClick(l, v, position, id);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_test, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

}

Using SurfaceView

1) Create layout with surface view
eg:  activity_surface_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".SurfaceTest" >

   <TextView
       android:id="@+id/surfacetitle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:text="Surface Test" />
   
   <SurfaceView
       android:id="@+id/drawingsurface"
       android:layout_below="@+id/surfacetitle"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       />
</RelativeLayout>

2) Create Activity for using surfaceview
eg: SurfaceTest.java

package com.test.surface;

import com.test.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceTest extends Activity implements SurfaceHolder.Callback, Runnable
{
public static final int DIFF = 10;
int cx, cy;
int radius = 10;
Paint cpaint, bgpaint;
int h, w;
int xdiff =DIFF;
int ydiff = DIFF;
SurfaceView sView;
SurfaceHolder holder;
Rect surfaceRect;
boolean isExit;
Thread thisThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_test);
sView = (SurfaceView) findViewById(R.id.drawingsurface);
holder = sView.getHolder();
holder.addCallback(this);
thisThread = new Thread(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.surface_test, menu);
return true;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d("surfaceCreated", " on surfaceCreated holder "+ holder);
isExit = false;
//start the thread only if surface created
thisThread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surfaceDestroyed", " on surfaceDestroyed holder "+ holder);
isExit = true;
}

@Override
public void run()
{
Canvas canvas = null;
while(!isExit)
{
try
{
Thread.sleep(100);
// Get the canvas for drawing on surface
canvas = holder.lockCanvas();
// random moving circle eg
if(canvas != null)
{
if(w == 0)
{
h = canvas.getHeight();
w = canvas.getWidth();
cx = w/2;
cy = h/2;
surfaceRect = new Rect(0,0,w,h);
cpaint = new Paint();
cpaint.setColor(Color.RED);
bgpaint = new Paint();
bgpaint.setColor(Color.BLACK);
}
canvas.drawRect(surfaceRect, bgpaint);
canvas.drawCircle(cx, cy, radius, cpaint);
}
if((cx - radius) <= 0)
{
xdiff = DIFF;
}
else if((cx + radius) >= w)
{
xdiff = -DIFF;
}
if((cy - radius) <= 0)
{
ydiff = DIFF;
}
else if((cy + radius) >= h)
{
ydiff = -DIFF;
}
cx += xdiff;
cy += ydiff;
}
catch(Exception e)
{
Log.d("run method", e.toString());
}
finally
{
if(canvas != null)
{
// publish the drawing
holder.unlockCanvasAndPost(canvas);
}
}
}
}

}