Showing posts with label Android/Java programming basic. Show all posts
Showing posts with label Android/Java programming basic. Show all posts

Wednesday, April 9, 2014

MainActivity.this vs getApplicationContext()

This example examine the behaviour between the contexts retrieved with MainActivity.this vs getApplicationContext(). The getApplicationContext() method return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

Shown in the following video, MainActivity.this will change when activity destroyed and re-created, getApplicationContext() will change when the application killed and re-started.


MainActivity.java
package com.example.androidtestcontext;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView info1 = (TextView)findViewById(R.id.info1);
  TextView info2 = (TextView)findViewById(R.id.info2);

  String s1 = "MainActivity.this:\n" 
   + "getClass() = " + MainActivity.this.getClass() + "\n"
   + MainActivity.this;
  info1.setText(s1);
  
  String s2 = "getApplicationContext():\n" 
    + "getClass() = " + getApplicationContext().getClass() + "\n"
    + getApplicationContext();
  
  info2.setText(s2);
 }

}

layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/info1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/info2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic" />

</LinearLayout>

Tuesday, April 1, 2014

Step-by-step to create a simple Android App

The video show the steps to build a simple Android App using Android-ADT (Eclipse), in 7 minutes. In the app, clicking on the button to show a photo, and clicking on the photo to remove itself. In the end of the video show how it in working.

  • The app generated using the Project Wizard of Android SDK Tools 22.6.2, ActionBarActivity created with Fragment. So we have to modify fragment_main.xml for layout, and handle our ui elements in onCreateView() of PlaceholderFragment class.
  • The photo is stored inside the app /res/drawable/spiderman.jpg, its name must be in lowercase letter.

/res/layout/fragment_main.xml
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.showmyphoto.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button
        android:id="@+id/mybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show my photo" />
    
    <ImageView
        android:id="@+id/myimageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

/src/com/example/showmyphoto/MainActivity.java
package com.example.showmyphoto;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 /**
  * A placeholder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {
  
  Button myButton;
  ImageView myImageView;

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container,
     false);
   
   myButton = (Button)rootView.findViewById(R.id.mybutton);
   myImageView = (ImageView)rootView.findViewById(R.id.myimageview);
   
   myButton.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     myImageView.setImageResource(R.drawable.spiderman);
    }});
   
   myImageView.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     myImageView.setImageBitmap(null);
    }});
   
   return rootView;
  }
 }

}


Monday, September 2, 2013

Implement callback function with interface

This example demonstrate how to implement callback function with interface.

Implement callback function with interface

Create a MyAsyncTask class extends AsyncTask. Inside MyAsyncTask, interface DoSomething declared. MyAsyncTask class handle the background timing only, know knowing about the actual jobs. The actual jobs are in the class implement DoSomething.

package com.example.androidcallback;

import android.os.AsyncTask;
import android.os.SystemClock;

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

 interface DoSomething {
  void doInBackground(int progress);
  void doPostExecute();
 }

 DoSomething myDoSomethingCallBack;
 int myMax;
 
 MyAsyncTask(DoSomething callback, int max){
  myDoSomethingCallBack = callback;
  myMax = max;
 }

 @Override
 protected Void doInBackground(Void... params) {
  for (int i = 0; i <= myMax; i++) {
   SystemClock.sleep(100);
   myDoSomethingCallBack.doInBackground(i);
  }
  return null;
 }

 @Override
 protected void onPostExecute(Void result) {
  super.onPostExecute(result);
  myDoSomethingCallBack.doPostExecute();
 }

}


MainActivity.java, implements DoSomething. Itself (this) will be passed to MyAsyncTask constructor, to implement callback function.
package com.example.androidcallback;

import com.example.androidcallback.MyAsyncTask.DoSomething;

import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity implements DoSomething{
 
 ProgressBar myProgressBar;
 MyAsyncTask myAsyncTask;
 int myProgress;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  myProgressBar = (ProgressBar)findViewById(R.id.myprogressbar);
  
  myProgress = 0;
  myAsyncTask = new MyAsyncTask(this, 100);
  myAsyncTask.execute();
 }

 @Override
 public void doInBackground(int i) {
  myProgressBar.setProgress(i);
 }

 @Override
 public void doPostExecute() {
  Toast.makeText(MainActivity.this, 
    "Finish", Toast.LENGTH_LONG).show();
 }

}


layout
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <ProgressBar 
        android:id="@+id/myprogressbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="0" />

</LinearLayout>


download filesDownload the files.