I have encountered a problem. When I'm at this page (the main menu) and I try to click the calculate button it will go back to the login page. However, the calculate and records page are all also opened, and can be accessed by clicking the back button.
Button Calculate;
Button Records;
Button Logout;
Calculate = (Button) findViewById(R.id.buttonCalculate);
Records =(Button) findViewById(R.id.buttonRecords);
Logout = (Button) findViewById(R.id.buttonLogout);
Calculate.setOnClickListener(this);
Records.setOnClickListener(this);
Logout.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
public void onClick(View view)
{
Intent i = new Intent(this,Calculate.class);
startActivity(i);
Intent f = new Intent(this,Records.class);
startActivity(f);
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
}
}
Try this way:
public void onClick(View v)
{
switch(v.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
default:
break;
}
}
and make sure you have to added all the Activities in your manifest.xml file
try this..
public void onClick(View view)
{
Switch(view.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
}
}
Related
I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.
I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !
Here is my code from the main :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the StatsActivity.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_main);
Intent intent = getIntent();
float consumOn = intent.getFloatExtra("consumOn", 0);
float consumOff = intent.getFloatExtra("consumOff", 0);
EditText editTextA = (EditText)findViewById(R.id.editText1);
editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);
EditText editTextB = (EditText)findViewById(R.id.editText2);
editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
}
When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.
Replace the getFloatExtra with
Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");
I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
with
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
because you haven't actually defined their type anywhere and they aren't variables.
You should pass bundle with your floats to intent and only then extract them
I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_about:
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How can I refer to onListItemClick within onDialogPositiveClick? So the page does not open before/while the dialog box opens..Can I literally put onListItemClick in onDialogPositiveClick, or do I have to do something completely different? Here is my code...thanks for all/any help!
public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] sites = {"Google", "Amazon", "Ebay" , "Reddit", "SmashingMag", "CCC"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, R.id.textView1, sites);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
DialogFragment newFragment = new TheDialog();
newFragment.show(getFragmentManager(), "Confirm");
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}
}
#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 void onDialogPositiveClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
}
What you can do is not fire intent on list item click and move the switch code to
onDialogPositiveClick(DialogFragment dialog){
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}}
This way user will only go to the next page when positive button is clicked, hope you want this kind of behavior.
I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity
I have three buttons on my main page. Something weird happens when I try to click on them. For example, when I click on the NewGame button, it first displays what the scores button should display, and then if I click the back button it will proceed to display the activity that it was meant to. With the About button, I have to click back twice (it displays the newGame activity and the scores activity. Is there a reason why this is happening?
public class Sakurame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//set up click listeners for buttons
View HighScoreButton = findViewById(R.id.highscore_button);
HighScoreButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(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:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any)
}
return false;
}
public void onClick(View v){
switch(v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
//break;
// more buttons go here (if any)
}
}
Try adding break; after each startActivity within the onClick method.
Edit to clarify. This ensures that once the case has been met, the switch statement is broken from instead of moving on to the next case statement.
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
Intent i2 = new Intent(this, HighScore.class);
startActivity(i2);
break;
case R.id.highscore_button:
Intent i3 = new Intent(this, DisplayScores.class);
startActivity(i3);
break;