I have a code to change the text of textview in a layout according to the value of the edittext (et) in the previous layout
there is MorningDrsGeneral :
public class MorningDrsGeneral extends ActionBarActivity {
Button button ;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morningdrs);
et = (EditText) findViewById(R.id.et);
addListenerOnButton1();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, bookingKamal.class);
intent.putExtra("fn" , et.getText().toString());
startActivity(intent);}
});}
#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);
}
}
and there is bookingKamal.java :
public class bookingKamal extends ActionBarActivity {
Button button ;
TextView textView3 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookingkamal);
textView3 = (TextView) findViewById(R.id.textView3) ;
String A = textView3.getText().toString();
String N = " " ;
if (A.equals(N)){
Intent intent = getIntent();
String texx = intent.getStringExtra("fn") ;
textView3.setText(texx);
}}
#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 have to keep the text in the bookingkamal layout .
It means when I go back from this layout and back to it the text should be the same as previous.
Then, it should be like this
bookingKamal.java
String texx;
void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MorningDrsGeneral.class);
intent.putExtra("texx" , texx);
startActivity(intent);
}
MorningDrsGeneral.java
protected void onCreate(Bundle savedInstanceState) {
et = (EditText) findViewById(R.id.et);
Intent intent2 = getIntent();
if (intent2.getStringExtra("texx") != "") {
String abcd = intent2.getStringExtra("texx");
et.setText(abcd);
}
}
You can use SharedPreferences, this will save everything if you leave the layout or even (if you want) if you close your app. Take a look at the Android documentation at http://developer.android.com/reference/android/content/SharedPreferences.html
Related
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();
}
}
}
i have a searchview with a listview. i put some items to open a new activity. While im in the first Activity, all right. But if i search another item on Searchview, app obeys the position. Then, wrong item is chosen.
How can i make the item click work by String?
My code:
public class MainActivity extends ListActivity {
ListView lv;
SearchView sv;
String[] teams={"Activity 1","Activity 2","Activity 3","Activity 4","Activity 5","Activity 6"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(android.R.id.list);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
#Override
//to open new activity
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0) {
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
}
}
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How can i make the item click work by String?
#Override
//to open new activity
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String team = teams[position];
if (team.equalsIgnoreCase("Activity 1")) {
Intent intent = new Intent(this, Activity1.class);
startActivity(intent);
} else if (team.equalsIgnoreCase("Activity 2")) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
} //...and so on
}
}
I am getting the message multiple markers at this line:
- Syntax error, insert ")" to complete
Expression
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert "}" to complete
ClassBody
This is the code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Menemukan lokasi tombol di activity_main.xml
Button tombol = (Button) findViewById(R.id.button1);
//Menangkap Klik pada Tombol
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//TODO Auto-generated method stub
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
} <<<<<<<<<< eror
}
#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);
}
}
You have not closed your on click listener anonymous class properly, you should have:
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
}
});
You are missing the last line.
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 am Very new to Android Programming.. I have listed my code below. MainActivity.java and activity_main.xml. I have tried following solutions but to no avail.
using only this
using getapplicationcontext()
using getbasecontext()
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button text2 = (Button) findViewById(R.id.text2);
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_LONG).show();
}
}
}); // Disabling/Enabling Wifi Mechanism
}
#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);
}
}
Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_LONG).show();
Try using the activity context rather than the application context. See this link for a more thorough description:
When to call activity context OR application context?