Q3
Question 3
Last updated
Question 3
Last updated
App Insights:
Made layout for default main and secondary activity and added onCreate method, used EditText field and TextView to display the result using Explicit Intent.
<?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:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_weight="10"
android:ems="10"
android:inputType="textPersonName"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:text="Enter any String" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendtext"
android:text="send"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".SecondaryActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="376dp"
android:text="@string/textview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<resources>
<string name="app_name">Q3_Explicit_Intent</string>
<string name="textview">TextView</string>
</resources>
package com.example.q3_explicit_intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendtext(View v) {
EditText edit =(EditText)findViewById(R.id.editText);
String str = edit.getText().toString();
Intent i = new Intent(this,SecondaryActivity.class);
i.putExtra("abc",str);
startActivity(i);
}
}
package com.example.q3_explicit_intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondaryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
TextView text =(TextView)findViewById(R.id.textView);
Intent intent = getIntent();
String str = intent.getStringExtra("abc");
text.setText(str);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.q3_explicit_intent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Q3_Explicit_Intent">
<activity android:name=".SecondaryActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"/>
<category
android:
name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Download to Run and Test Locally