Android R.id error - android

I have the following code and have an error in action_settings in R.id.action_settings. This is in the last method posted here so scroll down. I am not sure what is supposed to go in here. I am trying to create an app that has a login page at the start, with the potential to create a new user id in a registration page, take a picture in another page, and see a menu in another page.
package com.example.reynaldo.project1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onButtonClick (View v){
if (v.getId() == R.id.Blogin) {
EditText a = (EditText) findViewById(R.id.TFemail);
String str = a.getText().toString();
EditText b = (EditText) findViewById(R.id.TFpassword);
String pass = b.getText().toString();
String password = helper.searchPass(str);
if (pass.equals(password)) {
Intent i = new Intent(MainActivity.this, Display.class);
i.putExtra("Email", str);
startActivity(i);
} else {
Toast temp = Toast.makeText(MainActivity.this, "Username and password don't match", Toast.LENGTH_SHORT);
temp.show();
}
}
if (v.getId() == R.id.Bsignup){
Intent i = new Intent (MainActivity.this, SignUp.class);
startActivity(i);
}
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
int id = item.getItemId();
if (id == R.id.action_settings){
return true;
}
return onOptionsItemSelected(item);
}
}

You have missed to import R.
import youPackage.R;
This way you can manually import R.
Please replace yourPackage with your package name of application. You can find it in your Manifest.xml file or with build.gradle file.

import com.example.reynaldo.project1.R
in your file and check you have a menu item with id action_settings and rebuild project.

Most likely your R.menu.main xml file does not contain an item having #+id/action_settings as its id. Check your R.menu.main xml file and make sure the action_settings id is set

import yourpackagename.R;
For Ex:
import com.example.reynaldo.project1.R;

Check in your menu file and make sure you have created item with
id = action_settings

Related

Get variables from one activity to another

I work simple android app for sending sms. I have 2 activities. One is main for sending 2 different messages with 2 different content but messages send to same number. On secund activity i have 3 fields: one is for input number to send messages, and other two are for message content. When I click save button app save user input and go back to main activity. And here start my problem. How can i send users input for number to send messages and messages content to main activity to send sms with saved user input? I am totally beginner with android developing so please help! Here is my MainActivity.java:
package com.example.davor.light;
import android.content.Intent; import
android.support.v7.app.ActionBarActivity; import android.os.Bundle;
import android.telephony.SmsManager; import
android.telephony.SmsMessage; import android.view.Menu; import
android.view.MenuItem; import android.view.View; import
android.widget.Button; import android.widget.ImageView; import
android.widget.TextView; import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// GUMBI INFORMACIJE
Button ukljuci = (Button) findViewById(R.id.ukljuci);
Button iskljuci = (Button) findViewById(R.id.iskljuci);
Button postavke = (Button) findViewById(R.id.postavke);
final ImageView slika = (ImageView) findViewById(R.id.slika);
// INFORMACIJA O PORUCI
final String broj = "097";
final String ukljuciPoruka = "Uključi";
final String iskljuciPoruka = "Isključi";
// KLIK NA GUMB ISKLJUČI
iskljuci.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(broj, null, iskljuciPoruka, null, null);
Toast.makeText(getApplicationContext(), "Isključeno! poslano na broj " + broj, Toast.LENGTH_LONG).show();
slika.setImageResource(R.drawable.off);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Nemoguće isključiti!",Toast.LENGTH_LONG).show();
}
}
});
postavke.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent PostavkeActivity = new Intent(MainActivity.this,
Postavke.class);
startActivity(PostavkeActivity);
}
});
}
#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);
} }
And here is code for my second activity from witch I want to pull users input to MainActivity:
package com.example.davor.light;
import android.app.Activity; import android.content.Intent; import
android.content.SharedPreferences; import
android.preference.PreferenceManager; import
android.support.v7.app.ActionBarActivity; import android.os.Bundle;
import android.view.Menu; import android.view.MenuItem; import
android.view.View; import android.widget.Button; import
android.widget.EditText; import android.widget.TextView; import
android.widget.Toast;
public class Postavke extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_postavke);
gumbZaPovratak();
SharedPreferences loadSettings = PreferenceManager.getDefaultSharedPreferences(this);
String ucitajBroj = loadSettings.getString("spremiBroj", "");
String ucitajUkljuci = loadSettings.getString("spremiUkljuci", "");
String ucitajIskljuci = loadSettings.getString("spremiIskljuci", "");
final EditText postavkeBroj = (EditText) findViewById(R.id.postavkeBroj);
postavkeBroj.setText(ucitajBroj);
final EditText postavkeUkljuci = (EditText) findViewById(R.id.postavkeUkljuci);
postavkeUkljuci.setText(ucitajUkljuci);
final EditText postavkeIskljuci = (EditText) findViewById(R.id.postavkeIskljuci);
postavkeIskljuci.setText(ucitajIskljuci);
Button spremi = (Button) findViewById(R.id.postavkeSpremi);
spremi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
spremiPostavke("spremiBroj", postavkeBroj.getText().toString());
spremiPostavke("spremiUkljuci", postavkeUkljuci.getText().toString());
spremiPostavke("spremiIskljuci", postavkeIskljuci.getText().toString());
Toast.makeText(getApplicationContext(), "Spremljeno", Toast.LENGTH_LONG).show();
finish();
}
});
}
private void postavkeBroj() {
EditText postavkeBroj = (EditText) findViewById(R.id.postavkeBroj);
}
private void spremiPostavke (String ključ, String vrijednost) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(ključ, vrijednost);
editor.commit();
}
private void gumbZaPovratak(){
Button nazad = (Button) findViewById(R.id.nazad);
nazad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#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_postavke, 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);
} }
You can use startActivityForResult()
You can refer this link:
http://hmkcode.com/android-startactivityforresult/
In Activity1
Intent myIntent = new Intent(this,Activity2.class);
myIntent.putExtra("var", variable_to_pass);
startActivity(myIntent);
In Activity2
Bundle extras = getIntent().getExtras();
if (extras != null) {
intentExtra = extras.getString("var"); // retrieving variable
}
You can add data to an intent like this :
String messageContent = (EditText) findViewById(yourMessageContentId).getText().toString();
String messageNumber = (EditText) findViewById(yourMessageNumberId).getText().toString();
intent.putExtra("com.example.davor.light.MESSAGE_CONTENT", messageContent);
intent.putExtra("com.example.davor.light.MESSAGE_NUMBER", messageNumber);
And then get it on the MainActivity :
Intent intent = getIntent();
String messageNumber = intent.getStringExtra("com.example.davor.light.MESSAGE_NUMBER");
String messageContent = intent.getStringExtra("com.example.davor.light.MESSAGE_CONTENT");
you can send information from one activity to other activity like bellow
Intent postavkeActivity = new Intent(MainActivity.this,
Postavke.class);
Bundle bundle=new Bundle();
bundle.putString(|"messageKey1", "message content1");
bundle.putString(|"messageKey2", "message content2");
postavkeActivity.putExtras(bundle);
startActivity(PostavkeActivity);
in main activity get bundle data like bellow
Bundle b=getIntent.getExtras();
String smsContent=b.getString("messageKey1");

