Open new activity after passing argument - android

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);
}
}
}

Related

Put EditText in First Activity to TextView on Second Activity

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 :)

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);
}

How do I carry an object from one activity to another?

I'm trying to right a messaging application, and I have two separate activities. I have the Connect activity, and the Client activity. I also have a class for the client. The client is used in both the Connect and Client activities, but I don't know how to carry the Client object used in the ConnectActivity to the ClientActivity.
My ConnectActivity:
public class ConnectActivity extends AppCompatActivity {
private Client client;
private EditText txtServerAddress;
private EditText txtPortNumber;
private EditText txtUsername;
private TextView lblErrors;
public ConnectActivity(){}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
final EditText txtServerAddress = (EditText) findViewById(R.id.edit_text_server_address);
this.txtServerAddress = txtServerAddress;
final EditText txtPortNumber = (EditText) findViewById(R.id.edit_text_port_number);
this.txtPortNumber = txtPortNumber;
final EditText txtUsername = (EditText) findViewById(R.id.edit_text_username);
this.txtUsername = txtUsername;
final TextView lblErrors = (TextView) findViewById(R.id.text_view_errors);
this.lblErrors = lblErrors;
Button btnLogIn = (Button) findViewById(R.id.button_log_in);
btnLogIn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String username = txtUsername.getText().toString().trim();
if(username.length() == 0)
return;
String portNumber = txtPortNumber.getText().toString().trim();
if(portNumber.length() == 0)
return;
String serverAddress = txtServerAddress.getText().toString().trim();
if(serverAddress.length() == 0)
return;
int port;
try
{
port = Integer.parseInt(portNumber);
}
catch(Exception en)
{
return;
}
client = new Client(serverAddress, port, username, new ClientActivity(), ConnectActivity.this);
if(!client.start())
return;
Intent intent = new Intent(ConnectActivity.this, ClientActivity.class);
startActivity(intent);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void append(String str)
{
lblErrors.setText(str);
}
}
My ClientActivity:
public class ClientActivity extends AppCompatActivity {
// for I/O
private Client client;
private ListView lstMessages;
private EditText txtMessage;
private Button btnSend;
private ArrayList<ChatMessage> messages;
private ArrayAdapter<ChatMessage> messageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
final ListView lstMessages = (ListView) findViewById(R.id.list_messages);
final EditText txtMessage = (EditText) findViewById(R.id.edit_text_message);
final Button btnSend = (Button) findViewById(R.id.button_send);
btnSend.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String message = txtMessage.getText().toString();
txtMessage.setText("");
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, message));
}
});
messages = new ArrayList<>();
messageAdapter = new ArrayAdapter<ChatMessage>(ClientActivity.this, R.layout.chat_message, messages);
lstMessages.setAdapter(messageAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void connectionFailed()
{
Intent intent = new Intent(this, ConnectActivity.class);
startActivity(intent);
}
public void display(String msg)
{
messageAdapter.add(new ChatMessage(ChatMessage.ERROR, msg));
}
public void append(String str)
{
messageAdapter.add(new ChatMessage(ChatMessage.MESSAGE, str));
}
}
You can use Parcelable to pass an object from one Activity to another.
From your ConnectActivity.java btnLogIn onClick method , parcel Client object:
// Bundle
Bundle bundle = new Bundle();
bundle.putParcelable("CLIENT", client);
// Intent
Intent intent = new Intent(ConnectActivity.this, ClientActivity.class);
intent.putExtras(bundle);
startActivity(intent);
In your ClientActivity.java onCreate() method, get Client object:
// Get data from Intent
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
Client client = bundle.getParcelable("CLIENT");
}
FYI, Make sure your Client class implement Parcelable.
Hope this will help~
You have several options for sharing data between activities:
If the data should be permanent, store it in a database, flat file, or SharedPreferences. Each activity accesses the data directly from the permanent storage.
If the data should not be stored permanently, make your Client class Parcelable and pass it with an Intent. Others have already described this, so I will not go into more details.
Make your Client class implement Parcelable then pass the Client object in the Intent using
Bundle bundle = new Bundle();
bundle.putParcelable("client", client);
intent.putExtra(bundle);
startActivity(intent);
Here is a sample code to implement Parcelable.
You can easly do it by create an instance of Class A in Class B and pass anything you want

non-static method putExtra and cannot find symbol method

I'm a PHP developer but today I need to face the Android Studio.
I want to send an input text to another activity. But I had some errors:
error: non-static method putExtra(String,String) cannot be
referencedfrom a static context error: cannot find symbol method
StartActivity(Intent)
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.proj.proj";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
EditText editText = (EditText) findViewById(R.id.main);
String message = editText.getText().toString();
Intent.putExtra(EXTRA_MESSAGE, message);
StartActivity(intent);
}
}
what is wrong?
Use proper instance of intent not Intent.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
String message = editText.getText().toString();
intent.putExtra("com.proj.proj", 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.

Categories

Resources