Tuesday, September 1, 2015

One Day in Android Programming

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

  1. In Eclipse, create new Android Application, named "UnitConverter". It will be the application name, "app_name" can be found in /res/values/strings.xml.
  2. Named the main activity to "ConverterChoicesActivity", and related layout to "lo_converter_choices.xml".
  3. Create java class, named "ConverterActivity" extends "android.app.Activity".
  4. Create java class, named "Converter" extends "java.lang.Object", select checkbox "Constructors from Superclass".
  5. Create file, named "lo_convert_me.xml".
  6. Modify AndroidManifest.xml, 
    • activity "ConverterActivity".
  7. Modify ConverterChoicesActivity.java, 
    • replace "extends Activity" by "extends ListActivity".
    • update method "onCreate".
    • update method "onListItemClick".
    • add method "loadConverter".
    • remove "onCreateOptionsMenu".
    • remove "onOptionsItemSelected".
  8. Modify ConverterActivity.java.
    • modify method "onCreate".
    • add method "addListenerOnButton"
  9. Modify Converter.java.
    • add method "convertInchToCm".
    • add method "convertFehrenheitToCelsius".
  10. modify lo_converter_choices.xml
    • change the layout to "LinearLayout", if it isn't.
    • add TextView "display_selection".
    • add ListView "list".
    • add TextView "item".
  11. 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

  1. The Google I/O 2014 App  https://github.com/google/iosched 
  2. Android Training http://developer.android.com/training/index.html
  3. K-9 Mail - Advanced Email for Android  https://github.com/k9mail/k-9/
  4. Agit - Git client for Android https://github.com/rtyley/agit/
  5. Used to integrated Android Apps with Facebook Platform https://github.com/facebook/facebook-android-sdk 
  6. Replica Island https://code.google.com/p/replicaisland/  http://replicaisland.net/ 
  7. Notepad Tutorial http://developer.android.com/training/notepad/index.html

Handy Development Tools 

  1. Droid-Fu - A utility library for daily Android needs https://github.com/mttkay/droid-fu 
  2. Ignition - Kick-starts Android application development https://github.com/mttkay/ignition 
  3. Roboguice - Google Guice on Android, v3.0 https://github.com/mttkay/ignition 
  4. Google Translator Toolkit https://translate.google.com/toolkit/ 
  5. Draw 9-patch http://developer.android.com/tools/help/draw9patch.html 
  6. Hierarchy Viewer http://developer.android.com/tools/help/hierarchy-viewer.html 
  7. UI/Application Exerciser Monkey - Stress-test applications http://developer.android.com/tools/help/monkey.html 
  8. Zipalign - An archive alignment tool http://developer.android.com/tools/help/zipalign.html 
  9. Lint - code scanning tool (verify code standard) http://developer.android.com/tools/debugging/improving-w-lint.html
  10. Git - fast, light, open-source-distributed version control system https://git-scm.com/ 
  11. Free image manipulation software http://www.gimp.org/ http://www.getpaint.net/ 
  12. SQLCipher - Encrypted Database https://guardianproject.info/code/sqlcipher/ 







No comments:

Post a Comment