August 2014

Thursday, August 14, 2014

Creating SimpleAdapter for ListView in Android.....


This Article shows to create SimpleAdapter for ListView in Android.

1- Create row xml for ListView(row.xml).

<?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="wrap_content"
     android:orientation="vertical">
     <TextView android:id="@+id/text1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView android:id="@+id/text2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
     <TextView android:id="@+id/text3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
</LinearLayout>


2- Create ListActivity(MainActivity.java).

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Abc extends ListActivity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> list =
                  new ArrayList<HashMap<String, String>>();

String[] from = new String[] {"Text1", "Text2", "Text3"};

int[] to = new int[] {R.id.text1, R.id.text2, R.id.text3};
for(int i=0; i<3; i++) {
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
map.put(from[i], "Text" + i);
list.add(map);
}
setListAdapter(new SimpleAdapter(this, list, R.layout.row, from, to));
}
}

Thursday, August 7, 2014

Creating ListActivity & BaseAdapter in Android.....


This article shows to create ListActivity & BaseAdapter in Android.

1- Create BaseAdapter for ListActivity(MyAdapter.java).

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
private Activity activity;
private String[] str;
public MyAdapter(Activity c, String[] str)
{
this.activity = c;
this.str = str;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return (str.length);
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return str[arg0];
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
TextView txtView;
if(arg1 == null)
{
        txtView= new TextView(activity);
txtView.setPadding(15,15,15,15);
}
else
txtView= (TextView) arg1;
txtView.setText(str[arg0].toString());
txtView.setTextSize(20);
return txtView;
}
}

2- Create ListActivity(MainActivity.java).

import android.app.ListActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new MyAdapter(this, new String[]{"Android", "PHP", "ASP.NET", "iPhone"}));
}
}

Wednesday, August 6, 2014

Creating animated dialog in android…..


This article shows you to create animated dialog in android.

1- Create anim folder inside resoures(res).

2- Create xml for slide in dialog(slide_in.xml) in anim.

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

3- Create xml for slide out dialog(slide_out.xml) in anim.


<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

4- Create style for animation in styles.xml.

<style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_in</item>
        <item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

<style name="AnimatedDialogTheme" parent="@android:style/Theme.Panel">
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>

5- In Java file initialize dialog as-

//Use try catch to avoid Exception in lower api devices
try {
dialog = new Dialog(this, R.style.AnimatedDialogTheme);
} catch(Exception ex) {
dialog = new Dialog(this);
}

Tuesday, August 5, 2014

Creating custom toast using xml.....


This article shows you to create custom toast in android.

1- Create xml for Toast(toast.xml).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="8dp"
              android:background="@drawable/image"
              >    
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=""
              android:textColor="#000"
              android:textSize="20sp"
              />   
</LinearLayout>

2- Create Activity Class(MainActivity.java).

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pressMe = (Button) findViewById(R.id.button1);
pressMe.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showCustomToast("Hey! I'm Toast.");
}
});
}

protected void showCustomToast(String msg) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup)                                         findViewById(R.id.toast_layout_root));
textview = (TextView) layout.findViewById(R.id.text);
textview.setText("" + text);
Toast toast = new Toast(this);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);/* Replace (0,0) to corresponding (x,y) to set position of toast.*/
toast.setView(layout);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
}

Creating custom dialog using xml.....


This tutorial show you how to create a custom dialog in Android.
          1-  Create activity & custom dialog layout xml file.

activity_main.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press me!" />

</LinearLayout>


custom_dialog.xml


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


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hey! I'm Dialog."
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press Me!" />


</LinearLayout>


2-  Create Activity Class.

MainActivity.java

package com.example.customdialog;

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


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pressMe = (Button) findViewById(R.id.button1);
pressMe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showCustomDialog();
}
});
}

protected void showCustomDialog() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this);
dialog.setTitle("Custom Dialog");
dialog.setContentView(R.layout.custom_dialog);
Button button = (Button) dialog.findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}

}