Download import android.widget.TextView

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
¡ 
¡ 
¡ 
¡ 
¡ 
What do you need to develop Android applications? Tools in android sdk First Android Application Run the application in emulator Examine the structure of the project ¡ 
¡ 
¡ 
Ubuntu (Linux), Mac or Windows Java 1.6 and higher java –version
ADT Bundle from Google §  Eclipse + SDK + Android Development Tools §  http://developer.android.com/sdk/index.html ¡ 
Eclipse §  Download from http://eclipse.org §  Android ADT Plugin https://dl-­‐ssl.google.com/android/eclipse/ §  Adding Java and Android bin directories to path ¡  Getting the latest updates for Andoid SDK ¡  Android Virtual Device Manager Create New Virtual Device Configured Virtual Devices Name of Virtual Device Device Type External Storage Create and model device characteristics in the Android Emulator. Each AVD functions as an independent device with its own storage and user data. Design, Debug and Test your application in an actual Android Runtime Environment without a real device ¡  Optimizes the way an application is packaged ¡  Potentially makes the application and system run faster ¡  Android accesses resources efficiently where they are aligned on 4-­‐byte boundaries ¡  ADT automatically align release packages $ zipalign –c –v 4 helloworld.apk
¡  Set the path to the android tools directory ¡  On Windows set path=%path%;path_to_Android_SDK_directory\tools; path_to_Android_SDK_directory\playtform-­‐tools; ¡  On Mac/Linux export PATH=$PATH:/path_to_Android_SDK_directory/tools; path_to_Android_SDK_directory\tools; ¡  Android Asset Packaging Tool §  aapt ¡  Android Debug Bridge §  adb devices §  adb shell §  adb install myapp.apk ¡  Telnet §  telnet localhost 5554 Helloworld ! 1 2 3 4 5 All source code here
All non-code
resources
Images
Java code for our activity
Generated Java code
Helps link resources to
Java code
Layout of the activity
Strings used in the
program
Android Manifest
17
<?xml version="1.0" encoding="utf-­‐8"?> Android version support <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld” android:versionCode=”1” android:versionName="1.0" > <uses-­‐sdk android:minSdkVersion=”8” android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" Application Information android:theme="@style/AppTheme" > <activity android:name="com.example.helloworld.HelloWorldActivity" 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 </application> </manifest>
18
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HelloWorldActivity" > <TextView android:id="@+id/hello_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" Add identifier to the android:text="@string/hello_world" /> TextView </RelativeLayout>
19
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HelloWorldActivity" > <TextView android:id="@+id/hello_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
Value stored in the string hello_world 20
<?xml version="1.0" encoding="utf-­‐8"?> <resources> <string name="app_name">HelloWorld</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> </resources> Change the value 21
¡  Add another string to your strings.xml file ¡  Add another TextView to your layout file that refers the string ¡  Change the size of the Text ¡  Change the color of the text ¡  Create a colors.xml ¡  Refer the color created in the View package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
Inherit from Activity Class public class HelloWorldActivity extends Activity {
}
….
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world);
TextView tv = (TextView) findViewById(R.id.hello_txt);
tv.setText("Hello my name is Jasmit");
24
Run in the emulator 25
<?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>
Redirection to the value stored in res/values/strings.xml @2011 Mihail L. Sichitiu
26
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
// import android.widget.TextView;
public class HelloWorldActivity extends Activity {
}
….
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world);
// TextView tv = (TextView) findViewById(R.id.hello_txt);
// tv.setText("Hello my name is Jasmit");
Comment the previous changes 27
package com.example.helloworld;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Customize dimensions originally defined in res/values/dimens.xml (such as
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080001;
public static final int hello_txt=0x7f080000;
}
public static final class layout {
public static final int hello_world=0x7f030000;
}
public static final class menu {
public static final int hello_world=0x7f070000;
}
…..
}
28
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
o.toString();
setContentView(R.layout.main);
}
}
29
¡  Drag and Drop a Button Control and give it id my_button ¡  Add the Button XML to your layout file <Button!
android:id="@+id/my_button"!
android:layout_width="match_parent"!
android:layout_height="wrap_content"!
android:text=”Click Me" /> Button btn =
(Button)findViewById(R.id.my_button);!
!
btn.setOnClickListener!
(new View.OnClickListener() {!
"public void onClick(View view) { ""
"Toast.makeText(getBaseContext(),
"Button was clicked!", "
"
""
"Toast.LENGTH_SHORT).show();!
"}!
}); ¡  Change the text on the screen when you click the button ¡  Mechanism for collecting and viewing system messages ¡  Logcat dumps messages §  System messages such as error and warning stack traces §  Messages from the Log class in an application Method Description Log.v verbose Log.d debug Log.i information Log.w warning Log.e error