Monday 5 September 2011

Android Basics

Sample Log-in Screen:

User Interface:
                Username and password edit field with login Button.(main.xml)
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">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/UserNameText"
android:text="UserName" android:typeface="sans" android:textStyle="bold"></TextView>

<EditText android:layout_height="wrap_content" android:hint="User Name"
android:layout_width="fill_parent" android:id="@+id/username"></EditText>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/PasswordText"
android:text="Password" android:textStyle="bold"></TextView>
<EditText android:layout_height="wrap_content" android:hint="Password"
android:singleLine="true" android:layout_width="fill_parent"
android:id="@+id/password"></EditText>
</LinearLayout>
<Button android:layout_height="wrap_content" android:id="@+id/login"
android:text="Login" android:layout_gravity="center"
android:layout_width="100px"></Button>
</LinearLayout>

Activity:
               On Clicking on login button , displays an dialog with username

LoginActivity.java
package com.login;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
/** Called when the activity is first created. */
// Retrieve username and password using id.
EditText userName = null;
EditText password = null;
// Button
Button loginButton = null;
Context context = null;

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userName = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
loginButton = (Button)findViewById(R.id.login);
loginButton.setOnClickListener(new Button.OnClickListener() 
{
@Override
public void onClick(View arg0)
{
showDialog();
}
});
}
/**
 * On clicking login button, dialog is displayed with userName
 */
private void showDialog() 
{
Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Welcome "+userName.getText()+"!!!!");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {


}
});
alert.show();

}
}
Output:

Simple List View Component:


User Interface:
Composed of single list view component(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">
    <ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:dividerHeight="1px">
    </ListView>
</LinearLayout>

Activity:

In this activity , we retrieve list view component  and bind the data to the list view using array adapter.

package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        String[] tech= {"java", "blackberry","android"};
        setContentView(R.layout.main);
        ListView mylist = (ListView)findViewById(R.id.ListView01);       
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,tech);
        mylist.setAdapter(adapter);
       
    }
}
Output :



No comments:

Post a Comment