Pages

Saturday, October 27, 2012

Android Simple Button click Example with String Resources



Your Activity
package pro2.pro3;

import android.app.Activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.widget.Toast;

public class ButtonExample extends Activity implements OnClickListener
{

  private Button hiButton;
  private Button helloButton;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    hiButton = (Button) findViewById(R.id.hi_button);
    hiButton.setOnClickListener(this);
    helloButton = (Button) findViewById(R.id.hello_button);
    helloButton.setOnClickListener(this);
  }

  public void onClick(View v)
  {

    EditText nameField = (EditText) findViewById(R.id.name_field);
    String name = nameField.getText().toString();
    if (name.length() == 0)
    {
      new AlertDialog.Builder(this).setMessage(R.string.error_name_missing)
          .setNeutralButton(R.string.error_ok, null).show();
      return;

    }
    if (v == hiButton || v == helloButton)
    {
      int resourceId = v == hiButton ? R.string.hi_greeting
          : R.string.hello_greeting;
      String greeting = getResources().getString(resourceId, name);
      Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();
      TextView greetingField = (TextView) findViewById(R.id.greeting_field);
      greetingField.setText(greeting);

    }
  }
}


Create main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
   android:id="@+id/greeting_field"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_width="match_parent"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:text="@string/enter_your_name"
android:textColor="#ff00ff"/>
<EditText
   android:id="@+id/name_field"
   android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
   android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">

<Button
    android:id="@+id/hi_button"
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
android:text="@string/hi_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

<Button
   android:id="@+id/hello_button"
   android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

</LinearLayout>
</LinearLayout>



Define main.xml  strings in your strings.xml file

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="hello">Hello World, manu!</string>
    <string name="app_name">pro1</string>
    <string name="enter_your_name">Enter your name:</string>
    
    <string name="hi_button">Say Hi!</string>
<string name="hello_button">Say Hello!</string>

<string name="error_name_missing">Please enter your name</string>
<string name="error_ok">OK</string>

<string name="hi_greeting">Hi %s!</string>
<string name="hello_greeting">Hello %s!</string>
</resources>

No comments:

Post a Comment