Android passing of string to another activity - java

Can I ask if someone knows how to pass the data entered from one activity to another activity? The string to be passed to that activity will be compared to the images in the database of the Android phone..
Here is the code sirs:
AlphabetConversion.java
package com.mobilebasedsignlanguage;
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 AlphabetConversionMenu extends Activity {
EditText et_conv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphaconv);
Button alphaConv = (Button)findViewById(R.id.btn_alphaconv);
et_conv = (EditText)findViewById(R.id.et_convtxt);
alphaConv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String checkWord = et_conv.getText().toString();
Bundle word = new Bundle();
word.putString("key", checkWord);
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras(word);
startActivity(a);
}
});
}
}
Here is the java for the second activity
package com.mobilebasedsignlanguage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AlphabetCompareClass extends Activity {
String get;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabetcompare);
Bundle gotWord = getIntent().getExtras();
get = gotWord.getString("key");
TextView Word = (TextView)findViewById(R.id.textView1);
Word.setText(get);
for(int x = 0; x > gotWord.getString("key").length(); x++){
//if(gotWord.getString("key").charAt(0) == "SELECT * from " + TABLE_CONTACTS)
// Get Image from the Database
// Set it on Image View
// set timer 2 secs
}
};
}
The comments in the second activity I think I don't know how to code that. Really need someones help. Thank you very much.

Did you try to save information on the shared preferences?
Allows you you to save "key-value".
http://developer.android.com/reference/android/content/SharedPreferences.html

try this on AlphabetConversion.java
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String checkWord = et_conv.getText().toString();
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtra("key",checkWord);
a.putExtras(word);
startActivity(a);
}
and in the onCreate of second activity
Bundle b;
b = this.getIntent().getExtras();
mssg = b.getString("key");

Select Statement will return the resultset object. If you want to compare it to the specific column then fetch the column name only. For comparing the string objects you can use String.compare method or simple == will be sufficient.

First, you should put it into extras in the first activity:
Intent intent = new Intent(this, AlphabetCompareClass.class);
intent.putExtra("STRING_KEY", yourString);
In the second activity you can take it from extras:
String yourString = getIntent().getExtras().getString("STRING_KEY");
Or you can use shared preferences to store information. It's more convenient if you should save info for more than one activity change.

In your first activity call like this :
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras("key", checkWord);
startActivity(a);
And in second activity get value like this
get = getIntent().getStringExtra("key",""); // here if no extra found then default "" return.

Related

Data passing between two activities repetitively

I'm a beginner in android studio and i want to write a code with 3 activities.
1st is for starting the app.2nd is for showing an English word and 3rd is for showing the English word and a description of it in two texts.
I want to transfer data of English word itself and its description and the number of the word to show the words one by one.
I wrote the code with help of tutorial clips but it ain't work and in 3rd activity shows nothing.
these are my codes:
main activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view)
{
Intent i = new Intent(this,ActivityOne.class);
startActivity(i);
}
}
ActivityOne
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityOne extends AppCompatActivity {
int a=0;
String E , P;
private Button show;
private TextView Eword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Eword = (TextView) findViewById(R.id.Eword);
show = (Button) findViewById(R.id.show);
}
public void show(View view)
{
Intent intent = new Intent(this,ActivityTwo.class);
startActivity(intent);
int a = getIntent().getIntExtra("counter",0);
Eword.setText(Eng[a]);
E = Eword.getText().toString();
P = Fa[a];
a++;
intent.putExtra("counter",a);
intent.putExtra("EWord",E);
intent.putExtra("PWord",P);
}
private String[] Eng = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
private String[] Fa = {
"Abundance Description",
"Anxiety Description",
"Bruxism Description",
"Discipline Description",
"Drug Addiction Description"
};
}
ActivityTwo
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
TextView Eng , Fa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Eng = (TextView) findViewById(R.id.eng);
Fa = (TextView) findViewById(R.id.fa);
// recieve data from activity one
String EWord = getIntent().getStringExtra("EWord");
String PWord = getIntent().getStringExtra("PWord");
int a = getIntent().getIntExtra("counter",0);
Eng.setText(EWord);
Fa.setText(PWord);
}
public void iknow(View view)
{
Intent myIntent = new Intent(this,ActivityOne.class);
startActivity(myIntent);
int a = getIntent().getIntExtra("counter",0);
myIntent.putExtra("counter",a);
}
public void idknow(View view)
{
Intent myIntentTwo = new Intent(this,ActivityOne.class);
startActivity(myIntentTwo);
int a = getIntent().getIntExtra("counter",0);
myIntentTwo.putExtra("counter",a);
}
}
And it shows this result:
It is the third activity and it can clear the pretext that was set in design page but can not replace EWord or PWord
Can someone help me?????
(1) To get you started, you need to put your variables into the intent before you start the activity. (2). I don't think its a good idea to have activity one start two, which then can start one. It is better to just close second activity which then automatically returns to one. (3) To pass data back to the activity you are returning you need to startActivityForResult https://developer.android.com/training/basics/intents/result.html
activity one starts activity two using startActivityForResult. Activity Two returns an intent (which is the result intent) and activity one recieves this in onActivityResult.
check out this: Android: how to make an activity return results to the activity which calls it?

