Menu Item To New Activity - java

I'm trying to send the user to a new activity using a menu button, and back again. To put it into context, I have a basic calculator that runs when the app is opened (works perfect right now), then I want the user to hit the menu button on his/her phone, (two buttons should pop up Home and Tip Calculator) when the user hits tip calculator it should then load the new activity TipCalculator. Then if the menu button is pressed again, the same two options should appear and the Home button should bring them back to the basic calculator.
This is my Code for the Calculator.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_calculator, menu);
menu.add(Menu.NONE,HOME_MENU_ID,Menu.NONE,"Home");
menu.add(Menu.NONE,SECOND_MENU_ID,Menu.NONE,"Tip Calculator");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case HOME_MENU_ID:
Intent main = new Intent(this,Calculator.class);
startActivity(main);
finish();
return true;
case SECOND_MENU_ID:
Intent second = new Intent(this,TipCalculator.class);
startActivity(second);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This is the code for the TipCalculator.java
public class TipCalculator extends Calculator{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tipcalculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_tipcalculator, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Intent main = new Intent(this,Calculator.class);
startActivity(main);
finish();
return super.onOptionsItemSelected(item);
}
}
The menu comes up fine, and the home button will refresh the home page, but the tip calculator button gives me a force close error and crashes on me.

Related

Activity not opening when clicking menu item

I am trying to tap on the "logout" option whose id is logout but nothing happens. Below is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
please help
in onCreateOptionsMenu method replace
return super.onCreateOptionsMenu(menu);
by
return true;
and replace super.onOptionsItemSelected(item); in onOptionsItemSelected() by return true
You have to return true in onCreateOptionsMenu() and in onOptionsItemSelected() after starting the activity.
The correct way would be like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
If you chain a overridden method to its superclass(i.e call super.someMethod()), it means you are requesting the superclass to handle the implementation on its own.
You should return true here to tell that you successfully inflated the menu in this child Activity OR succesfully handled the onClick() event on the option items.
NOTE: Mostly, you need to chain a superclass method call, where superclass is reponsible for doing some critical stuffs. For eg:
public class ChildActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
//request superclass to do its critical stuffs
super.onCreate(savedInstanceState);
//continue with your logics here
}
Restart Android studio. Let me know if that still persists

Tabbed Activity and Menu in Toolbar

I have a Tabbed Activity with 3 different tabs with fragments.
I have a Toolbar too and in this Toolbar i have 1 static menu object and 1 dynamic object.
I put the static object (bluetooth connection) into MainActivity that contains Fragments loaded into a container and the dynamic button into the Fragment that need that button.
The problem is that if i declare the method of menu into MainActivity, the methods declared into fragments are useless and when i press the icon nothing happends....
But if i delete the method that manage menu into MainActivity now the buttons into fragments works...
There is some way to around this thing?
Or the only way is to repeat to every fragments the static button and for each fragment add the specific buttons that other fragments don't need?
MainActivity menu method:
#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.
switch (item.getItemId()) {
case R.id.action_bluetooth:
if (mBluetoothService == null) {
Log.d(TAG, "menu Bluetooth button");
setupComunication();
} else {
mBluetoothService = null;
setupComunication();
}
// Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
//findDevice();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Fragment menu method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_dashboard, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#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.
switch (item.getItemId()) {
case R.id.action_commands:
// User chose the "Settings" item, show the app settings UI...
Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivityForResult(settingsIntent, RESULT_SETTINGS);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Override these two methods in your Fragment, add items of menu_dashboard to menu_main and set visibility of menuItem to false by default:
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_commands).setVisible(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.
switch (item.getItemId()) {
case R.id.action_commands:
// User chose the "Settings" item, show the app settings UI...
Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivityForResult(settingsIntent, RESULT_SETTINGS);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

error while programming for a button in initial level

I am new in android programming, having error in the given code which are: Multiple markers at this line
-Button1 cannot be resolved to a variable
-Syntax error on token "#", class expected
-id cannot be resolved to a variable
-Line breakpoint:MainActivity [line: 22] - onCreate(Bundle)
public class MainActivity extends Activity {
Button aButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.Button_1);
aButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
aButton.setText("Submitted");
}});
}
#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);
}
}
1.u must have activity_main and it should include Button with Button_1
2.check .JavaClassName added in Manifest file

Acessing the 'options' menu from a fragment? Android

I currently have an app which has 4 pages, these pages are being added using fragments by a pageViewer, so that I can swipe between them.
What I want, is to be able to access the 'options' menu (the one which has a 'settings' item in it by default) from each page, so that I can perform commands specifically for one page, such as a 'refresh' item, which refreshes the data in the current page.
Could anyone point me in the right direction?
Thanks!
Each Fragment can declare its own menu, which will be automatically merged with the current activity's menu.
class YourPage extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Signal that this fragment has proper actions
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
// The menu will be added to the action bar
inflater.inflate(R.menu.fragment_page_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_refresh:
{
// Handle the action...
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
Here's a complete example that I made, and also a relevant tutorial.

Make android app backround change when button is pushed, and is on value true or false

In my android app I have a button named "button1". When you click the button a boolean gets either true or false and displays some text appropriately for either true or false.
What I would like to have it do is along with change the text I would like it to change the background based on whether the value after you click the button is true or false.
I have tried anything and everything I can think of so any suggestions and help would be appreciated.
Below is the code without any modifications to try and make the background change.
public class MainActivity extends Activity {
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calculate(View v){
EditText number1text=(EditText)findViewById(R.id.num1text);
String num1=(number1text.getText().toString());
String ans=num1;
TextView answer=(TextView)findViewById(R.id.answer);
boolean value = r.nextBoolean();
if(value==true){
answer.setTextColor(Color.GREEN);
answer.setText("It is a cause for celebration! So CELEBRATE!");}
if(value==false){
answer.setTextColor(Color.RED);
answer.setText("Exciting, but not a cause for celebration!");}
}
#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;
}
}
If you want to change background of your activity then try this,
View root = findViewById(R.id.main);
root.setBackgroundColor(getResources().getColor(R.color.red));
so your full activity code would look something like this
public class MainActivity extends Activity {
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = findViewById(R.id.main);
}
public void calculate(View v){
EditText number1text=(EditText)findViewById(R.id.num1text);
String num1=(number1text.getText().toString());
String ans=num1;
TextView answer=(TextView)findViewById(R.id.answer);
boolean value = r.nextBoolean();
if(value==true){
answer.setTextColor(Color.GREEN);
root.setBackgroundColor(getResources().getColor(R.color.red));
answer.setText("It is a cause for celebration! So CELEBRATE!");}
else{
root.setBackgroundColor(getResources().getColor(R.color.green));
answer.setTextColor(Color.RED);
answer.setText("Exciting, but not a cause for celebration!");}
}
#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;
}
}

Categories