Wednesday, 28 March 2012

ListView Animation in Android

Here below code is use full to create List View with Animation.

Android Activity Class(ListviewAnimation.java)

package com.androidsurya.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListviewAnimation extends Activity {

private ListView mainListView;
private DisplayMetrics metrics;
MyOwnAdapater moAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mainListView = new ListView(this);
mainListView.setFadingEdgeLength(0);
String[] atoz = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i < atoz.length; i++) {
strings.add(atoz[i]);
}
moAdapter = new MyOwnAdapater(this, strings, metrics);
mainListView.setAdapter(moAdapter);
setContentView(mainListView);
}

public class MyOwnAdapater extends ArrayAdapter<String> {
private Context context;
private LayoutInflater mInflater;
private ArrayList<String> strings;
private DisplayMetrics metrics_;

private class Holder {
public TextView textview;
}

public MyOwnAdapater(Context context, ArrayList<String> strings,
DisplayMetrics metrics) {
super(context, 0, strings);
this.context = context;
this.mInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.strings = strings;
this.metrics_ = metrics;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final String str = this.strings.get(position);
final Holder holder;

if (convertView == null) {
convertView = mInflater.inflate(
android.R.layout.simple_list_item_1, null);
convertView.setBackgroundColor(0xFF202020);

holder = new Holder();
holder.textview = (TextView) convertView
.findViewById(android.R.id.text1);
holder.textview.setTextColor(0xFFFFFFFF);

convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}

holder.textview.setText(str);
Animation animation = null;
animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0,
0);
animation.setDuration(750);
convertView.startAnimation(animation);
animation = null;
return convertView;
}

}
}

Register Activity in Android Manifest File

 <activity
            android:name="com.androidsurya.listview.ListviewAnimation"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Output Screenshot:









































Monday, 26 March 2012

How to start activity from service in android

Here the below code is used to start activity from the service in android

Intent newIntent = new Intent(getBaseContext( ),nextActivity.class);
newIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(newIntent);









Tuesday, 20 March 2012

How to downgrade Android SDK

If you want to downgrade Android SDK Tools to a previous version,

it's possible following these steps:

Find your Android SDK folder

Locate the "tools" subfolder and rename it to "toolsbak" (just to keep a backup copy of the original tools folder)

Download from google repository the SDK Tool version

you want to downgrade to (in this case it was: http://dl-ssl.google.com/android/repository/tools_r10-windows.zip) and unzip it.

The ZIP file you downloaded contains a tools folder that has to be moved to your Android SDK folder.

Sunday, 18 March 2012

Android Custom Listview Example

Listview is nothing but a A view that shows items in a vertically scrolling list.
Here we will see how to show android custom listview(with image and text)

UI layout(activity_main.xml)

activity_main.xml file contains listview object which is used to display listview

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Select your Favorite Social Network"
        android:textSize="15sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/banner" >
    </ListView>

</RelativeLayout>

(listview_row.xml)

Here (listview_row.xml) layout for ListView rows in a XML file and keep it in res/layout folder.
provides data to each row in ListView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/icon_id"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

    <TextView
        android:id="@+id/title_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon_id"
        android:paddingBottom="10dp"
        android:text="Title"
        android:textColor="#3399FF"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/description_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_id"
        android:layout_toRightOf="@+id/icon_id"
        android:paddingLeft="10dp"
        android:text="Description"
        android:textColor="#CC0033"
        android:textSize="12dp" />

</RelativeLayout>

Row.java

This class is usefull to store the data for each row in ListView.

package com.androidsurya.androidcustomlistview;

public class Row {
private int imageId;
private String title;
private String desc;

public Row(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}

public int getImageId() {
return imageId;
}

public void setImageId(int imageId) {
this.imageId = imageId;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return title + "\n" + desc;
}
}

Android Activity class(CustomListView.java)

package com.androidsurya.androidcustomlistview;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListView extends Activity implements OnItemClickListener {
public static final String[] titles = new String[] { "Facebook",
"Google +", "Orkut", "Twitter" };

public static final String[] descriptions = new String[] {
"Facebook is an online social networking service",
"Google + is an online social networking service",
"Orkut is an online social networking service",
"Twitter is an online social networking service" };

public static final Integer[] images = { R.drawable.facebook,
R.drawable.googleplus, R.drawable.orkut, R.drawable.twitter };

ListView listView;
List<Row> rowItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<Row>();
for (int i = 0; i < titles.length; i++) {
Row item = new Row(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}

listView = (ListView) findViewById(R.id.listView);
CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(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.activity_main, menu);
return true;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "Item Selected.."
+ (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}

}

CustomBaseAdapter.java

CustomBaseAdapter is a Class which implements BaseAdapater 
BaseAdapater is job is display data to each row in ListView.

package com.androidsurya.androidcustomlistview;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<Row> rowItems;

public CustomBaseAdapter(Context context, List<Row> items) {
this.context = context;
this.rowItems = items;
}

/* private view holder class */
private class ViewHolder {
ImageView iconView;
TextView txtTitle;
TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView
.findViewById(R.id.description_id);
holder.txtTitle = (TextView) convertView
.findViewById(R.id.title_id);
holder.iconView = (ImageView) convertView
.findViewById(R.id.icon_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Row rowItem = (Row) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.iconView.setImageResource(rowItem.getImageId());

return convertView;
}

@Override
public int getCount() {
return rowItems.size();
}

@Override
public Object getItem(int position) {
return rowItems.get(position);
}

@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}

Register Android Activity in AndroidManifest.xml file

 <activity
            android:name="com.androidsurya.androidcustomlistview.CustomListView"
            android:label="@string/app_name" >

Output Screenshot





















Thursday, 15 March 2012

Update Progress Bar using Buttons OR Spinner


(UI Layout)showdata.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" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp" />
    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:max="100"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:progress="0"/>
    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignParentLeft="true"
        android:text="-" />
    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignParentRight="true"
        android:text="+" />
</RelativeLayout>


Activity( ShowSpinner .java)

package com.surya.spinner;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import android.app.Activity;

public class ShowSpinner extends Activity implements OnClickListener {
ProgressBar bar;
Button plus, minus;
Spinner spinner;
int i;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdata);
plus = (Button) findViewById(R.id.button2);
plus.setOnClickListener(this);
minus = (Button) findViewById(R.id.button1);
minus.setOnClickListener(this);
bar = (ProgressBar) findViewById(R.id.progress);
String aList[] = getResources().getStringArray(R.array.values);
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, aList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Object item = parent.getItemAtPosition(pos);
Toast.makeText(getApplicationContext(), "selcted" + item,
Toast.LENGTH_SHORT).show();
bar.setProgress(Integer.parseInt(item.toString()));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

i = bar.getProgress();
switch (v.getId()) {
case R.id.button1:
if (i != 0)
i = i - 10;
setValues();
break;
case R.id.button2:
if (i != 100)
i = i + 10;
setValues();
break;

}
}

void setValues() {
for (int j = 0; j <= 10; j++) {
if (i == (j * 10)) {
spinner.setSelection(j);
bar.setProgress(i);
}
}
}

}

