I'm very new to Android Studio. I am trying to get data from my EditMessage and EditSendTo activities to my TestExplicitIntents activity. This is a school project, but all I was required to do was create the EditSendTo activity and display the phone number in the TestExplicitIntents activity.
I can do this following lessons on the other activities, but I would like to learn how to display the data from both Edit activities. I have tried several approaches but everything I try ends with either the EditMessage or the EditSendTo producing null results once the Done button is pressed.
public class TestExplicitIntents extends Activity {
public static final String CLASS_TAG = "TestExplicitIntents";
public static final int NEW_MESSAGE_REQUEST = 1;
public static final int NEW_PHONE_REQUEST = 1;
private String message = "";
private String phone = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_explicit_intents);
// Getting to the views defined in the XML files.
TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
tvMessageDetails.setBackgroundColor(Color.GREEN);
tvMessageDetails.setMovementMethod(new ScrollingMovementMethod());
message ="Is it St. Patricks Day?";
phone = "";
setSummary();
// Responding to an event - the onClick for the Edit Message Button
// Using a named inner class
Button btnEditMessage;
btnEditMessage = (Button) this.findViewById(R.id.btnEditMessage);
HandleButtonEditMessageOnClick buttonEditMessageOnClick;
buttonEditMessageOnClick = new HandleButtonEditMessageOnClick();
btnEditMessage.setOnClickListener(buttonEditMessageOnClick);
// Responding to an event - the onClick for the Edit Send To Button
// Using a named inner class
Button btnEditSendTo;
btnEditSendTo = (Button) this.findViewById(R.id.btnEditSendTo);
HandleButtonEditSendToOnClick buttonEditSendToOnClick;
buttonEditSendToOnClick = new HandleButtonEditSendToOnClick();
btnEditSendTo.setOnClickListener(buttonEditSendToOnClick);
}
/**
* Put together a summary of the phone and message and display it.
*/
private void setSummary() {
StringBuilder summary;
summary = new StringBuilder("Sending To:\n");
summary.append(phone);
summary.append("\n\nMessage:\n");
summary.append(message);
TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
tvMessageDetails.setText(summary);
}
/**
* Handle Edit Button OnClick by starting the activity This is an example of
* starting another activity using an explicit Intent.
*
*/
#SuppressWarnings("rawtypes")
public class HandleButtonEditMessageOnClick implements OnClickListener {
public static final String CLASS_TAG = "HandleButtonEditMessageOnClick";
public void onClick(View v) {
Log.i(CLASS_TAG, "onClick started...");
// Example of an EXPLICIT intent, as we are naming the java class to use
// (EditMessage.class)
Intent editIntent;
Activity sourceActivity;
Class destinationClass;
sourceActivity = TestExplicitIntents.this;
destinationClass = EditMessage.class;
editIntent = new Intent(sourceActivity, destinationClass);
// Sending information to the intent receiver through the Intent object
editIntent.putExtra("CURRENT_MESSAGE", TestExplicitIntents.this.message);
//startActivity(editIntent);
startActivityForResult(editIntent, NEW_MESSAGE_REQUEST);
}
}
#SuppressWarnings("rawtypes")
public class HandleButtonEditSendToOnClick implements OnClickListener {
public static final String CLASS_TAG = "HandleButtonEditSendToOnClick";
public void onClick(View v) {
Log.i(CLASS_TAG, "onClick started...");
Intent editSendIntent;
Activity startActivity;
Class endClass;
startActivity = TestExplicitIntents.this;
endClass = EditSendTo.class;
editSendIntent = new Intent(startActivity, endClass);
// Sending information to the intent receiver through the Intent object
editSendIntent.putExtra("CURRENT_PHONE", TestExplicitIntents.this.phone);
//startActivity(editIntent);
startActivityForResult(editSendIntent, NEW_PHONE_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
String newMessage = getIntent().getStringExtra("NEW_MESSAGE");
String curMessage = getIntent().getStringExtra("CURRENT_MESSAGE");
String newPhone = getIntent().getStringExtra("NEW_PHONE");
String curPhone = getIntent().getStringExtra("CURRENT_PHONE");
// Check which request we're responding to
if (requestCode == NEW_MESSAGE_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
message = newMessage;
phone = curPhone;
setSummary();
}
}
if (requestCode == NEW_PHONE_REQUEST) {
if (resultCode == RESULT_OK) {
message = curMessage;
phone = newPhone;
setSummary();
}
}
}
}
Here is EditMessage:
public class EditMessage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_message);
// Get the intent for this activity. Every activity has an intent and
// set the EditText contents to the string in the extra info that comes with
// the intent
Intent editIntent;
EditText etMessage;
editIntent = this.getIntent();
String theMessage;
theMessage = editIntent.getStringExtra("CURRENT_MESSAGE");
etMessage = (EditText) this.findViewById(R.id.etMessage);
etMessage.setText(theMessage);
// Get an event handler going for the Done button so that we can return the
// new message
Button btnDone = (Button) this.findViewById(R.id.btnDone);
btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_edit_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Handles the Button Done onClick event by creating a resulting Intent and
* finishing
*/
private class ButtonDoneOnClickHandler implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("NEW_MESSAGE", ((EditText) findViewById(R.id.etMessage)).getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
}
Here is EditSendTo:
public class EditSendTo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_send_to);
// Get the intent for this activity. Every activity has an intent and
// set the EditText contents to the string in the extra info that comes with
// the intent
Intent editIntent;
EditText etPhone;
editIntent = this.getIntent();
String thePhone;
thePhone = editIntent.getStringExtra("CURRENT_PHONE");
etPhone = (EditText) this.findViewById(R.id.etPhone);
etPhone.setText(thePhone);
// Get an event handler going for the Done button so that we can return the
// new message
Button btnDone = (Button) this.findViewById(R.id.btnDone);
btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_edit_send_to, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Handles the Button Done onClick event by creating a resulting Intent and
* finishing
*/
private class ButtonDoneOnClickHandler implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("NEW_PHONE", ((EditText)
findViewById(R.id.etPhone)).getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
}
I tried to figure this out for a few days and have had no luck finding an answer.
I would really appreciate a pointer in the right direction.
Your code should be like below :
TestExplicitIntents.java
public class TestExplicitIntents extends Activity {
public static final String CLASS_TAG = "TestExplicitIntents";
public static final int NEW_MESSAGE_REQUEST = 1;
public static final int NEW_PHONE_REQUEST = 2;
private String message = "";
private String phone = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_explicit_intents);
// Getting to the views defined in the XML files.
TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
tvMessageDetails.setBackgroundColor(Color.GREEN);
tvMessageDetails.setMovementMethod(new ScrollingMovementMethod());
message = "Is it St. Patricks Day?";
phone = "";
setSummary();
// Responding to an event - the onClick for the Edit Message Button
// Using a named inner class
Button btnEditMessage;
btnEditMessage = (Button) this.findViewById(R.id.btnEditMessage);
HandleButtonEditMessageOnClick buttonEditMessageOnClick;
buttonEditMessageOnClick = new HandleButtonEditMessageOnClick();
btnEditMessage.setOnClickListener(buttonEditMessageOnClick);
// Responding to an event - the onClick for the Edit Send To Button
// Using a named inner class
Button btnEditSendTo;
btnEditSendTo = (Button) this.findViewById(R.id.btnEditSendTo);
HandleButtonEditSendToOnClick buttonEditSendToOnClick;
buttonEditSendToOnClick = new HandleButtonEditSendToOnClick();
btnEditSendTo.setOnClickListener(buttonEditSendToOnClick);
}
/**
* Put together a summary of the phone and message and display it.
*/
private void setSummary() {
StringBuilder summary;
summary = new StringBuilder("Sending To:\n");
summary.append(phone);
summary.append("\n\nMessage:\n");
summary.append(message);
TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
tvMessageDetails.setText(summary);
}
/**
* Handle Edit Button OnClick by starting the activity This is an example of
* starting another activity using an explicit Intent.
*/
#SuppressWarnings("rawtypes")
public class HandleButtonEditMessageOnClick implements View.OnClickListener {
public static final String CLASS_TAG = "HandleButtonEditMessageOnClick";
public void onClick(View v) {
Log.i(CLASS_TAG, "onClick started...");
// Example of an EXPLICIT intent, as we are naming the java class to use
// (EditMessage.class)
Intent editIntent;
Activity sourceActivity;
Class destinationClass;
sourceActivity = TestExplicitIntents.this;
destinationClass = EditMessage.class;
editIntent = new Intent(sourceActivity, destinationClass);
// Sending information to the intent receiver through the Intent object
editIntent.putExtra("CURRENT_MESSAGE", TestExplicitIntents.this.message);
//startActivity(editIntent);
startActivityForResult(editIntent, NEW_MESSAGE_REQUEST);
}
}
#SuppressWarnings("rawtypes")
public class HandleButtonEditSendToOnClick implements View.OnClickListener {
public static final String CLASS_TAG = "HandleButtonEditSendToOnClick";
public void onClick(View v) {
Log.i(CLASS_TAG, "onClick started...");
Intent editSendIntent;
Activity startActivity;
Class endClass;
startActivity = TestExplicitIntents.this;
endClass = EditSendTo.class;
editSendIntent = new Intent(startActivity, endClass);
// Sending information to the intent receiver through the Intent object
editSendIntent.putExtra("CURRENT_PHONE", TestExplicitIntents.this.phone);
//startActivity(editIntent);
startActivityForResult(editSendIntent, NEW_PHONE_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == NEW_MESSAGE_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
message = data.getStringExtra("NEW_MESSAGE");
setSummary();
}
} else if (requestCode == NEW_PHONE_REQUEST) {
if (resultCode == RESULT_OK) {
phone = data.getStringExtra("NEW_PHONE");
setSummary();
}
}
}
}
EditSendTo class:
public class extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_send_to);
// Get the intent for this activity. Every activity has an intent and
// set the EditText contents to the string in the extra info that comes with
// the intent
Intent editIntent;
EditText etPhone;
editIntent = this.getIntent();
String thePhone;
thePhone = editIntent.getStringExtra("CURRENT_PHONE");
etPhone = (EditText) this.findViewById(R.id.etPhone);
etPhone.setText(thePhone);
// Get an event handler going for the Done button so that we can return the
// new message
Button btnDone = (Button) this.findViewById(R.id.btnDone);
btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_edit_send_to, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Handles the Button Done onClick event by creating a resulting Intent and
* finishing
*/
private class ButtonDoneOnClickHandler implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("NEW_PHONE", ((EditText)
findViewById(R.id.etPhone)).getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
}
EditMessage class:
public class EditMessage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_message);
// Get the intent for this activity. Every activity has an intent and
// set the EditText contents to the string in the extra info that comes with
// the intent
Intent editIntent;
EditText etMessage;
editIntent = this.getIntent();
String theMessage;
theMessage = editIntent.getStringExtra("CURRENT_MESSAGE");
etMessage = (EditText) this.findViewById(R.id.etMessage);
etMessage.setText(theMessage);
// Get an event handler going for the Done button so that we can return the
// new message
Button btnDone = (Button) this.findViewById(R.id.btnDone);
btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_edit_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Handles the Button Done onClick event by creating a resulting Intent and
* finishing
*/
private class ButtonDoneOnClickHandler implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("NEW_MESSAGE", ((EditText) findViewById(R.id.etMessage)).getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
}
Related
I want to change the menuitem register of my menu when I return from another activity to settings which will go to a new intent. On clicking register which is in menu_item the activity goes to RegisterActivity. On returning from RegisterActivity I want to change the menu item register to settings.
package com.mausamakasvani.srsk.mausamakasvani;
public class LoginActivity extends AppCompatActivity {
private EditText password;
private EditText userName;
public static boolean Register;
public static final int REG_REQ_CODE = 235;
private int x = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar as = getSupportActionBar();
as.setTitle("Login");
userName = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
}
public void onLoginClicked(View view){
/*
*/
if (!MausamCheckList.getStringValue(this,MausamCheckList.USER_NAME).equals(MausamCheckList.DEF_VALUE)) {
if (userName.getText().toString().equals(MausamCheckList.getStringValue(this, MausamCheckList.USER_NAME)) && password.getText().toString().equals(MausamCheckList.getStringValue(this, MausamCheckList.PASSWORD))) {
Intent intent = new Intent(this,SheharChunoActivity.class);
startActivity(intent);
//finish();
}else {
MausamCheckList.displayToast(this,"Username or Password is Wrong!!!");
}
}else{
MausamCheckList.displayToast(this,"Please register");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
if(x==0)
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent = new Intent(this,RegisterActivity.class);
startActivityForResult(intent,REG_REQ_CODE);
//startActivity(intent);
return super.onOptionsItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REG_REQ_CODE) {
if (resultCode == RegisterActivity.REG_RES_CODE) {
x=RegisterActivity.REG_RES_CODE;
}
}
}
}
You can include both menu items in one menu.xml file and depending on what you want to show change visibility of particular item. You can simple hold reference to Menu (and use findItem method as well) or MenuItem inside your activity and manage it like this (pseudo-code):
if(user.isRegistered()) {
mSettingsMenuItem.setVisibility(View.VISIBLE);
mRegisterMenuItem.setVisibility(View.GONE);
}
You can also use
public boolean onPrepareOptionsMenu (Menu menu)
which is called everytime menu is shown on the screen and depending on the conditions show proper menu items
Documentation for this
i am trying to develop a simple app that read a QR code with zxing and redirect to the corresponding link. I do the MainActivity Class, with methods on create and onActivityResult, but when i scan a QR code my app crashes....and i dunno why can anyone help me? i post my MainActivity class, if it can help...
Thanks to all
Fabrizio
-------- MainActivity------------
public class MainActivity extends Activity {
// static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private Button button;
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.go_direct);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
public void scanNow(View view) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
// intent.setPackage("com.google.zxing.client.android");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
"QR_CODE_MODE");
startActivityForResult(intent, 0);
}
#SuppressLint("SetJavaScriptEnabled")
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
Log.i("intent", intent.toString());
Log.i("scan result", intent.getStringExtra("SCAN_RESULT"));
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: " + contents + " format: " + format); // Handle
// successful
// scan
setContentView( R.layout.web_view);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(contents);
} else if (resultCode == RESULT_CANCELED) { // Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);}}
I'm writing an app to take a survey and tally's the answers. The app has a default question "Do you like the Vikings", which works fine. I added in the feature to edit the question & buttons but now both are blank by default. Worse, no matter what I put in the edit activity, it stays blank!
public class MainActivity extends ActionBarActivity {
private Button mYesButton;
private Button mNoButton;
private Button mResultsButton;
private TextView mQuestionTextView;
private Button mEditButton;
private TextView EditQuestion;
private Button YesButtonEdit;
private Button NoButtonEdit;
int YesTimes;
int NoTimes;
public static final String EXTRA_EDIT_QUESTION = "com.example.surveyapp.EditActivity.edit_this_question";
//private SurveryQuestion[] mQuestionBank = new SurveryQuestion[] {
// new SurveryQuestion(R.string.question_1),
// new SurveryQuestion()
//};
String[] mSurveyArray = {
new String("hello"), //More questions coming soon!
new String("Hello1"),
//new String(R.string.question_1)
};
int mCurrentIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYesButton = (Button)findViewById(R.id.yes_button); //Yes/NoButton modified from LMGTFY app
mYesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YesTimes += 1;
mCurrentIndex = (mCurrentIndex + 1) % mSurveyArray.length;
// Coming soon!
}
});
mNoButton = (Button)findViewById(R.id.no_button);
mNoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NoTimes += 1;
}
});
mResultsButton = (Button)findViewById(R.id.result_button);
mResultsButton.setOnClickListener(new View.OnClickListener() {
//Here we make toast!
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), //Discovered getApplicationContext from stackoverflow
mYesButton + ": " + YesTimes + mNoButton + ": " + NoTimes, Toast.LENGTH_SHORT).show();
}
});
mEditButton = (Button)findViewById(R.id.edit_button); //This is the button that lets you edit the survey
mEditButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, EditActivity.class);
startActivity(i);
}
});
Intent intent = getIntent();
String EditQuestion = intent.getStringExtra("sendBack");
String YesButtonEdit = intent.getStringExtra("sendBack2");
String NoButtonEdit = intent.getStringExtra("sendBack3");
TextView mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
Button mYesButton = (Button)findViewById(R.id.yes_button);
Button mNoButton = (Button)findViewById(R.id.no_button);
mQuestionTextView.setText(EditQuestion);
mYesButton.setText(YesButtonEdit);
mNoButton.setText(NoButtonEdit);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is the second activity where you add in your edited responses.
package com.example.surveyapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EditActivity extends Activity {
private Button mCompleteButton;
private EditText mEditText1;
private EditText mEditText2;
private EditText mEditText3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mCompleteButton = (Button)findViewById(R.id.CompleteButton);
mCompleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText mEditText1 = (EditText) findViewById(R.id.editText1);
EditText mEditText2 = (EditText) findViewById(R.id.editText2);
EditText mEditText3 = (EditText) findViewById(R.id.editText3);
Intent i = new Intent(EditActivity.this, MainActivity.class);
i.putExtra("SendBack", mEditText1.getText().toString()); //This is supposed to use EXTRA to send back the data
i.putExtra("SendBack2", mEditText2.getText().toString());
i.putExtra("SendBack3", mEditText2.getText().toString());
startActivity(i);
}
});
}
}
I've tried several approaches and I've gotten nothing. Does anyone know the answer?
I think that the problem might be that when you call startActivity with an intent to make a new MainActivity, android recognizes that there is already an instance of this activity, so it just brings it to the front.
One way to get around this would be to first make a public method that you can use to edit the fields in your MainActivity, and you can call this method just before you would like to send the user back to this activity from the edit activity. Also, instead of creating a new activity, just close the edit activity and android will resume the main activity.
If this doesn't work, post any logcat data you have from testing your app.
For clean and clearer code, initialize all your button objects in the onCreate() method. Now try putting the following code in the onResume callback method like this
#Override
public void onResume()
{
super.onResume();
Intent intent = getIntent();
String EditQuestion = intent.getStringExtra("sendBack");
String YesButtonEdit = intent.getStringExtra("sendBack2");
String NoButtonEdit = intent.getStringExtra("sendBack3");
TextView mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
Button mYesButton = (Button)findViewById(R.id.yes_button);
Button mNoButton = (Button)findViewById(R.id.no_button);
mQuestionTextView.setText(EditQuestion);
mYesButton.setText(YesButtonEdit);
mNoButton.setText(NoButtonEdit);
}
remove this code from your onCreate(...) method and place it in your onResume method. When you clcik the edit button and want to get the data in the edit activity, do like so from the callling activity
mEditButton = (Button)findViewById(R.id.edit_button); //This is the button that lets you edit the survey
mEditButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, EditActivity.class);
i.putExtra("sendBack","send back value");
i.putExtra("sendBack2","send back 2 value");
startActivity(i);
}
});
with this, you should now be able to modify and pass in the values you wish to modify in your EditActivity
I get error with Intent. It says: intent is undefined. I've been searching a lot of other posts and it still doesn't work. Any ideas?
for your knowledge, i develop my own app with the basic of android developpes. Because my own intent didn't work, i used and created the exact same class as in the example, class DisplayMessageActivity but the problem remains the same, Here some of my code.
public class Main extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
catch(Exception e){
System.out.println("fout zit hier");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
Try replacing the first parameter on the Intent constructor.
Intent intent = new Intent(this, DisplayMessageActivity.class); // wrong
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); // right
The first parameter is a Context object, and remember that a Fragment does not inherit from Context, so the solution is getting the one from the parent Activity.
So there is 4 buttons on this page... 3 of them work fine, the 4th button, which links to ToolsTableLayout.class does not respond at all. There are no erros, the app does not crash or anything... you just click the button and nothing happens.
Code for the button class:
public class MainMenu extends Activity implements OnClickListener{
private String result;
boolean isScanout;
public static final String SCAN_RESULT = "MyPreferencesFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Button ScanOut=(Button)findViewById(R.id.scanout);
ScanOut.setOnClickListener(this);
Button ScanIn=(Button)findViewById(R.id.scanin);
ScanIn.setOnClickListener(this);
Button EndSession = (Button) findViewById(R.id.endsession);
EndSession.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.scanout){
isScanout = true;
IntentIntegrator.initiateScan(this);
}
else if(v.getId()==R.id.scanin){
isScanout = false;
IntentIntegrator.initiateScan(this);
}
else if(v.getId()==R.id.endsession){
Intent endsessionintent = new Intent(MainMenu.this, MainActivity.class);
startActivity(endsessionintent);
}
else if(v.getId()==R.id.toolDB){
Intent i = new Intent(MainMenu.this, ToolsTableLayout.class);
startActivity(i);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String qrCode = scanResult.getContents();
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
SharedPreferences.Editor editor = codeHack.edit();
editor.putString("entry", qrCode);
editor.commit();
}
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
R.id.toolDB is the button that makes no response..
and here is the ToolsTableLayout.java class (which does not open):
public class ToolsTableLayout extends Activity {
public static final String SCAN_RESULT = "MyPreferencesFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelayout2);
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
String QRcode = codeHack.getString("entry", "unregistered");
StringTokenizer token = new StringTokenizer(QRcode," ");
String name = token.nextToken();
String quantity = token.nextToken();
TextView t1 = (TextView) findViewById(R.id.slot1b);
TextView t2 = (TextView) findViewById(R.id.slot2b);
TextView t3 = (TextView) findViewById(R.id.slot3b);
ToolDB info = new ToolDB(this);
info.open();
String c1 = info.getRowID();
info.createEntry(name, quantity);
info.close();
t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);
t1.setText(c1);
t2.setText(name);
t3.setText(quantity);
}
}
not sure if the XML files are needed? if so let me know and I will update.
You did not set onClickListener for 4th button. Please add
Button btnToolDb = (Button) findViewById(R.id.toolDB);
btnToolDb.setOnClickListener(this);
Judging by the code you have provided, I believe you have forgotten to create and attach the ClickListener to the fourth button which is missing from the code.