HelloAndroid.java :  » Samples » ew-samples » com » sap » test » android » eoth » Android Open Source

Android Open Source » Samples » ew samples 
ew samples » com » sap » test » android » eoth » HelloAndroid.java
package com.sap.test.android.eoth;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  private JSONObject jo;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TableLayout layout = new TableLayout(this);

    final TextView nameTextView = new TextView(this);
    final EditText et = new EditText(this);
    final Button button = new Button(this);
    final ImageView iv = new ImageView(this);
    final TextView result = new TextView(this);

    final Button startButton = new Button(this);
    startButton.setText("Start");
    startButton.setOnClickListener(new OnClickListener() {

      public void onClick(View arg0) {
        String uri = "http://eothdemo.appspot.com/rest/random";
        jo = RestClient.fetch(uri);

        try {
          nameTextView.setText(jo.getString("name"));
          Bitmap imageBitmap = RestClient.getImageBitmap(jo.getString("pictureUrl"));
          iv.setImageBitmap(imageBitmap);
        } catch (JSONException e) {
          
          e.printStackTrace();
        }

      }

    });

    button.setText("Submit");
    button.setOnClickListener(new OnClickListener() {

      public void onClick(View arg0) {
        try {
          if (et.getText().toString().equals(jo.getString("game"))) {
            result.setText("Correct!!!");
          } else {
            result.setText("Wrong! Correct answer is ... " + jo.getString("game"));
          }
        } catch (JSONException e) {
          result.setText("An error occured!");
        }

      }

    });


    layout.addView(startButton);
    layout.addView(nameTextView);

    layout.addView(iv);
    layout.addView(et);

    layout.addView(button);
    layout.addView(result);

    setContentView(layout);
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.