Strings.Xml
<resources>
<string-array name="values">
        <item>0</item>
        <item>10</item>
        <item>20</item>
        <item>30</item>
        <item>40</item>
        <item>50</item>
        <item>60</item>
        <item>70</item>
        <item>80</item>
        <item>90</item>
        <item>100</item>
    </string-array>
</resources>

Output Screenshot:



Please Add valuable Comments on this.........








Wednesday, 14 March 2012

Android Customized Expandable ListView Example


A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view. 

Expandable lists are able to show an indicator beside each item to display the item's current state (the states are usually one of expanded group, collapsed group, child, or last child).
Use setChildIndicator(Drawable) or setGroupIndicator(Drawable) (or the corresponding XML attributes) to set these indicators (see the docs for each method to see additional state that each Drawable can have). The default style for an ExpandableListView provides indicators which will be shown next to Views given to the ExpandableListView. The layouts android.R.layout.simple_expandable_list_item_1 and android.R.layout.simple_expandable_list_item_2 (which should be used with SimpleCursorTreeAdapter) contain the preferred position information for indicators.

The context menu information set by an ExpandableListView will be a ExpandableListView.ExpandableListContextMenuInfo object with packedPosition being a packed position that can be used with getPackedPositionType(long) and the other similar methods.

The above source is from : Android Developers Site

Here the below example show List of States with Districts

Here States are act as Group Parent and Districts are act as Child

Output Screenshot:

















































Android UI Layout(main.xml)

This is main UI layout which is visible in main Activity

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Customized Expandable ListView with states and districts" />

    <ExpandableListView
        android:id="@+id/ExpandableListViewid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>

Android UI Layout(expandablelv_groupview.xml)

This UI layout to show list of Group parent(States in this example)

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

    <TextView
        android:id="@+id/groupTXT"
        android:layout_width="wrap_content"
        android:layout_height="60px"
        android:layout_marginLeft="50px"
        android:gravity="center_vertical" >
    </TextView>

</LinearLayout>

Android UI Layout(expandablelv_childview.xml)

This UI layout to show list of childern(Distcts in this example)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <TextView
        android:id="@+id/childTXT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30px"
        android:textColor="@android:color/white" >
    </TextView>

</LinearLayout>

Java class(ReadData.java)

This class contains two methods one is for get group data and another one is for child data

package com.androidsurya.extendablelv;

import java.util.ArrayList;

