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.
Related
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 :)
I want to passe the result of the QR code from the ScanCodeActivity to the TextView in other layout called ResultWebService.
#Override
public void handleResult(Result result) {
ResultWebService.resultTExtViewCode.setText(result.getText());
onBackPressed();
}
this is my ResultWebService :
public class ResultWebService extends AppCompatActivity {
public static TextView resultTExtViewCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_web_service);
resultTExtViewCode = (TextView) findViewById(R.id.textViw_QRcode);
}
}
How can i do this please ?
You can do that, but the better way is to send it in the intent as extras, or have a global variable and assign the text value of the result there.
Intent i=new Intent(context);
i.putExtra("QrText", result.getText());
context.startActivity(i);
and to get it in the other activity
Bundle extras = getIntent().getExtras();
String qrText;
if (extras != null) {
qrText = extras.getString("QrText");
}
// after you do the findViewById
resultTExtViewCode.setText(qrText)
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);
}
In my code below I am able to edit a text from my first setText() call but not from within the callback.
The error I receive is a NullPointerException for title in the callback.
How do I set this up so I can edit the text from within the callback?
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
setContentView(R.layout.listing_activity);
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}
Your setContentView() method must be called before giving references using findViewById() method, so your code will be -
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing_activity);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}
title.setText("listingId");//fine
That shouldn't be fine...
You must put setContentView before any findViewById. That is number one reason why your TextView is null.
Your callback is fine.
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);
}
}
}