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>

Simple Example to move one activity to another in android


First Create your 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"
android:gravity="center">
<EditText
      android:id="@+id/et_data"
      android:hint="EditText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
</EditText>
<Button
     android:text="Next Screen"
     android:id="@+id/next_screen_btn"
    android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

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

public class NextScreen extends Activity
{
  private TextView tvText;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextview);

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  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=".NextScreen"/>
    </application>
</manifest>



















Android Simple Tutorial to Send Data from First Activity to Second Activity



First Create your 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"
android:gravity="center">
<EditText
     android:id="@+id/et_data"
     android:hint="EditText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
</EditText>
<Button
    android:text="Next Screen"
    android:id="@+id/next_screen_btn"
  android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

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

public class NextScreen extends Activity
{
  private TextView tvText;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextview);

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  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=".NextScreen"/>
    </application>
</manifest>