Friday, 7 October 2011

Designing Your User interface Using Views


Designing Your User interface Using Views:
                                                                               "In Android UI consists of view the viewGroups are divided into following categories."

➤➤ Basic views — Commonly used views such as the TextView, EditText, and Button
views

➤➤ Picker views — Views that enable users to select from a list, such as the TimePicker
and DatePicker views

➤➤ List views — Views that display a long list of items, such as the ListView and the
SpinnerView views

BASIC VIEWS:
                       "The basic views enable you to display text information, as well as perform some basic selection.To get started, let’s explore some of the basic views that you can use to design the UI of your Android applications:"

➤➤ TextView

➤➤ EditText

➤➤ Button

➤➤ ImageButton

➤➤ CheckBox

➤➤ ToggleButton

➤➤ RadioButton

➤➤ RadioGroup

The following sections explore all these views in more detail.

TextView view :"The TextView view is used to display text to the user. This is the most basic view and one that you will frequently use when you develop Android applications. If you need to allow users to edit the text displayed, you should use the subclass of TextView, EditText, which is discussed in the next section."

When you create a new Android project, Eclipse always creates the main.xml fi le (located in the res/
layout folder), which contains a <TextView> element:

exj-

   <?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=”@string/hello”
   />
</LinearLayout>


                                      

Monday, 3 October 2011

Registering Events for Views

Registering Events for Views :
                                                             "Views can fire events when users interact with them. For example, when a user touches a Buttonview, you need to service the event so that the appropriate action can be performed. To do so, youneed to explicitly register events for views.Using the same example discussed in the previous section, recall that the activity has two Buttonviews; therefore, you can register the button click events using an anonymous class as shown here:"


 exj:
 

package net.learn2develop.UIActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button btn1 = (Button)findViewById(R.id.btn1);
            btn1.setOnClickListener(btnListener);
            Button btn2 = (Button)findViewById(R.id.btn2);
            btn2.setOnClickListener(btnListener);
      }
      //   create an anonymous class to act as a button click listener
private OnClickListener btnListener = new OnClickListener()
{
   public void onClick(View v)


    {
      Toast.makeText(getBaseContext(),
      ((Button) v).getText() + “ was clicked”,
      Toast.LENGTH_LONG).show();
     }
};

override the keydown method

public boolean onKeyDown(int keyCode, KeyEvent event)
{
   switch (keyCode)
   {
      //...
     //...
   }
   return false;
  }
}






"If you now press either the OK button or the Cancel button, the appropriate message will be displayed 
proving that the event is wired up properly."






Wednesday, 28 September 2011

Controlling the Orientation of the Activity

"Occasionally you might want to ensure that your application is only displayed in a certain orientation."

For example, you may be writing a game that should only be viewed in landscape mode. In this
case, you can programmatically force a change in orientation using the setRequestOrientation()
method of the Activity class:





import android.content.pm.ActivityInfo;
public class MainActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          }


To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Besides using the setRequestOrientation() method, you can also use the android:screenOrientation
attribute on the <activity> element in AndroidManifest.xml as follows to constrain the activity to a
certain orientation:
 <?xml version=”1.0” encoding=”utf-8”?>
       <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
              package=”net.learn2develop.Orientations”
               android:versionCode=”1”
               android:versionName=”1.0”>
       <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
        <activity android:name=”.MainActivity”
            android:label=”@string/app_name”
                   android:screenOrientation=”landscape” >
          <intent-filter>
      <action android:name=”android.intent.action.MAIN” />
     <category android:name=”android.intent.category.LAUNCHER” />
   </intent-filter>
</application>
<uses-sdk android:minSdkVersion=”9” />
</manifest>

Following are two other values that you can specify in the android:screenOrientation attribute:
       1.  portrait — Portrait mode
       2.  sensor — Based on the accelerometer

Sunday, 25 September 2011

Displaying Notifications

