Pages

Ads 468x60px

Monday 1 July 2013

Applying Styles and themes to Activity

Applying Styles and themes to Activity

By default, an activity occupies the entire screen. However, you can also apply a dialog theme to an activity so that it is displayed as a fl oating dialog. For example, you might want to customize your activity to display as a pop-up, warning the user about some actions that they are going to perform. In this case, displaying the activity as a dialog is a good way to get their attention.

To apply a dialog theme to an activity, simply modify the <Activity> element in the
AndroidManifest.xml file by adding the android:theme attribute:

<?xml version=”1.0” encoding=”utf-8”?>
  <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
     package=”net.learn2develop.Activities”
     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:theme=”@android:style/Theme.Dialog” >
    <intent-filter>
 <action android:name=”android.intent.action.MAIN” />
    <category android:name=”android.intent.category.LAUNCHER” />
           </intent-filter>
          </activity>
        </application>
       <uses-sdk android:minSdkVersion=”9” />
</manifest>

Doing so will make the activity appear as a dialog, as shown in Figure


Hiding the Activity Title

You can also hide the title of an activity if desired (such as when you just want to display a status update to the user). To do so, use the requestWindowFeature() method and pass it the Window.FEATURE_NO_TITLE constant, like this: 

 package net.learn2develop.Activities;
     import android.app.Activity;
     import android.os.Bundle;
     import android.util.Log;
     import android.view.Window;

          public class MainActivity extends Activity
          {
                   String tag = “Events”;
                   /** Called when the activity is first created. */
                   @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    //---hides the title bar---
                          requestWindowFeature(Window.FEATURE_NO_TITLE);
                          setContentView(R.layout.main);
                         Log.d(tag, “In the onCreate() event”);
                      }
               }

This will hide the title bar, as shown in Figure


0 التعليقات:

Post a Comment

.

Education blog