Reputation: 651
1.First Activity
:
This Activity
has two EditTexts
and two Buttons
.
I need to give login id and password.Here
the Buttons
contained logging in and cancel:
package com.samelayout;
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 SameLayoutActivity extends Activity {
/** Called when the activity is first created. */
EditText Myusername;
EditText Mypassword;
Button btnlogin;
Button btncancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnlogin=(Button)findViewById(R.id.loginbutton);
btncancel=(Button)findViewById(R.id.cancelbutton);
Myusername=(EditText)findViewById(R.id.username);
Mypassword=(EditText)findViewById(R.id.password);
btnlogin.setOnClickListener(new OnClickListener()
{ public void onClick(View v) {
String str1=Myusername.getText().toString();
String str2=Mypassword.getText().toString();
Intent int1=new Intent(getApplicationContext(),Login.class);
Bundle b= new Bundle();
b.putString("userdata", str1);
b.putString("userdata1", str2);
int1.putExtras(b);
int1.putExtras(b);
//(int1);
startActivityForResult(int1, 0);
}
});
btncancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
2.second Activity
:
This Activity
retrieves the data from the first Activity
. I need to show user details to second xml file
package com.samelayout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Intent int1= getIntent();
Bundle b=this.getIntent().getExtras();
if(b!=null)
{
String str1=b.getString("userdata");
String str2=b.getString("userdata1");
TextView tv1=(TextView)findViewById(R.id.text1);
TextView tv2=(TextView)findViewById(R.id.text2);
tv1.setText(str1);
tv2.setText(str2);
setContentView(tv1);
setContentView(tv2); }
} }
3.main.xml
here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="@android:color/darker_gray" >
<TextView android:layout_width="fill_parent"
android:text="Employee ID:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
<EditText android:hint="enter ur ID"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView android:layout_width="fill_parent"
android:text="Password:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20dip"/>
<EditText android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:password="true"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/loginbutton"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancelbutton"/>
</LinearLayout>
4.needed output can be displayed here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="@android:color/darker_gray" >
<TextView
android:layout_width="fill_parent"
android:id="@+id/text1"
android:text="Employee Name:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:id="@+id/text2"
android:text="Employee password:"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="20sp"/>
</LinearLayout>
5.this is my Logcat getting errors
11-22 12:04:27.298: W/KeyCharacterMap(334): No keyboard for id 0
11-22 12:04:27.298: W/KeyCharacterMap(334): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 12:04:37.738: D/AndroidRuntime(334): Shutting down VM
11-22 12:04:37.738: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 12:04:37.758: E/AndroidRuntime(334): FATAL EXCEPTION: main
11-22 12:04:37.758: E/AndroidRuntime(334): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:41)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.view.View.performClick(View.java:2485)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.view.View$PerformClick.run(View.java:9080)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Handler.handleCallback(Handler.java:587)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
11-22 12:04:37.758: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 12:04:37.758: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:04:37.758: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 12:04:37.758: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 12:04:37.758: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
11-22 12:04:42.079: I/Process(334): Sending signal. PID: 334 SIG: 9
11-22 14:46:39.458: W/KeyCharacterMap(377): No keyboard for id 0
11-22 14:46:39.458: W/KeyCharacterMap(377): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 14:46:42.309: D/AndroidRuntime(377): Shutting down VM
11-22 14:46:42.309: W/dalvikvm(377): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:46:42.329: E/AndroidRuntime(377): FATAL EXCEPTION: main
11-22 14:46:42.329: E/AndroidRuntime(377): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.view.View.performClick(View.java:2485)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.os.Looper.loop(Looper.java:123)
11-22 14:46:42.329: E/AndroidRuntime(377): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:46:42.329: E/AndroidRuntime(377): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:46:42.329: E/AndroidRuntime(377): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:46:42.329: E/AndroidRuntime(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:46:42.329: E/AndroidRuntime(377): at dalvik.system.NativeStart.main(Native Method)
11-22 14:48:22.648: D/AndroidRuntime(411): Shutting down VM
11-22 14:48:22.648: W/dalvikvm(411): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:48:22.657: E/AndroidRuntime(411): FATAL EXCEPTION: main
11-22 14:48:22.657: E/AndroidRuntime(411): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.view.View.performClick(View.java:2485)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.os.Looper.loop(Looper.java:123)
11-22 14:48:22.657: E/AndroidRuntime(411): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:48:22.657: E/AndroidRuntime(411): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:48:22.657: E/AndroidRuntime(411): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:48:22.657: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:48:22.657: E/AndroidRuntime(411): at dalvik.system.NativeStart.main(Native Method)
11-22 14:59:44.668: W/KeyCharacterMap(441): No keyboard for id 0
11-22 14:59:44.668: W/KeyCharacterMap(441): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-22 14:59:53.378: D/AndroidRuntime(441): Shutting down VM
11-22 14:59:53.388: W/dalvikvm(441): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 14:59:53.411: E/AndroidRuntime(441): FATAL EXCEPTION: main
11-22 14:59:53.411: E/AndroidRuntime(441): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.samelayout/com.samelayout.Login}; have you declared this activity in your AndroidManifest.xml?
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.Activity.startActivityForResult(Activity.java:2827)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.samelayout.SameLayoutActivity$1.onClick(SameLayoutActivity.java:38)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.view.View.performClick(View.java:2485)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.view.View$PerformClick.run(View.java:9080)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Handler.handleCallback(Handler.java:587)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.os.Looper.loop(Looper.java:123)
11-22 14:59:53.411: E/AndroidRuntime(441): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 14:59:53.411: E/AndroidRuntime(441): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 14:59:53.411: E/AndroidRuntime(441): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 14:59:53.411: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 14:59:53.411: E/AndroidRuntime(441): at dalvik.system.NativeStart.main(Native Method)
11-22 15:04:53.512: I/Process(441): Sending signal. PID: 441 SIG: 9
6.android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samelayout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".SameLayoutActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 1
Views: 4047
Reputation: 56
You can achieve this easily by data binding by setting
dataBinding {
enabled = true
}
inside your Build.gradle(:app)
Now, to send data from one xml to another, look at the below example. In this, I have made a layout named item_top_card.xml which I want to include it in another xml file with different titles. One way is to make different xml for different titles. A good way is to pass the title string from one xml to another as shown :
<include
android:id="@+id/todays_clicks_parent"
app:option="@{@string/todays_clicks}"
layout="@layout/item_top_card" />
You can include the layout xml which you want to include in other xml file as shown above. Notice that here we are passing the string in "option" parameter. This is the parameter name which can be changed as per your needs. Below is the file for item_top_card.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable name="option" type="java.lang.String"/>
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="120dp"
android:minHeight="120dp"
android:layout_marginLeft="16dp"
android:background="@color/transparent"
app:cardBackgroundColor="@color/white"
app:cardElevation="0.1dp"
app:cardCornerRadius="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingVertical="12dp"
android:paddingLeft="12dp"
android:paddingRight="24dp">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_circle"
android:backgroundTint="@color/purple_light"
android:paddingHorizontal="12dp"
android:paddingVertical="12dp"
android:src="@drawable/clicks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:lineHeight="24dp"
android:text="123"
android:paddingVertical="5dp"
android:paddingLeft="5dp"
android:lineSpacingExtra="5sp"
android:textColor="@color/black"
android:textFontWeight="600"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="@+id/icon"
app:layout_constraintTop_toBottomOf="@+id/icon" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineHeight="20dp"
tools:text="@string/todays_clicks"
android:text="@{option}"
android:paddingLeft="5dp"
android:layout_marginTop="4dp"
android:textColor="@color/light_grey_text_color"
android:textSize="14sp"
android:lineSpacingExtra="3sp"
app:layout_constraintStart_toStartOf="@+id/title"
app:layout_constraintTop_toBottomOf="@+id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
Upvotes: 0
Reputation: 109237
In SameLayoutActivity replace this,
btnlogin.setOnClickListener(new OnClickListener()
{ public void onClick(View v) {
String str1=Myusername.getText().toString();
String str2=Mypassword.getText().toString();
Intent int1=new Intent(SameLayoutActivity.this,Login.class);
Bundle b= new Bundle();
b.putString("userdata", str1);
b.putString("userdata1", str2);
int1.putExtras(b);
//(int1);
startActivityForResult(int1, 0);
}
});
In your Login Activity, Replace this code, And try again and let me know what happen ..
TextView tv1=(TextView)findViewById(R.id.text1);
TextView tv2=(TextView)findViewById(R.id.text2);
if(b!=null)
{
String str1=b.getString("userdata");
String str2=b.getString("userdata1");
tv1.setText(str1);
tv2.setText(str2);
}
}
Manifest file:
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:label="@string/app_name"
android:name=".SameLayoutActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login"> </activity>
</application>
</manifest>
Upvotes: 1
Reputation: 31283
You need to pass your data through the Intent
used to start the 2nd Activity. Check this blog post out.
Upvotes: 1