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;
Related
I am a beginner, I am a few days with this problem
I didn't find a solution. I have a menu that appears in an activity and when I click, I want it to open a new activity.
My question is, what to put in the activity with menu, and what to put in the new activity?
This is my code
Menu_chat.xml (my menu)
android:id="#+id/salva_vida"
android:icon="#drawable/salva_vida"
android:title="#string/save_life"
app:showAsAction="always" />
ChatActivity.java (this is the activity with menu)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
??????? (What put here?)------------------
break;
tab2.java (this is the new activity- I want to open this)
public class tab2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab2);
}
}
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
Your ChatActivity.java will look like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.salva_vida:
//Start Activity here
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);
break;
Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent);
#Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case android.R.id.home: onBackPressed(); return true; case R.id.salva_vida: //Start Activity here Intent mIntent = new Intent(this, tab2.class); startActivity(mIntent); break;
Don't forget to add your tab2 activity to manifest.
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 am a beginner android programmer ... I want to do something simple:
can I use these function and show my action bar in all the activities without copy these function in every activity's file?
thank you for help
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
Keep these in a parent activity class and extend it inside all your activities instead of extending Activity
Keep a parent activity which has these 2 common methods.
public class ParentActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
}
Extend the parent class in your activity.
public class YourActivity extends ParentActivity {
}
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);
}
}
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;
}
}