public class ReadData {
private static ArrayList<String> groupsList;
private static ArrayList<ArrayList<ArrayList<String>>> childsList;

static ArrayList<String> getGroupData() {
groupsList = new ArrayList<String>();
groupsList.add("Andhra Pradesh");
groupsList.add("Tamil Nadu");
groupsList.add("Karnataka");
return groupsList;
}

static ArrayList<ArrayList<ArrayList<String>>> getChildData() {

childsList = new ArrayList<ArrayList<ArrayList<String>>>();
childsList.add(new ArrayList<ArrayList<String>>());
childsList.get(0).add(new ArrayList<String>());
childsList.get(0).get(0).add("Nellore District");
childsList.get(0).add(new ArrayList<String>());
childsList.get(0).get(1).add("Srikakulam District");
childsList.get(0).add(new ArrayList<String>());
childsList.get(0).get(2).add("Visakhapatnam District");
childsList.get(0).add(new ArrayList<String>());
childsList.get(0).get(3).add("East Godavari District");

childsList.add(new ArrayList<ArrayList<String>>());
childsList.get(1).add(new ArrayList<String>());
childsList.get(1).get(0).add("Ariyalur District");
childsList.get(1).add(new ArrayList<String>());
childsList.get(1).get(1).add("Chennai District");
childsList.get(1).add(new ArrayList<String>());
childsList.get(1).get(2).add("Salem District");
childsList.get(1).add(new ArrayList<String>());
childsList.get(1).get(3).add("Madurai District");

childsList.add(new ArrayList<ArrayList<String>>());
childsList.get(2).add(new ArrayList<String>());
childsList.get(2).get(0).add("Bagalkot District");
childsList.get(2).add(new ArrayList<String>());
childsList.get(2).get(1).add("Bangalore Rural District");
childsList.get(2).add(new ArrayList<String>());
childsList.get(2).get(2).add("Belgaum District");
childsList.get(2).add(new ArrayList<String>());
childsList.get(2).get(3).add("Mysore District");
return childsList;
}

}

Android Activity (Expandable_ListView.java)

This  is an main activity in this example here we are created myExpandableAdapter to
add data to listview this myExpandableAdapter takes parameters of group and child data
and added to listview. and when user click on child element this here we can see one Toast
message which one is clicked by user from child list.


package com.androidsurya.extendablelv;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

public class Expandable_ListView extends Activity {

ExpandableListView expLView;
TextView childtxt;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expLView = (ExpandableListView) findViewById(R.id.ExpandableListViewid);
myExpandableAdapter adapter = new myExpandableAdapter(this,
ReadData.getGroupData(), ReadData.getChildData());
expLView.setAdapter(adapter);
}

public class myExpandableAdapter extends BaseExpandableListAdapter {

private ArrayList<String> groups;

private ArrayList<ArrayList<ArrayList<String>>> children;

private Context context;

public myExpandableAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<ArrayList<String>>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}

@Override
public boolean areAllItemsEnabled() {
return true;
}

@Override
public ArrayList<String> getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String child = (String) ((ArrayList<String>) getChild(
groupPosition, childPosition)).get(0);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.expandablelv_childview, null);
}

childtxt = (TextView) convertView.findViewById(R.id.childTXT);

childtxt.setText(child);
childtxt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Your clicked->" + child, Toast.LENGTH_SHORT)
.show();
}
});

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}

@Override
public String getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

String group = (String) getGroup(groupPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.expandablelv_groupview, null);
}

TextView grouptxt = (TextView) convertView
.findViewById(R.id.groupTXT);

grouptxt.setText(group);

return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}

}

}

Register Android Activity in Android Manifest file

  <activity
            android:name="com.androidsurya.extendablelv.Expandable_ListView"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>




Android Expandable listview Example

Click below Button to see Android Expandable listview




Tuesday, 13 March 2012

Reset All Values using Reset Button in Android

Main.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="User Name" />
    <EditText android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/username"/>
    <Button  android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/reset"
              android:text="Reset Button"/>

</LinearLayout>


Activity(ResetExampleActivity )
package com.surya.resetvalues;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ResetExampleActivity extends Activity {
     EditText uname;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        uname =(EditText)findViewById(R.id.username);
        Button reset=(Button)findViewById(R.id.reset);
        reset.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
       
                uname.setText(" ");
            }  
    });
   
}
    }

Friday, 2 March 2012

Simple Listview in Android

Listview is nothing but a A view that shows items in a vertically scrolling list.
The items come from the ListAdapter associated with this view.
Here we can see on example listview using setAdapter(ListAdapter adapter).
setAdapter(ListAdapter adapter) used to Sets the data behind this ListView.

Android Activity (SimpleListview.java)


package com.androidsurya.simplelistview;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleListview extends Activity {

    private ListView mainListView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_listview);

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.mainListView);

        // Create and populate a List of planet names.
        String[] states = new String[] { "Andhra Pradesh",
                "Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa",
                "Gujarat", "Haryana", "Himachal Pradesh" };
        mainListView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, states));


    }
  
}

UI Layout(activity_simple_listview.xml)

place this .xml file in layout folder

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mainListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

Register Activity in Android Manifest File


 <activity
            android:name="com.androidsurya.simplelistview.SimpleListview"
            android:label="@string/app_name" >

Ouput Screenshot:




Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...