Intent not passing correct information

Im using intents to pass the noteId from my database to a different activity for displaying the information it was working and i went to work on another part of the project and now it seems to only pass one value and from playing about i have found that seems to be whatever is the next highest object on the ListView. Sorry if this is badly worded, Im not very good at explaining these things
ViewNote Class
package com.hardy.passnotes;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ViewNote extends Activity {
EditText NewNoteTitle;
EditText NewNoteContent;
TextView tvNoteId;
DBTools dbTools = new DBTools(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_note);
NewNoteTitle = (EditText)findViewById(R.id.ETNewNoteTitle);
NewNoteContent = (EditText)findViewById(R.id.ETNewNoteContent);
Intent n = getIntent();
String NoteId = n.getStringExtra("noteId");
Toast.makeText(getApplicationContext(), NoteId,
Toast.LENGTH_LONG).show();
HashMap<String, String> NoteList = dbTools.getNoteInfo(NoteId);
if(NoteList.size() != 0)
{
setTitle("Note: " + NoteList.get("noteTitle"));
NewNoteTitle.setText(NoteList.get("noteTitle"));
NewNoteContent.setText(NoteList.get("noteContent"));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_note, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_Editnote:
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(getApplication(),EditNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
case R.id.action_DelNote:
Intent intent1 = getIntent();
String NoteId = intent1.getStringExtra("noteId");
dbTools.deleteNote(NoteId);
Intent i = new Intent(getApplication(),MyNotes.class);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
MyNote Class:
package com.hardy.passnotes;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyNotes extends ListActivity {
DBTools dbTools = new DBTools(this);
TextView tvNoteId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_notes);
Log.i("Tag", "OnCreate Started As Normal");
setTitle("My Notes");
ArrayList <HashMap<String, String>> NoteList = dbTools.getAllNotes();
if(NoteList.size() != 0)
{
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(MyNotes.this,ViewNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
Log.i("Tag", "if Started As Normal");
}
});
ListAdapter Adapter = new SimpleAdapter(MyNotes.this,NoteList,R.layout.notes_list, new String[]{"noteId","noteTitle",}, new int[]{R.id.NoteId,R.id.NoteTitle} );
setListAdapter(Adapter);
Log.i("Tag", "Arrey Started As Normal");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_notes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.action_Add:
Intent i = new Intent(getApplicationContext(),CreateNote.class);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
Thanks in advance been searching around and debugging for hours with no luck.
Hi I think the problem is that you use the findViewById() method inappropriately in your onItemClick listener.
You should use:
tvNoteId = (TextView)view.findViewById(R.id.NoteId);
Notice that I call the findViewById method on the view given by the onItemClick method because you want to find the view with the id NoteId from the pressed item and not from the entire's activity content view.. the way you do it will search in the content view of your activity for the first id equal with NoteId ...
I think your note get deleted on the moment edit is clicked from the options menu.
At the end of case R.id.action_Editnote nothing is returned nor break is called so case R.id.action_Delnote will be executed as well. This is where you delete the note from your database.
In your switch, put a break:
switch (item.getItemId())
{
case R.id.action_Editnote:
tvNoteId = (TextView)findViewById(R.id.NoteId);
String NoteValue = tvNoteId.getText().toString();
Intent intent = new Intent(getApplication(),EditNote.class);
intent.putExtra("noteId", NoteValue);
startActivity(intent);
break;
case R.id.action_DelNote:
Intent intent1 = getIntent();
String NoteId = intent1.getStringExtra("noteId");
dbTools.deleteNote(NoteId);
Intent i = new Intent(getApplication(),MyNotes.class);
startActivity(i);
finish();
break;
}

How to change background color across activities

This is my first app and my first time posting so bear with me.
I'm trying to make it so the user can choose the background color of the entire app from a Change Skins screen. However with what I have now it only changes the color of the activity until it goes back to the main activity.
Here is the code for the Change Skins screen
package cs.pacificu.mypace;
import android.R.layout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Skin extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
final String[] SKINS = new String[] {"Light", "Dark"};
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
final ListView listView = (ListView) findViewById (R.id.playlists);
listView.setAdapter(new ArrayAdapter<String> (this,R.layout.single_list_item,SKINS));
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
//
View layout = findViewById(R.layout.activity_main);
String selectedFromList = (listView.getItemAtPosition(position).toString ());
if (SKINS [0] == selectedFromList)
{
listView.setBackgroundColor(Color.CYAN);
//layout.setBackgroundColor(android.R.color.darker_gray);
}
else if (SKINS[1] == selectedFromList)
{
listView.setBackgroundColor(Color.BLACK);
//layout.setBackgroundColor(android.R.color.black);
}
//
finish();
}
});
}
}
And here is the main activity code
package cs.pacificu.mypace;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
/*import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;*/
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
startPlaylists(null);
}
#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)
{
switch (item.getItemId()) {
case R.id.skin:
changeSkin();
return true;
case R.id.action_settings:
settingsList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void startPlaylists (View view)
{
Intent intentPlaylists = new Intent();
intentPlaylists.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Playlist");
intentPlaylists.setAction("#strings/action_playlists");
startActivity(intentPlaylists);
}
public void changeSkin ()
{
Intent intentSkin = new Intent();
intentSkin.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Skin");
intentSkin.setAction("#strings/action_skin");
startActivity(intentSkin);
}
public void settingsList ()
{
Intent intentSettings = new Intent();
intentSettings.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Settings");
intentSettings.setAction("#strings/action_settings");
startActivity(intentSettings);
}
}
Thank you!
Put this on your xml
android:background="#025cd8"
Depending on your choice of color.
what you can do is set a SharedPreferences value to what the user selects, and at the start of each of your activities, set that value to the background colour of your layout. That is the best way I see to solving this.
So in all of your activities, you would set them to the value from the SharedPreferences and if not set use your default one. As for storing the possible values, you can either use the XML file or just hard code the values in one global class, although the former is preferable.
Look here and see what they did (Android-page-about-data-storage)
Edit:
If you are going to keep it simple like that, just background colour, then you can do something like this:
|- Keep these somewhat global, maybe in a class and access them as needed,
// Make sure to maintain the connection
String[] SKINS = {"Light", "Dark"};
int[] COLOURS = {Color.CYAN, Color.BLACK};
|- For the onItemClick(),
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// You may wish to make these two variables class-global
SharedPreferences settings = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("mskin", COLOURS[position]);
editor.commit();
// For on the spot changes
listView.setBackgroundColor(COLOURS[position]);
// Your other code, as you wish, before or after, depends on how you need it.
|- As for your other activities, or at the onCreate() of all activities,
SharedPreferences settings = getPreferences(MODE_PRIVATE);
listView.setBackgroundColor(settings.getInt("mskin", COLOURS[0]));

exchange data between activities

I created 2 Activities, Main and Second. And I want to send text, put in the box EditText from Main to Second activity. Id of first EditText is UserName, and of the second is Description.
The code of the Main activity is:
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.EditText;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("username", UserName.getText().toString());
intent.putExtra("gift", Description.getText().toString());
startActivity(intent);
}
public void ButtonOneClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent i = new Intent(this, Second.class);
startActivity(i);
break;
}
}
}
Full code of the Second Activity is:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txtInfo = (TextView)findViewById(R.id.TextOne);
String user = "ЖЫвотное";
String gift = "дырку от бублика";
user = getIntent().getExtras().getString("username");
gift = getIntent().getExtras().getString("gift");
txtInfo.setText(user + " , вам передали " + gift);
}
}
and errors:
main.java: Description cannot be resolved
main.java: UserName cannot be resolved
sorry for my english
Thanks for help
P.S. I'm studying java and programming for android just third day, please don't throw stones to me.
You'd then refer to your R class file, that is:
EditText desc = (EditText) findViewById(R.id.Description);
...
You have not included
EditText UserName = (EditText) findViewById(R.id.username);
EditText Description = (EditText) findViewById(R.id.description);
in your onCreate() method in MainActivity.
As you reference UserName and Description here refer to them as they are classes. Unless you have such classes an error will be stated because these classes can not be found (or "resolved").
You can even notice this by the syntax highlight. Do you see the words a displayed in a lght green/turquoise like Main or Intent?
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
intent.putExtra("username", UserName.getText().toString());
intent.putExtra("gift", Description.getText().toString());
startActivity(intent);
}
Instead you want instances of the class EditText which you call userName and description. On these objects you can perform getText() and work your way along. :)
public void onClick(View v) {
Intent intent = new Intent(Main.this, Second.class);
EditText userName = (EditText) findViewById(R.id.username);
EditText description = (EditText) findViewById(R.id.description);
intent.putExtra("username", userName.getText().toString());
intent.putExtra("gift", description.getText().toString());
startActivity(intent);
}
Please notice I've changed the names of your variables to lowerCase starting camelCase. Though not an explicit rule it's considered good practise to start variable names with lower case letters ('userName,description,intent) while **C**lass references like (ÈditText,Main,Second`) are started with upper case letters.
you have not initialize the username & description in your java;
EditText us_name=(EditText) findViewById(R.id.user_name);
EditText us_desp=(EditText) findViewById(R.id.desp);

calling menu from other activity

please guide me how can i call the menu on the other activity
Like here is the main activity
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class ControlMenu extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, ShowSettings.class);
startActivity(intent);
break;
case R.id.services: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.Quit:
new Thread(new Runnable() {
public void run() {
ControlMenu.this.finish();
// The following makes the Android Gods frown upon me
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}).start();
break;
default:
break;
}
return true;
}
}
now i have make another activity in which i need to show the same menu in this
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ShowSettings extends Activity {
Prefs myprefs = null;
final String tag = "CH12:ShowSettings";
AlertDialog.Builder adb;// = new AlertDialog.Builder(this);
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.showsettings);
this.myprefs = new Prefs(getApplicationContext());
// load screen
PopulateScreen();
this.adb = new AlertDialog.Builder(this);
final Button savebutton = (Button) findViewById(R.id.settingssave);
// create anonymous click listener to handle the "save"
savebutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
// get the string and do something with it.
final EditText email = (EditText) findViewById(R.id.emailaddress);
if (email.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter Your Email Address");
ad.show();
return;
}
final EditText serverurl = (EditText) findViewById(R.id.serverurl);
if (serverurl.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter The Server URL");
ad.show();
return;
}
// save off values
ShowSettings.this.myprefs.setEmail(email.getText().toString());
ShowSettings.this.myprefs.setServer(serverurl.getText().toString());
ShowSettings.this.myprefs.save();
// we're done!
finish();
} catch (Exception e) {
Log.i(ShowSettings.this.tag, "Failed to Save Settings [" + e.getMessage() + "]");
}
}
});
}
private void PopulateScreen() {
try {
final EditText emailfield = (EditText) findViewById(R.id.emailaddress);
final EditText serverurlfield = (EditText) findViewById(R.id.serverurl);
emailfield.setText(this.myprefs.getEmail());
serverurlfield.setText(this.myprefs.getServer());
} catch (Exception e) {
}
}
}
what should i write in the above code so my menu can also appear over here .
Create the menu as an xml resource, then you can access it from everywhere. Check the documentation. If you need to pass a certain data from only one activity to the other, extending Application seems too much, given you could just pass the info as en extra in the Intent. Besides there are "dangers" in that way of doing things (because of the activity lyfecicle) so you should use SharedPreferences in that case (instead of creating an Application global variable).
If you want to share some information or data or lists (or anything really) with your app, you should look at the following link and make your app as follows:
How to declare global variables in Android?

Categories

Resources