Android Programming
Part 1 Setup Part 2 Programming Part 3 Troubleshoot (Coming Soon)In this Blog Post
- Create Android UI, Activity
Basics
- Android SDK
- Minimum Required SDK : API 19: Android 4.4 (KitKat)
- Target SDK : API 19: Android 4.4 (KitKat)
- Compile with : API 19: Android 4.4 (KitKat)
- File Structure
- /AndroidManifest.xml
- /src/*.java
- /bin/classes/*.classes
- /bin/*.apk
- /gen/.../R.java
- /res/drawable-hdpi/ic_launcher.png
- /res/layout/activity*.xml
- /res/menu/menu*.xml
- /res/values/String.xml
Create Android UI, Activity
- In Eclipse, create new Android Application, named "UnitConverter". It will be the application name, "app_name" can be found in /res/values/strings.xml.
- Named the main activity to "ConverterChoicesActivity", and related layout to "lo_converter_choices.xml".
- Create java class, named "ConverterActivity" extends "android.app.Activity".
- Create java class, named "Converter" extends "java.lang.Object", select checkbox "Constructors from Superclass".
- Create file, named "lo_convert_me.xml".
- Modify AndroidManifest.xml,
- activity "ConverterActivity".
- Modify ConverterChoicesActivity.java,
- replace "extends Activity" by "extends ListActivity".
- update method "onCreate".
- update method "onListItemClick".
- add method "loadConverter".
- remove "onCreateOptionsMenu".
- remove "onOptionsItemSelected".
- Modify ConverterActivity.java.
- modify method "onCreate".
- add method "addListenerOnButton"
- Modify Converter.java.
- add method "convertInchToCm".
- add method "convertFehrenheitToCelsius".
- modify lo_converter_choices.xml
- change the layout to "LinearLayout", if it isn't.
- add TextView "display_selection".
- add ListView "list".
- add TextView "item".
- modify lo_convert_me.xml
- add TextView "text_convert_type".
- add TextView "label_convert_from".
- add EditText "edit_convert_from".
- add TextView "label_convert_to".
- add TextView "text_convert_to".
- add Button "button_convert".
Sample Code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chewy.unitconverter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ConverterChoicesActivity"
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=".ConverterActivity"
/>
</application>
</manifest>
|
lo_converter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chewy.unitconverter.ConverterChoicesActivity" >
<TextView
android:id="@+id/display_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
/>
</LinearLayout>
|
lo_convert_me.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chewy.unitconverter.ConverterChoicesActivity" >
<TextView
android:id="@+id/display_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
/>
</LinearLayout>
|
package com.chewy.unitconverter; import android.app.ListActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; public class ConverterChoicesActivity extends ListActivity { private ListAdapter mStaticAdapter; // int selectedMenuItem; int selectedItemInt; TextView selectedItem; String[] items; TextView displaySelection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lo_converter_choices); Resources res = this.getResources(); items = new String[] { res.getString(R.string.item_inch_cm), res.getString(R.string.item_fahrenheit_celsius) }; mStaticAdapter = new ArrayAdapter<String>( this, //getActivity(), R.layout.lo_converter_choices, R.id.item, items); setListAdapter(mStaticAdapter); selectedItem=(TextView)findViewById(R.id.item); displaySelection = (TextView)findViewById(R.id.display_selection); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String text = " position:" + position + " " + items[position]; this.displaySelection.setText(text); selectedItemInt = position; this.loadConverter(v); } private void loadConverter(View view) { Intent converterIntent = new Intent(this, ConverterActivity.class); Bundle bundle = new Bundle(); bundle.putString ("passMeOn", this.displaySelection.getText().toString()); bundle.putInt("selectedItem", selectedItemInt); converterIntent.putExtras(bundle); this.startActivity(converterIntent); } } |
package com.chewy.unitconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ConverterActivity extends Activity {
private static final int CASE_INCH_CM = 0;
private static final int CASE_FAH_CEL = 1;
private int selectedItemInt = 0;
private TextView tvConvertFrom;
private TextView tvConvertTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lo_convert_me);
Bundle bundle = this.getIntent().getExtras();
String thingPassedOntoMe = bundle.getString ("passMeOn");
selectedItemInt = bundle.getInt("selectedItem");
TextView tvConvertType = (TextView)findViewById(R.id.text_convert_type);
tvConvertType.setText(thingPassedOntoMe);
tvConvertFrom = (TextView)findViewById(R.id.edit_convert_from);
tvConvertFrom.setText("");
tvConvertTo = (TextView)findViewById(R.id.text_convert_to);
tvConvertTo.setText("0.00");
addListenerOnButton();
}
private void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button_convert);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
float result =0f;
switch (selectedItemInt) {
case CASE_INCH_CM:
result = Converter.convertInchToCm(Float.valueOf(tvConvertFrom.getText().toString()));
break;
case CASE_FAH_CEL:
result = Converter.convertFehrenheitToCelsius(Float.valueOf(tvConvertFrom.getText().toString()));
break;
} // end switch
tvConvertTo.setText(String.valueOf(result));
}
});
}
}
|
package com.chewy.unitconverter;
public class Converter {
public Converter() {
// TODO Auto-generated constructor stub
}
public static float convertInchToCm(float inch){
float cm = Float.valueOf( inch*2.54f );
return cm;
}
public static float convertFehrenheitToCelsius(float fehrenheit){
float cel = Float.valueOf( (fehrenheit-32f)*(5f/9f) );
return cel;
}
}
|
-- |
Sample Android Applications and SDKs
- The Google I/O 2014 App https://github.com/google/iosched
- Android Training http://developer.android.com/training/index.html
- K-9 Mail - Advanced Email for Android https://github.com/k9mail/k-9/
- Agit - Git client for Android https://github.com/rtyley/agit/
- Used to integrated Android Apps with Facebook Platform https://github.com/facebook/facebook-android-sdk
- Replica Island https://code.google.com/p/replicaisland/ http://replicaisland.net/
- Notepad Tutorial http://developer.android.com/training/notepad/index.html
Handy Development Tools
- Droid-Fu - A utility library for daily Android needs https://github.com/mttkay/droid-fu
- Ignition - Kick-starts Android application development https://github.com/mttkay/ignition
- Roboguice - Google Guice on Android, v3.0 https://github.com/mttkay/ignition
- Google Translator Toolkit https://translate.google.com/toolkit/
- Draw 9-patch http://developer.android.com/tools/help/draw9patch.html
- Hierarchy Viewer http://developer.android.com/tools/help/hierarchy-viewer.html
- UI/Application Exerciser Monkey - Stress-test applications http://developer.android.com/tools/help/monkey.html
- Zipalign - An archive alignment tool http://developer.android.com/tools/help/zipalign.html
- Lint - code scanning tool (verify code standard) http://developer.android.com/tools/debugging/improving-w-lint.html
- Git - fast, light, open-source-distributed version control system https://git-scm.com/
- Free image manipulation software http://www.gimp.org/ http://www.getpaint.net/
- SQLCipher - Encrypted Database https://guardianproject.info/code/sqlcipher/
No comments:
Post a Comment