Showing posts with label Android Fundamentals. Show all posts
Showing posts with label Android Fundamentals. Show all posts

Tuesday, 2 July 2013

Attach Android source code to Eclipse IDE

// siddhu vydyabhushana // 8 comments
By default, Android SDK or Eclipse ADT plugin are not bundle with any Android’s source code for debugging. In Eclipse IDE, step into any Android class will prompt no source code attach, see following screen :
android source code not found in Eclipse

Solution

According to this official Android source code article, it’s shocked that we need to use “repo” to download and then build the entire source code to get the source for “android.jar“.
If you think above is too much works, alternatively, you can install the Eclipse plugin called “Android Source” to get source for “android.jar“. Read this article “Additional Eclipse plugins for Android“.
After installed the “Android source” plugin, suppose the existing projects as well as new created projects which is targeted for Android will have attached the source jar automatically. However, my existing Android project still didn’t attach to the correct source, i have to attach it manually.
Locate “Android Source” plugin folder, it should be in following directory :
ECLIPSE_PATH\plugins\com.android.ide.eclipse.source_MAY_BE_VARY
Folders :
android source folders
  • 14 – Android 4.0.1
  • 10 – Android 2.3.4
  • 9 – Android 2.3
  • 8 – Android 2.2
  • 7 – Android 2.1
  • 6 – Android 2.0.1
  • 4 – Android 1.6
  • 3 – Android 1.5
Each folder contain a “sources.zip“, which target to specific Android version. For example, if you develop Android 2.3, then get the “sources.zip” from folder “10“, and attach it to the Eclipse IDE manually.
Step into Android class again, source code is display.
attach android source code to eclipse ide
Read More

Android activity – from one screen to another screen

// siddhu vydyabhushana // 3 comments
In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens, for example, one activity to display a list of the application settings, another activity to display the application status.
Note
Refer to this official Android activity article to understand more about Android activity.
In this tutorial, we show you how to interact with activity, when a button is clicked, navigate from current screen (current activity) to another screen (another activity).
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. XML Layouts

Create following two XML layout files in “res/layout/” folder :
  1. res/layout/main.xml – Represent screen 1
  2. res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m screen 1 (main.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me to another screen" />
 
</LinearLayout>
File : res/layout/main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m screen 2 (main2.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
</LinearLayout>

2. Activities

Create two activity classes :
  1. AppActivity.java –> main.xml
  2. App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
    Intent intent = new Intent(context, anotherActivity.class);
    startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
 
public class AppActivity extends Activity {
 
	Button button;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addListenerOnButton();
	}
 
	public void addListenerOnButton() {
 
		final Context context = this;
 
		button = (Button) findViewById(R.id.button1);
 
		button.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
 
			    Intent intent = new Intent(context, App2Activity.class);
                            startActivity(intent);   
 
			}
 
		});
 
	}
 
}
File : App2Activity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
 
public class App2Activity extends Activity {
 
	Button button;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
	}
 
}

3. AndroidManifest.xml

Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="10" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".App2Activity" >
        </activity>
    </application>
 
</manifest>

4. Demo

Run application.
AppActivity.java (main.xml) screen is display.
android activity demo1
When above button is clicked, it will navigate to another screen App2Activity.java (main2.xml).
android activity demo2

Download Source Code

Read More

Monday, 1 July 2013

Android wrap_content and fill_parent example

// siddhu vydyabhushana // 5 comments
n Android, you always put either “wrap_content” or “fill_parent” on component’s attribute “layout_width” and “layout_height“, did you wonder what’s the different?
See following definition :
  1. wrap_content – The component just want to display big enough to enclose its content only.
  2. fill_parent – The component want to display as big as its parent, and fill in the remaining spaces. (renamed match_parent in API Level 8)
Above terms may not make sense now, let see following demonstration :

1. wrap_content

A button component, set “wrap_content” on both width and height attribute. It tell Android to display the button big enough to enclose it’s content “Button ABC” only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/btnButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button ABC"/>
 
</RelativeLayout>
android wrap-content example1

2. fill_parent – width

Change the “layout_width” to “fill_parent“, now, the button’s width will fill in the remaining spaces, just as big as it’s parent “RelativeLayout“, but button’s height is still big enough to enclose it’s content only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/btnButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button ABC"/>
 
</RelativeLayout>
android wrap-content example2

3. fill_parent – height

Change the “layout_height” to “fill_parent“, now, the button’s height will fill in the remaining spaces, just as big as it’s parent “RelativeLayout“, but button’s width is still big enough to enclose it’s content only.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/btnButton1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button ABC"/>
 
</RelativeLayout>
android wrap-content example3

4. fill_parent – width, height

Change the both “layout_width” and “layout_height” to “fill_parent“, the button will display as big as the whole device screen, it just fill in the entire screen space.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <Button
        android:id="@+id/btnButton1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Button ABC"/>
 
</RelativeLayout>
android wrap-content example4
Note
Actually, you can specifying an exact width and height, but it’s not recommended, due to Android variety of devices screen size. You just do not know what size of Android device is running your fantasy application.
Read More