Android Studio: playing with Intents and sending data between activities

I'm new to Android Studio and Java, currently I'm learning about intents and sending data/text from one activity to another. On the second activity I get "Cannot resolve symbol 'intent'" even though it's in onCreate field. What I'm trying to do is to have two text fields for first and last name, sending the text from first activity to the second one which will read it, and all done with onButtonClick method. I'm only trying to do the first name and the code looks like this MainAcitivty
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClicked(View view) {
EditText nameText = (EditText)findViewById(R.id.firstName);
TextView messageText = (TextView) findViewById(R.id.messageTextView);
if(nameText.getText().toString().trim().length() == 0){
messageText.setText("text here");
} else {
String textForSecondAcitivity = "your name is: " + nameText.getText();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
startActivity(intent);
`
The problem is the second Activity that gives me the error "Cannot resolve symbol 'intent'". And here is the code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent scIntent = getIntent();
String messageText = intent.getStringExtra("EXTRA_TEXT");
TextView messageView = (TextView)findViewById(R.id.textView);
messageView.setText(messageText);
}
It wont show the error here but it's int this line
String messageText = intent.getStringExtra("EXTRA_TEXT");
I apologize in advance because surely I messed up something bad, I'm a beginner and if you have some advice on what's the best way to do what I'm actually trying to accomplish feel free to tell me.
The app should look like this in the end 1.The first activity
1.The second one showing first name and last name
You have no local variable named intent
Replace it with scIntent
Here you are making it wrong
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
instead you use this
intent.putExtra("EXTRA_TEXT", textForSecondAcitivity);

Sugar ORM how do i add data to a row in the table

Good day all i am using android studio, I have a fairly good idea of how to use sugar ORM at this point, but there's one thing that I just cant find or figure out.
So i have an activity named UserProfiles.java in which i ask the user for various things and then i store them in the database. So at this point i have about 20 records/rows in the database. The problem however is when i send the user to the next activity (UserPins.java) i ask them to set some additional information now how do i add this information to the same line as the data that has the User Profiles information?
//this is my UserProfiles.java
package com.chika.mia;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.chika.mia.models.Mia;
public class UserProfile extends AppCompatActivity implements View.OnClickListener {
private EditText Name, PhoneNumber, Email, Password, Address;
private Button SaveUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Name = (EditText)findViewById(R.id.Name);
PhoneNumber = (EditText)findViewById(R.id.PhoneNumber);
Email = (EditText)findViewById(R.id.Email);
Password = (EditText)findViewById(R.id.Password);
Address = (EditText)findViewById(R.id.Address);
SaveUser = (Button)findViewById(R.id.SaveUser);
SaveUser.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Mia m = new Mia();
m.setName(register.g);
m.setPhoneNumber(Integer.parseInt(PhoneNumber.getText().toString())) ;
m.setEmail(register.i) ;
m.setPassword(register.h);
m.setAddress(Address.getText().toString()) ;
m.setLocation("MiaL");
m.setWipePhone("MiaWP");
m.setDispMessage("MiaDM");
m.setAlarm("MIAA");
m.setShutdown("MIASD");
m.save();
Toast.makeText(this, " " + m.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(UserProfile.this, UserPins.class);
startActivity(intent);
}
}
And this is my Userpins.java
package com.chika.mia;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.chika.mia.models.Mia;
import com.orm.query.Condition;
import com.orm.query.Select;
public class UserPins extends AppCompatActivity implements View.OnClickListener {
private EditText UserLocation,UserWipePhone,UserDisplayMsg,UserAlarm,UserShurdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_pins);
UserLocation = (EditText)findViewById(R.id.UserLocation);
UserWipePhone = (EditText)findViewById(R.id.UserWipePhone);
UserDisplayMsg = (EditText)findViewById(R.id.UserDisplayMsg);
UserAlarm = (EditText)findViewById(R.id.UserAlarm);
UserShurdown = (EditText)findViewById(R.id.UserShutdown);
}
#Override
public void onClick(View v) {
Mia m = new Mia();
Select.from(Mia.class).where(Condition.prop("Name").eq(register.i)).list();
m.setLocation(UserLocation.getText().toString());
m.setWipePhone(UserWipePhone.getText().toString());
m.setDispMessage(UserDisplayMsg.getText().toString());
m.setAlarm(UserAlarm.getText().toString());
m.setShutdown(UserShurdown.getText().toString());
m.update();
Intent intent = new Intent(this,AppSettings.class);
startActivity(intent);
}
}
What am aiming to do is something like this, So for which ever user logs in say "Tom", i already have the login and registration page sorted out. It will go through the database and look for the name "Tom" or email "tom#gmail.com" and then add the userpins information to the appropriate column in that table.
When you create a new record in Sugar ORM, you get a unique ID back from the save(). On your first activity, get the ID, and pass it to the next activity:
long new_user_id = m.save();
...
Intent intent = new Intent(UserProfile.this, UserPins.class);
intent.putExtra("NEW_USER_ID", new_user);
startActivity(intent);
Then you can grab it in the new activity with:
long new_user_id = getIntent().getLongExtra("NEW_USER_ID",0);
You may then use the ID to get the record to update:
Mia m = Mia.findById(Mia.class, new_user_id);

Cannot set Gmail 'subject' and 'text' fields from an intent

I am calling an intent that sets the 'To' field no problem of a GMail template from a String array, but I cannot get it to set the 'subject' and 'text' fields.
Hopefully someone can see where I'm going wrong
Here's my code:
package com.example.flybase2;
import android.app.Activity;
import android.app.Dialog;
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;
import android.widget.TextView;
public class ContactsEmail extends Activity implements OnClickListener{
String emailPassed;
String emailAdd;
String emailSub;
String emailMess;
EditText setEmailAddress;
EditText setEmailSubject;
EditText setEmailMessage;
Button btnSendEmail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emaillayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
emailPassed = extras.getString("passedEmailAdd");
}
setEmailAddress = (EditText) findViewById (R.id.inputEmailAddress);
setEmailAddress.setText(emailPassed);
setEmailSubject = (EditText) findViewById (R.id.inputEmailSubject);
setEmailMessage = (EditText) findViewById (R.id.inputEmailMessage);
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
btnSendEmail = (Button)findViewById(R.id.btnSendEmail);
btnSendEmail.setOnClickListener(this);
}
#Override
public void onClick(View sendEmailClick) {
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
}
Try this.
Actually When activity create you get the value of these editext. At that time value of these all are empty. so you need to write these in onclick method.
When you click on button then you will get the current edit text value.
I hope this will solve your problem.
#Override
public void onClick(View sendEmailClick) {
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
and remove these three lines from onCreate() method.
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();

I want to make password protected android application

I want to make password protected android app, but when in this simple program system is not matching two strings.
package com.pokmgr;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainPM extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pm_layout);
final EditText pin = (EditText) findViewById(R.id.pinET);
final String pass = pin.getText().toString();
final String code = "ajaj";
Button enter = (Button) findViewById(R.id.enterBtn);
enter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (pass.equals(code)) {
Intent in = new Intent(MainPM.this, Menu.class);
startActivity(in);
}
else {
Intent in = new Intent(MainPM.this, Menu2.class);
startActivity(in);
}
}
});
}
/*#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.pm_layout, menu);
return true;
}*/
}
I have made Menu and Menu2 class, but every time Menu2 class is accessed. Even if I enter same pass that is "ajaj" [in this code to test]
i have defined both activities in manifest file.
Can't understand why pass.eqals(code) is not working
The problem is that you are setting pass to the contents of the EditText when the activity gets created. Instead you have to retrieve the contents of your EditText inside the OnClickListener.
Like this:
public void onClick(View v) {
final String pass = pin.getText().toString();
if (pass.equals(code)) {
// do something
} else {
// do something different
}
}
Put pin.getText().toString(); inside onClick of button. You are setting variable pass before the user actually entered something in pinEt EditText.

Categories