Java Part One :
package first.learn.passingdatabetweenactivities;
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;
public class MainActivity extends AppCompatActivity {
EditText etName;
Button btnSimpleString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
btnSimpleString = (Button) findViewById(R.id.btnSimpleString);
btnSimpleString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = etName.getText().toString();
if(TextUtils.isEmpty(s)){
etName.setError("Empty");
return;
}
etName.setError(null);
Intent intent = new Intent(MainActivity.this, DataGettingActivity.class );
intent.putExtra("StringPacket", s );
startActivity(intent);
}
});
}
}
xml part One :
<?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="write...."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etName"/>
<Button
android:id="@+id/btnSimpleString"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simple String"/>
</LinearLayout>
Java Part Two :
package first.learn.passingdatabetweenactivities;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DataGettingActivity extends AppCompatActivity {
TextView tvData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_getting);
setTitle("Data Getting Activity");
tvData = (TextView) findViewById(R.id.tvData);
if(getIntent()!=null){
if(getIntent().getStringExtra("StringPacket")!=null){
String gettingString = getIntent().getStringExtra("StringPacket");
tvData.setText(gettingString);
}
}
}
}
Xml Part Two :
<?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"
android:orientation="vertical"
tools:context=".DataGettingActivity"
android:layout_margin="16dp">
<TextView
android:id="@+id/tvData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Text"
android:gravity="center"
android:layout_marginTop="25dp"/>
</LinearLayout>
No comments:
Post a Comment