Part 13 - Passing Data To Next Activity - Method Two

 Java part : 

package first.learn.datapassingtwo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText etOne, etTwo;
Button btnSimpleInt,
btnSimpleString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etOne = (EditText) findViewById(R.id.etOne);
etTwo = (EditText) findViewById(R.id.etTwo);

btnSimpleInt = (Button)findViewById(R.id.btnSimpleInt);
btnSimpleString = (Button)findViewById(R.id.btnSimpleString);


btnSimpleString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s = etOne.getText().toString();

if(TextUtils.isEmpty(s)){

etOne.setError("Empty");
return;
}

etOne.setError(null);

Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);

intent.putExtra("SimpleString",s );

startActivity(intent);



}
});


btnSimpleInt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s = etTwo.getText().toString();
if(TextUtils.isEmpty(s)){
etTwo.setError("Empty");
return;
}

etTwo.setError(null);


int p = -0;

try {
p = Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}

if(p==-0){

Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;

}

Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);
intent.putExtra("SimpleInt", p);
startActivity(intent);


}
});




}





}


Xml Part : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">

<EditText
android:hint="Simple String....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etOne"/>


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleString"
android:text="Button Simple String"/>


<EditText
android:hint="Simple int ....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etTwo"
android:inputType="number"/>


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleInt"
android:text="Button Simple Integer"/>



</LinearLayout>



Java Part 2 : 

package first.learn.datapassingtwo;

import androidx.appcompat.app.AppCompatActivity;

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

public class DataGettingActivity extends AppCompatActivity {


TextView tvShow;

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

tvShow = (TextView) findViewById(R.id.tvShow);

if(getIntent()!=null){

if(getIntent().getStringExtra("SimpleString")!=null){

String s = getIntent().getStringExtra("SimpleString");

tvShow.setText(s);

}

}



if(getIntent()!=null){

if(getIntent().getIntExtra("SimpleInt", -0)!=-0){

int p = getIntent().getIntExtra("SimpleInt", -25);
tvShow.setText(""+p);

}


}



}



}


Xml Part 2 : 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DataGettingActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<TextView
android:hint="Data....."
android:textSize="30dp"
android:textColor="@color/purple_700"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvShow"
/>




</LinearLayout>






No comments:

Post a Comment