Thursday, December 23, 2010

DialogTest

This example contains the different types of Dialogs

The code has been broken into three files

a) main.xml
b) dialogtest.xml
c) dialogActivity.java

a) main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise a Toast"
android:id="@+id/toast" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Alert"
android:id="@+id/alert" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Custom Dialog"
android:id="@+id/custom" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Raise an Progress Dialog"
android:id="@+id/progress" />
</LinearLayout>


b) dialogtest.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp"
android:src="@drawable/icon" />
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#FFF"
android:text="This is a custom dialog" />
</LinearLayout>

c) dialogActivity.java

package org.example.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class dialogActivity extends Activity implements OnClickListener{
Button toast, alert, progress, custom;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
}

public void initialize()
{
toast = (Button)findViewById(R.id.toast);
alert = (Button)findViewById(R.id.alert);
progress = (Button)findViewById(R.id.progress);
custom = (Button)findViewById(R.id.custom);
toast.setOnClickListener(this);
alert.setOnClickListener(this);
progress.setOnClickListener(this);
custom.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toast:
Toast.makeText(this, "Toast Raised", Toast.LENGTH_SHORT).show();
break;
case R.id.alert:
AlertDialog.Builder ar = new AlertDialog.Builder(this);
ar.setTitle("Alert Example");
ar.setPositiveButton("Ok", new DialogInterface.OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {
notify.this.finish();
}
});
ar.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
ar.create().show();
break;
case R.id.custom:
// Context mcontext = getApplicationContext();
Dialog d = new Dialog(this);
d.setContentView(R.layout.dialogtest);
d.setTitle("Custom Dialog");
d.show();
break;
case R.id.progress:
//ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
ProgressDialog dialog = new ProgressDialog(this);
dialog.show(this, "Progress", "Loading. Please wait...", false, true, new DialogInterface.OnCancelListener(){

@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
});
break;
default:
break;
}
}  
}


Screenshots :


Toast:



Alert Dialog:



Custom Dialog : 


Progress Dialog:


Friday, December 17, 2010

List View Test with Java Code

The code has been written in two files
a) main.xml
b) listActivity.java


a) main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="The Months are" />
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/list" />
</LinearLayout>

b) listActivity.java

package org.example.list;

import java.util.ArrayList;

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

public class listActivity extends Activity {
    /** Called when the activity is first created. */
ListView lv;
ArrayList<String> months;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeArray();
        lv = (ListView)findViewById(R.id.list);     
        ArrayAdapter<String> aa = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, months);
        lv.setAdapter(aa);
    }
    
    public void initializeArray()
    {
     months = new ArrayList<String>();
        months.add("January");
        months.add("February");
        months.add("March");
        months.add("April");
        months.add("May");
        months.add("June");
        months.add("July");
        months.add("August");
        months.add("September");
        months.add("October");
        months.add("November");
        months.add("December");  
    }
}

Screenshot :





RatingBar Test

The code has been written in two files
a) main.xml
b) ratingActivity.java

The code is as follows

a) main.xml

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


b) ratingActivity.java


package org.example.rating;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class ratingActivity extends Activity implements OnRatingBarChangeListener{
   
    RatingBar rb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rb = (RatingBar)findViewById(R.id.ratingbar);
        rb.setOnRatingBarChangeListener(this);
    }
    public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {
        int ratedStars = (int) rating;
        Toast.makeText(this, "Rating star count is "+ratedStars, Toast.LENGTH_SHORT).show();
    }  
}



Screenshot :





Toggle Button Test

The code has been written in two files
a) main.xml
b) toggleActivity.java

The code is as follows

a) main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ToggleButton 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textOn="Checked"
    android:textOff="Unchecked"
    android:id = "@+id/toggle"/>
    />
</LinearLayout>



b) toggleActivity


package org.example.toggle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class toggleActivity extends Activity implements OnClickListener{
   
    ToggleButton tb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tb = (ToggleButton)findViewById(R.id.toggle);
        tb.setOnClickListener(this);
    }
   
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(tb.isChecked())
        {
            Toast.makeText(this, "Toggle Button is checked", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, "Toggle Button is unchecked", Toast.LENGTH_LONG).show();
        }
    }
}



Screenshots :


1) Toggle On 




2) Toggle Off











Thursday, December 16, 2010

ScrollView Test

The code has been written in three files
a) main.xml
b) strings.xml
b) scrollActivity.java

The code is as follows

a) main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation = "vertical">
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/RED"/>
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/DARK_GREEN"/>
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/LIGHT_BLUE"/>
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/GREEN"/>   
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/BLUE"/>
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/LIGHT_RED"/>
    <TextView
    android:layout_width="fill_parent" android:layout_height="80px" android:background = "@color/NEW_COLOR"/>   
    </LinearLayout>   
</ScrollView>


b) strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, scrollActivity!</string>
    <string name="app_name">ScrollViewTest</string>
<color name="RED">#FF5555</color>
<color name="BLUE">#5555FF</color>
<color name="LIGHT_BLUE">#2222AA</color>
<color name="GREEN">#88FF22</color>
<color name="DARK_GREEN">#00FF00</color>
<color name="LIGHT_RED">#FFAAAA</color>
<color name="NEW_COLOR">#FFAAEE</color>
</resources>



c) scrollActivity.java



package org.example.scroll;

import android.app.Activity;
import android.os.Bundle;

public class scrollActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}




TableLayout Sample

The code has been written in two files
a) main.xml
b) tableActivity.java

The code is as follows

a) main.xml


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row1 Button1" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row1 Button2" />
    </TableRow>
    <TableRow>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row2 Button1" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row2 Button2" />
    </TableRow>
    <TableRow>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row3 Button1" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Table Row3 Button2" />
    </TableRow>
</TableLayout>



b) tableActivity.java
package org.example.table;

import android.app.Activity;
import android.os.Bundle;

public class tableActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}




Relative Layout Sample

The code has been written in two files
a) main.xml
b) relativeActivity.java

The code is as follows

a) main.xml


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Username"
    android:id = "@+id/user"
    android:paddingTop = "5px"
    android:paddingRight = "25px"
    />
   <EditText 
    android:layout_width="150px" 
    android:layout_height="wrap_content"
    android:layout_toRightOf = "@id/user" 
    android:id = "@+id/useredit"
    />
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Password"
    android:id = "@+id/pass"
    android:paddingTop = "35px"
    android:paddingRight = "25px"
    android:layout_below = "@id/user"
    />
   <EditText 
    android:layout_width="150px" 
    android:layout_height="wrap_content"
    android:layout_below =  "@id/useredit"
    android:layout_toRightOf = "@id/pass" 
    />
</RelativeLayout>

b) relativeActivity.java

package org.example.relative;

import android.app.Activity;
import android.os.Bundle;

public class relativeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}