Pages

Saturday, October 27, 2012

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>



















No comments:

Post a Comment