Put EditText in First Activity to TextView on Second Activity - android

I'm french so sorry for my bad english.
I need to update on my second activity my TextView with a EditText of my first Activity. But I don't how to do.
That is my code on First Activity:
public class MainActivity extends AppCompatActivity {
private TextView mGreetingTextView;
private EditText mLoginEditText,mEmailEditText,mPasswordEditText;
private Button mLoginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGreetingTextView = findViewById(R.id.main_textview_info);
mLoginEditText = findViewById(R.id.main_edittext_login);
mEmailEditText = findViewById(R.id.main_edittext_email);
mPasswordEditText = findViewById(R.id.main_edittext_password);
mLoginButton = findViewById(R.id.main_button_log);
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validateLogin();
}
String login = mLoginEditText.getText().toString();
});
}
private void validateLogin(){
if (mEmailEditText.getText().toString().equals("admin#admin.com") &&
mPasswordEditText.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Login successful",Toast.LENGTH_SHORT).show();
callHome();
} else {
Toast.makeText(getApplicationContext(), "Wrong login",Toast.LENGTH_SHORT).show();
}
}
public void callHome(){
Intent i = new Intent(getApplicationContext(),HomeActivity.class);
i.putExtra("mLoginText", mLoginEditText.getText());
startActivity(i);
}
But I search a same topic who has the same problem and I don't find.
Apologize for my bad level on Android but I'm student on Android. This is my first topic on StackOverFlow ^^

You are calling mLoginEditText.getText() and that returns Editable. You will need to call mLoginEditText.getText().toString() and that will be a String.

You can update your second activity (HomeActivity) like this
public class HomeActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = findViewById(R.id.textView);
// get the text from main activity
Intent intent = getIntent();
String text = intent.getStringExtra("mLoginText");
textView.setText(text);
}}

I found a result :)
MainActivity:
public class MainActivity extends Activity {
public final static String LOGIN_DATA = "mLoginText";
private EditText mLoginEditText,mEmailEditText,mPasswordEditText;
private Button mLoginButton;
private boolean isValid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLoginEditText = findViewById(R.id.main_edittext_login);
mEmailEditText = findViewById(R.id.main_edittext_email);
mPasswordEditText = findViewById(R.id.main_edittext_password);
mLoginButton = findViewById(R.id.main_button_log);
mLoginButton.setOnClickListener(v -> {
isValid = validateLogin();
if (isValid){
callHome();
}
});
}
private boolean validateLogin(){
if (mEmailEditText.getText().toString().equals("admin#admin.com") &&
mPasswordEditText.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Login successful",Toast.LENGTH_SHORT).show();
return true;
} else {
Toast.makeText(getApplicationContext(), "Wrong login",Toast.LENGTH_SHORT).show();
return false;
}
}
public void callHome(){
Intent i = new Intent(MainActivity.this,HomeActivity.class);
i.putExtra(LOGIN_DATA, mLoginEditText.getText().toString());
startActivity(i);
}
}
HomeActvity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mHelloText = findViewById(R.id.home_textview_hello);
Intent intent = getIntent();
String login = intent.getStringExtra(MainActivity.LOGIN_DATA);
mHelloText.setText(login);
With that, the login name of the first activity is in the second activity :)

Related

How to read a value from a String from a java file to another file?

I have a String value which is the text from an EditText and I want to use that String value in another activity.
In a TextView the user puts his/her name and in the next activity I want to have a Welcome screen that says, Hello, name
package com.example.aprendelastablasdemultiplicar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private TextView cifracero;
private TextView cifrauno;
ingresarnombre = (EditText)findViewById(R.id.ingresarnombre);
String nombre = ingresarnombre.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero.setText(0);
cifrauno.setText(1);
}
}
In your first activity where you will put the name on the edittext, just get the string from the Edittext and pass that with intent.
FirstActivity :
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText)findViewById(R.id.edittext);
String name = edittext.getText().toString();
Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
intent.putExtra("name_extra",name);
startActivity(intent);
}
}
On your second activity just receive the string extra value from intent and work with it.
Second Activity :
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}
Create a button in your pantalla2 so that on button click you can go to another activity.And make sure you have given the id of your textview and button in activity_pantalla2 See this it will work:
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private Button button;
private TextView cifracero;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero = findViewById(R.id.txtCifracero);
button = findViewById(R.id.button);
String cifracero = cifracero.getText().toString().trim();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("key", cifracero);
startActivity(intent);
}
});
}
}
In your SecondActivity you just have to add textview in your layout and get the value send from first activity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}

Button visibility according to actvities

I have three activities A.B and C. I have a button in activity C. My requirement is that the button should only visible when going from activity B to C. The button should be invisble when going from A to C. Please help me.
Activity A
public class A extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(A.this, C.class);
intent.putExtra(KEY_EXTRA, FROM_A);
startActivity(intent);
}
});
}
}
Activity B
public class B extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(B.this, C.class);
intent.putExtra(KEY_EXTRA, FROM_B);
startActivity(intent);
}
});
}
}
Activity C
public class C extends AppCompatActivity {
Button button;
public static int FROM_A = 1;
public static int FROM_B = 2;
public static String KEY_EXTRA = "KEY_EXTRA";
int activityStartedFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
// TODO: 5/5/2018 consider findViewById
activityStartedFrom = getIntent().getIntExtra(KEY_EXTRA, FROM_B);
button.setVisibility(activityStartedFrom == FROM_B ? View.VISIBLE : View.GONE);
}
}

Android:Error:(20, 51) error: cannot find symbol variable Message_Text

I am trying to send data through an activity to another but there is a variable error that I didn't understand, please Help. Thank you very much for your time and assistance in this matter.
public class MainActivity extends AppCompatActivity {
EditText Message_Text;
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View views) {
Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
the second activity code is:
public class SecondActivity extends AppCompatActivity {
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
// setContentView(R.layout.second_layout);
}
}
you have to write like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessage();
}
public void sendMessage() {
EditText Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("MESSAGE_KEY",message);
startActivity(intent);
}

android if-statement doesn't work

public class MainActivity extends Activity
{
public static String EXTRA_MESSAGE;
private Intent ceec;
private EditText cc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
public void sendMessage(View view)
{
ceec = new Intent(this, ToActivity.class);
cc = (EditText) findViewById(R.id.edit_message);
String message = cc.getText().toString();
ceec.putExtra(EXTRA_MESSAGE, message);
startActivity(ceec);
}}
And
public class ToActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
textView.setTextColor(Color.rgb(5,8,100));
if( message == "hi"){
textView.setTextSize(80);
}
// Set the text view as the activity layout
setContentView(textView);
}}
[ if( message == "hi"){
textView.setTextSize(80);
} ]
It didn't work why? And how to fix it and thank you
Instead of
message == "hi"
You should do:
message.equal("hi")
Never compare Strings with ==, check out this question to understand why.
Use .equals() method as,
if(message.equals("hi")){
textView.setTextSize(80);
}
else
{
// do your else stuff
}
this should work.

Open new activity after passing argument

Can someone help me? How do I open a new activity after passing basic login argument, here's my code and I don't know what's going, I get an error:
public class MainActivity extends AppCompatActivity {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
#Sheldon Madison : Try this way . Need proper Initialization of Global or local variables .
Please read Official Documents
http://developer.android.com/intl/es/index.html
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
Try
public class MainActivity extends AppCompatActivity {
EditText usern,passw;
String user_name,pass_word;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usern = (EditText)findViewById(R.id.user_name);
passw = (EditText)findViewById(R.id.password);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
user_name = usern.getText().toString();
pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}

Categories

Resources