This is my main class that will be forwarding the username to my GoalActivity class. I cannot figure out where the issue is. It keeps crashing for an unknown reason to me. I've followed various tutorials, and I cannot figure out the issue. It seems that I retrieve the username correctly, and then convert it into a string. Then create an Intent and pass the username value with a key.
MainActivity.java
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.agray.carpediem.LoginDataBaseAdapter;
import com.example.agray.carpediem.R;
import com.example.agray.carpediem.SignUPActivity;
public class MainActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
//create reference to the buttons used
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Signup button w/onclick listener
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
//start the activity w/intent
startActivity(intentSignUP);
}
});
}
// Methods to handleClick Event of Sign In Button
public void signIn(View View)
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
//get the References of views
final EditText editTextUserName=
(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=
(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
//Signin Button w/ onClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//store username and password as strings
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
//fetch the Password from the DB for respective username
String storedPassword=loginDataBaseAdapter.getSingleEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this, "Congrats: Login is Successful " + userName,
Toast.LENGTH_LONG).show();
dialog.dismiss();
// final EditText editTextUserName=
// (EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
// String userName=editTextUserName.getText().toString();
//create intent that will start the goals activity w/some data
Intent intro = new Intent(getApplicationContext(), GoalActivity.class);
//put the username into intent
intro.putExtra("USER_NAME", userName);
startActivity(intro);
}
else
{
Toast.makeText(MainActivity.this, "Access Denied: User Name or Password " +
"does not match",
Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
#Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
Here is my GoalActivity class that receives info from the MainActivity class.
GoalActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GoalActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.goals_page);
//get the username from the intent
String enteredUserName = getIntent().getStringExtra("USER_NAME");
final TextView tv = (TextView)findViewById(R.id.user_name_forwarded);
tv.setText(enteredUserName);
}
}
login.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" >
<EditText
android:id="#+id/editTextUserNameToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextPasswordToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="Password" />
<Button
android:id="#+id/buttonSignIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sign In" />
</LinearLayout>
goals_page.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"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_goals"
android:textSize="50sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/user_name_forwarded"
android:text="#string/emptyString"
android:layout_weight="0.09"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agray.carpediem" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="CarpeD"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.agray.carpediem.MainActivity"
android:label="CarpeD" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignUPActivity"/>
<activity android:name=".GoalActivity"/>
</application>
</manifest>
Try this, e.g. in ActivityA:
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("USER_NAME", userNameString);
startActivity(i);
In ActivityB:
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String USERNAME = extras.getString("USER_NAME");
Solved. It was a very careless mistake. I had updated all the other activities in the Manifest but the activity tag for the GoalActivity.java. Thanks guys for all your help!
Related
I am a noob at this, so going to keep it short.
This is a practice app which sends data from first activity to second activity as a message and then the first activity receives data from the second activity as a message. The tutorial I'm following instructed me to use the StartActivityForResult() function to extract data from a second activity to the first activity. I have two questions:
What is wrong with this code:
I. The MainActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
//LOG_TAG contains the name of the class package, encapsulated for ease
public static final String EXTRA_MESSAGE =
"com.example.android.twoactivities.extra.MESSAGE";
//This will be used as the unique key to send data to the second Activity
public static final int TEXT_REQUEST = 1;
//This is used to define the key for a particular type of response that
you're interested in.
private EditText mMessageEditText;
//This EditText is used to send the message to the second activity
private TextView mReplyHeadTextView;
private TextView mReplyTextView;
//These private variables hold the reply header and the reply TextViews
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageEditText = (EditText) findViewById(R.id.editText_main);
mReplyHeadTextView = (TextView) findViewById(R.id.textHeaderReply);
mReplyTextView = (TextView) findViewById(R.id.textMessageReply);
}
public void launchSecondActivity(View view) {
Log.d(LOG_TAG, "Button Clicked!");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
//Gets the data from EditText, converts it to String, and stores it in message
intent.putExtra(EXTRA_MESSAGE,message);
startActivityForResult(intent, TEXT_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);
mReplyHeadTextView.setVisibility(View.VISIBLE);
mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
}
}
}
}
II. The SecondActivity.java file
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
//Gets the intent that activated this activity
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//This extracts the extra text sent along with this key String, thus
receiving the text
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
//Do not reuse the intent from the first activity, create a new one
Intent replyIntent = new Intent();
setResult(RESULT_OK,replyIntent);
//RESULT_OK has the value -1 and is used as a Result code in the Activity
class
//to check that the data is send without a complication
finish();
}
public void returnReply(View view) {
}
}
III. The activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="#id/button_main"
android:layout_toLeftOf="#+id/button_main"
android:hint="Enter Your Message Here"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textHeaderReply"
android:text="#string/textHeaderReply"
android:visibility="invisible"/>
<!--The visibility mode is used to select how the attribute will be before data is passed to it.-->
<!--Invisible means that the attribute will be invisible before data is passed to it, hence not confusing the user-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textMessageReply"
android:layout_below="#+id/textHeaderReply"
android:visibility="invisible"/>
IV. The activity_second.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="#string/sendButton"
android:onClick="launchSecondActivity"/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:onClick="returnReply"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
V. The AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.twoactivitiesredo">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/secondActivity"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.twoactivitiesredo.MainActivity"/>
</activity>
</application>
</manifest>
I am sorry if the code is taking a lot of space, but I really needed help in clarifying my doubts and problems here, about why this code isn't working.
Thanks in advance to all the answers.
Replace your secondActivity.java with this:
package com.example.android.twoactivitiesredo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity{
public static final String EXTRA_REPLY =
"com.example.android.twoactivities.extra.REPLY";
//This tag will be used as a key to send reply to first activity
private EditText mReply;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_message);
textView.setText(message);
mReply = (EditText) findViewById(R.id.replyEditText);
}
public void returnReply(View view) {
Intent replyIntent = new Intent();
String replyMessage = mReply.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, replyMessage);
setResult(RESULT_OK,replyIntent);
finish();
}
}
In activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_header"
android:layout_marginBottom="20dp"
android:text="#string/text_header"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_main"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Reply Button"
android:onClick="returnReply"
/>
<TextView
android:id="#+id/text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_header"
android:layout_margin="10dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/replyEditText"
android:layout_toLeftOf="#+id/button_main"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:hint="#string/replyText"
/>
</RelativeLayout>
I created the simple application App, the user enters his name in EditText and click on "Enter here" button and store his name in String variable that is instance variable then will move to another activity "activity_welcome" and will display his name in TextView
My problem:
when I create an object from Main Class to get the variable name in welcome Class when running this app, the name do not store in TextView that means that
the object main refers to null
My question:
why object main refer to null?
When I make the variable name as a static variable the app will display the name in TextView that mean that object main store the value of name variable
Note:
Main class
package code.app;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Main extends AppCompatActivity {
EditText editText ;
String name ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.et) ;
}
public void Go (View v){
name=editText.getText().toString() ;
Intent intent= new Intent(this,Welcome.class);
startActivity(intent);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ba"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:hint="Enter Your Name"
android:textColorHint="#000"
android:id="#+id/tv1"
android:textColor="#000"
android:background="#ffffff"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="Enter Here"
android:background="#645353"
android:onClick="Go"/>
</LinearLayout>
Welcome Class
package code.app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class Welcome extends AppCompatActivity {
TextView textView ;
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
textView =findViewById(R.id.tv);
Main main =new Main () ;
Toast.makeText(this,main.name, Toast.LENGTH_SHORT).show(); // this Toast donot display any thing
textView.setText(main.name);
}
}
activity_welcome
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:layout_margin="50dp"
/>
</LinearLayout>
Just keep it simple and put string inside your intent when you go to another activity, obtain intent, and then string from the intent.
public void Go (View v){
name=editText.getText().toString() ;
Intent intent= new Intent(this,Welcome.class);
intent.putExtra("name", name);
startActivity(intent);
}
Obtain it in your welcome activity like this.
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
textView =findViewById(R.id.tv);
Intent intent = getIntent();
if(intent!=null) {
String name = intent.getStringExtra("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
textView.setText(name);
}
}
I've created two activities. The first accepts the name of a student through an edit text and has a submit button. When the submit button is pressed, the next activity opens which has an edit text (to accept the marks) along with a back button.
I wish to return the marks entered to the first activity, when the back button of second activity is pressed so that the marks are displayed in the first edit text.
Kindly provide me with the code for the two activities.
You can use activity for result . An example is given here
Hello Akanksha Gahalout, if i understand your question, you want to pass Data between two activities and vice versa , if so, you should use Intent and use PutExtrat() method to pass data from one Activity to another,then you can recuperate data from the second Activity with using Bundle and getExtras() method.
Here's an example :
FirstActivity.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="#+id/EditTextFirstActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/StudentName"
android:ellipsize="start"
android:gravity="center_horizontal"
android:labelFor="#+id/EditText1" />
<Button
android:id="#+id/buttonFirstActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/OK" />
<TextView
android:id="#+id/TextViewFirstActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:ellipsize="start"
android:gravity="center_horizontal"
android:textSize="20sp" />
</LinearLayout>
FirstActivity.java :
package dz.A;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity implements OnClickListener {
private TextView textView;
private EditText editTextFirstActivity;
private Button buttonFirstActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
editTextFirstActivity = (EditText) findViewById(R.id.EditTextFirstActivity);
textView = (TextView) findViewById(R.id.TextViewFirstActivity);
buttonFirstActivity = (Button) findViewById(R.id.buttonFirstActivity);
buttonFirstActivity.setOnClickListener(this);
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String marks = (String) b.get("STUDENT_MARKS");
textView.setText(marks);
}
}
#Override
public void onClick(View v) {
if (v == buttonFirstActivity) {
Log.i("OK", "onClickOK");
Intent i = new Intent(this, SecondActivity.class);
Log.i("OK", "IntentOK");
Log.i("OK", "startActivity(i)OK");
String studentName = editTextFirstActivity.getText().toString()
.trim();
Log.i("OK", "studentNameOK : " + studentName);
i.putExtra("STUDENT_NAME", "Student Name : " + studentName);
Log.i("OK", "i.putExtraOK");
startActivity(i);
finish();
Log.i("OK", "finish()OK");
}
}
}
SecondActivity.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" >
<EditText
android:id="#+id/EditTextSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="#string/Marks"
android:inputType="number"
android:labelFor="#+id/EditText1" />
<Button
android:id="#+id/buttonSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sendMarks" />
<TextView
android:id="#+id/TextViewSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
SecondActivity.java :
package dz.A;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends Activity implements OnClickListener {
private TextView textViewSecondActivity;
private EditText editTextSecondActivity;
private Button buttonSecondActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
textViewSecondActivity = (TextView) findViewById(R.id.TextViewSecondActivity);
editTextSecondActivity = (EditText) findViewById(R.id.EditTextSecondActivity);
buttonSecondActivity = (Button) findViewById(R.id.buttonSecondActivity);
buttonSecondActivity.setOnClickListener(this);
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String studentName = (String) b.get("STUDENT_NAME");
textViewSecondActivity.setText(studentName);
}
}
#Override
public void onClick(View v) {
if (v == buttonSecondActivity) {
String marks = editTextSecondActivity.getText().toString().trim();
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("STUDENT_MARKS", "Student Marks : " + marks);
startActivity(i);
finish();
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dz.A"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="dz.A.FirstActivity"
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="dz.A.SecondActivity"/>
</application>
</manifest>
String.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Project</string>
<string name="hello_world">Hello world!</string>
<string name="OK">OK</string>
<string name="Marks">Marks</string>
<string name="sendMarks">sendMarks</string>
<string name="StudentName">StudentName</string>
</resources>
I hope that helps you.
I am basically trying to create an application which consists of 2 activities.
In activity 1 , user can enter 2 values in 2 separate edit text fields and I have provided a spinner drop down item , using which user can select either of 2 values.
If user selects option as 1 and clicks on Submit button , user will be displayed a second activty with value displayed as per the id selected in spinner object in previous activity.
Like if user selects 1 , value in first edit text box gets shown and if he selects 2 , then second edit text box value is displayed.
Also, when user clicks on submit button in second activity , based on value he enters in edit text field of second activity , it will get displayed in either edit text box 1 or edit text box 2 of previous activity .
I am having issues understanding why my app is showing bizarre behaviour. Please guide me.
FIRST ACTIVITY FILE:
package spinnner.intents.understand;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class TwoAppsActivity extends Activity {
/** Called when the activity is first created. */
String [] items = {"1","2"};
//String ch;
TextView tv;
EditText et1,et2;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.txt);
et1=(EditText) findViewById(R.id.edittxt1); // value 1
et2=(EditText) findViewById(R.id.edittxt2); // value 2
spinner=(Spinner) findViewById(R.id.spin1); // select
//
Intent i3 = getIntent();
if(i3.getStringExtra("spinner.intents.understand.value1")!=null)
{
et1.setText(i3.getStringExtra("spinner.intents.understand.value1"));
}
else if(i3.getStringExtra("spinner.intents.understand.value2")!=null)
{
et2.setText(i3.getStringExtra("spinner.intents.understand.value2"));
}
else
{
Toast.makeText(getApplicationContext(), "Enter some text ", Toast.LENGTH_LONG).show();
}
// spinner logic
ArrayAdapter<String> as1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
as1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(as1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
tv.setText(items[arg2]);
Toast.makeText(getApplicationContext(), " You selected : "+items[arg2],Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You selected nothing", Toast.LENGTH_LONG).show();
}
});
//
}
public void Submit(View v)
{
Intent i = new Intent(this,Second.class);
startActivity(i);
//str1 = et1.getText().toString();
//Toast.makeText(getApplicationContext(), str1, Toast.LENGTH_LONG).show();
// str2 = et2.getText().toString();
//Toast.makeText(getApplicationContext(), str2, Toast.LENGTH_LONG).show();
//
if(spinner.getSelectedItem().toString()=="1")
{
Toast.makeText(getApplicationContext(), "I am in 1st option", Toast.LENGTH_LONG).show();
i.putExtra("spinner.intents.understand.value1", et1.getEditableText().toString());
i.putExtra("spinner.intents.understand.Id1", "1");
}
else if(spinner.getSelectedItem().toString()=="2")
{
Toast.makeText(getApplicationContext(), "I am in 2nd option", Toast.LENGTH_LONG).show();
i.putExtra("spinner.intents.understand.value2", et2.getEditableText().toString());
i.putExtra("spinner.intents.understand.Id2", "2");
}
else
{
Toast.makeText(getApplicationContext(), "You selected nothing!!!", Toast.LENGTH_LONG).show();
}
}
}
SECOND ACTIVITY FILE
package spinnner.intents.understand;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Second extends Activity{
/** Called when the activity is first created. */
TextView Tv;
EditText Et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//
Tv=(TextView) findViewById(R.id.txtS1);
Et=(EditText) findViewById(R.id.edittxtS1);
//
Intent i1 = getIntent();
if((i1.getStringExtra("spinner.intents.understand.Id1"))=="1")
{
Toast.makeText(getApplicationContext(), "I am in value 1", Toast.LENGTH_LONG).show();
Et.setText(i1.getStringExtra("spinner.intents.understand.value1"));
}
else if((i1.getStringExtra("spinner.intents.understand.Id2"))=="2")
{
Toast.makeText(getApplicationContext(), "I am in value 2", Toast.LENGTH_LONG).show();
Et.setText(i1.getStringExtra("spinner.intents.understand.value2"));
}
else
{
Toast.makeText(getApplicationContext(), "Some mismatch", Toast.LENGTH_SHORT).show();
}
}
public void submit(View v)
{
Intent i2 = new Intent(this,TwoAppsActivity.class);
startActivity(i2);
if(i2.getStringExtra("Id1")=="1")
{
i2.putExtra("spinner.intents.understand.value1", Et.getEditableText().toString());
}
else if(i2.getStringExtra("Id2")=="2")
{
i2.putExtra("spinner.intents.understand.value2", Et.getEditableText().toString());
}
else
{
Toast.makeText(getApplicationContext(), "No data to display", Toast.LENGTH_LONG).show();
}
}
}
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" >
<TextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<TextView
android:id="#+id/txt1"
android:text="Enter value 1:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edittxt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt2"
android:text="Enter value 2:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edittxt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt3"
android:text="Select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/spin1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnsubmit1"
android:text="Submit"
android:onClick="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
SECOND ACTIVITY XML FILE
<?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:id="#+id/txtS1"
android:text="Value:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/edittxtS1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btnsubmit2"
android:text="Submit"
android:onClick="submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
ANDROID MANIFEST XML FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="spinnner.intents.understand"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwoAppsActivity"
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=".Second">
</activity>
</application>
</manifest>
I am also willing to mail my project if my code appears too messed up.
You shouldn't use == nor != to compare two strings. Use equals instead:
if(spinner.getSelectedItem().toString().equals("1"))
Regards.
users have an activity with an edittext and a button.
in the edittext they have to insert their login ID. and press go to display a other activity with webview loading the right site.
an example. this is the link i want users to visit . www.example.com/time/id=1/type=student
if sombody enters a 2 in the edittext it wil open the times for student id 2 and so on.
this is what i have.
LOGIN
package com.beter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class LoginActivity extends Activity {
Button go_btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
go_btn = (Button) findViewById(R.id.go_btn);
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
and the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/idfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/id"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/idfield"
android:text="#string/login" />
The Webview Activity
package com.beter;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://roosters.gepro-osi.nl/roosters/rooster.php?wijzigingen=1&leerling=116277&type=Leerlingrooster&afdeling=12-13_OVERIG&school=905");
}
}
and again the layout
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How do i make the user input. display the good webview. ?
First of all get a reference to your edittext in your Login Activity as
go_btn = (Button) findViewById(R.id.go_btn);
ed_txt = (EditText)findViewById(R.id.youredittextid);
The change your onClick to
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText());
LoginActivity.this.startActivity(myIntent);
}
Then retrive this id in your WebActivity onCreate as
String id = getIntent().getExtras().getString("id");
Then your url will be
String myURL = "www.example.com/time/id="+id+"/type=student";