So far, you have been using the Toast class to display messages to the user. While the Toast class is
a handy way to show users alerts, it is not persistent. It flashes on the screen for a few seconds and
then disappears. If it contains important information, users may easily miss it if they are not looking
at the screen.
For messages that are important, you should use a more persistent method. In this case, you should
use the Notification Manager to display a persistent message at the top of the device, commonly
known as the status bar (sometimes also referred to as the notification bar). The following Try It
Out demonstrates how.


STEPS

1. Using Eclipse, create a new Android project and name it Notifications.

2. Add a new class file named NotificationView.java to the src folder of the project (see Figure 2-26).
    In addition, add a new notification.xml file to the res/layout folder as well.
    Figure 2-26

3. Populate the notification.xml file as follows:
   <?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=”Here are the details for the notification...” />
  </LinearLayout>

4. Populate the NotificationView.java file as follows:
   package net.learn2develop.Notifications;
   import android.app.Activity;
   import android.app.NotificationManager;
   import android.os.Bundle;
   public class NotificationView extends Activity
  {
    @Override
     public void onCreate(Bundle savedInstanceState)
    {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.notification);
    //---look up the notification manager service---
    NotificationManager nm = (NotificationManager)
    getSystemService(NOTIFICATION_SERVICE);
   //---cancel the notification that we started
   nm.cancel(getIntent().getExtras().getInt(“notificationID”));
   }
}

5. Add the following statements in bold to the AndroidManifest.xml file:
   <?xml version=”1.0” encoding=”utf-8”?>
   <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”net.learn2develop.Notifications”
   android:versionCode=”1”
   android:versionName=”1.0”>
  <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
  <activity android:name=”.MainActivity”
  android:label=”@string/app_name”>
  <intent-filter>
  <action android:name=”android.intent.action.MAIN” />
  <category android:name=”android.intent.category.LAUNCHER” />
  </intent-filter>
  </activity>
  <activity android:name=”.NotificationView”
  android:label=”Details of notification”>
  <intent-filter>
  <action android:name=”android.intent.action.MAIN” />
  <category android:name=”android.intent.category.DEFAULT” />
  </intent-filter>
  </activity>
  </application>
  <uses-sdk android:minSdkVersion=”9” />
  <uses-permission android:name=”android.permission.VIBRATE” />
</manifest>

6. Add the following statements in bold to the main.xml file:
  <?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:id=”@+id/btn_displaynotif”
  android:layout_width=”fill_parent”
  android:layout_height=”wrap_content”
  android:text=”Display Notification” />
  </LinearLayout>

7. Finally, add the following statements in bold to the MainActivity.java file:
   package net.learn2develop.Notifications;
   import android.app.Activity;
   import android.os.Bundle;
  import android.app.Notification;
  import android.app.NotificationManager;
   import android.app.PendingIntent;
  import android.content.Intent;
  import android.view.View;
  import android.widget.Button;
  public class MainActivity extends Activity {
  int notificationID = 1;
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
  Button button = (Button) findViewById(R.id.btn_displaynotif);
  button.setOnClickListener(new Button.OnClickListener() {
  public void onClick(View v) {
  displayNotification();
}
});
}
protected void displayNotification()
{
  Intent i = new Intent(this, NotificationView.class);
  i.putExtra(“notificationID”, notificationID);
  PendingIntent pendingIntent =
  PendingIntent.getActivity(this, 0, i, 0);
 NotificationManager nm = (NotificationManager)
  getSystemService(NOTIFICATION_SERVICE);
 Notification notif = new Notification(
 R.drawable.icon,
“ Reminder: Meeting starts in 5 minutes”,
 System.currentTimeMillis());
 CharSequence from = “System Alarm”;
 CharSequence message = “Meeting with customer at 3pm...”;
 notif.setLatestEventInfo(this, from, message, pendingIntent);
  notif.vibrate = new long[] { 100, 250, 100, 500};
  nm.notify(notificationID, notif);
}
}

8. Press F11 to debug the application on the Android Emulator.

9. Click the Display Notification button (see the top left of Figure 2-27) and a notification will appear
on the status bar.

10. Clicking and dragging the status bar down will reveal the notification (see the right of Figure).

11. Clicking on the notification will reveal the NotificationView activity. This also causes the
notification to be dismissed from the status bar.



                                                               Displaying Notifications