Monday, April 25, 2016

Launching different activity from main activity of Android App


  1. Using Eclipse, create a new Android project and name it UsingIntent.
  2. Right-click on the package name under the src folder and select New  Class.
  3. Name the new class SecondActivity and click Finish.
  4. 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.UsingIntent”
        android:versionCode=“1”
        android:versionName=“1.0” >
    
        <uses-sdk android:minSdkVersion=“14” />
    
        <application
            android:icon=“@drawable/ic_launcher”
            android:label=“@string/app_name” >
            <activity
                android:label=“@string/app_name”
                android:name=“.UsingIntentActivity” >
                <intent-filter >
                    <action android:name=“android.intent.action.MAIN” />
                    <category android:name=“android.intent.category.LAUNCHER” />
                </intent-filter>
            </activity>
            <activity
                android:label=“Second Activity”
                android:name=“.SecondActivity” >
                <intent-filter >
                    <action android:name=“net.learn2develop.SecondActivity” />
                    <category android:name=“android.intent.category.DEFAULT” />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
  5. Make a copy of the main.xml file (in the res/layout folder) by right-clicking on it and selecting Copy. Then, right-click on the res/layout folder and select Paste. Name the file secondactivity.xml. The res/layout folder will now contain the secondactivity.xml file (see Figure 2.10).

  6. Modify the secondactivity.xml file as follows:
    <?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=“fill_parent”
        android:orientation=“vertical” >
    
        <TextView
            android:layout_width=“fill_parent”
            android:layout_height=“wrap_content”
            android:text=“This is the Second Activity!” />
    
    </LinearLayout>
    
  7. In the SecondActivity.java file, add the following statements in bold:
    package net.learn2develop.UsingIntent;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class SecondActivity extends Activity{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.secondactivity);
        }
    }
    
  8. Add the following lines 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:layout_width=“fill_parent”
        android:layout_height=“fill_parent”
        android:orientation=“vertical” >
    
    <Button
        android:layout_width=“fill_parent”
        android:layout_height=“wrap_content”
        android:text=“Display second activity”
        android:onClick=“onClick”/>
    
    </LinearLayout>
    
  9. Modify the UsingIntentActivity.java file as shown in bold:
    package net.learn2develop.UsingIntent;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class UsingIntentActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        public void onClick(View view) {
            startActivity(new Intent(”net.learn2develop.SecondActivity”));
        }
    }

Tuesday, November 27, 2012

How to hide Activity Title in Android App

Android allows to hide your activity title if you wish to hide. To hide the activity title use the requestWindowFeature() method and pass it the Window.FEATURE_NO_TITLE flag during on create event of the activity. 


Activity with Title

No Title
Also make sure that before you call the setContentView of the activity you will have to call the requestWindowFeature() method. 
Code below of a sample activity helps you to understand the usage of requestWindowFeature() method.


package com.example.sampleapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);        
    }
}

Also make sure that you import android.view.Window package when you use requestWindowFeature() method.

How to dynamically create a button in Android App


Some times you required to create the dynamic button on your android application. Code below shows how to add the button to the dynamically created relative layout.


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout myLayout = new LinearLayout(this);              

Button btn = new Button(this);
btn.setText(numbers.get(i).toString());
btn.setId(numbers.get(i) );

//Layout Parameters for button
LayoutParams lpbtn = new LayoutParams(
   LayoutParams.WRAP_CONTENT,
   LayoutParams.WRAP_CONTENT);    

//set layout params for the button
btn.setLayoutParams(lpbtn);

//Add button to layout
myLayout.addView(btn);

//Layout parameters for linear layout
LayoutParams llp = new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);    

this.addContentView(myLayout, llp);
    }