Tabbed Activity and Menu in Toolbar - java

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);
}
}

Related

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

Android studio: An error has occured

I'm working on programming a android application. But as soon as I would like to test my application in my simulator, I get an instant error.
This is my code.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button toPech = (Button)findViewById(R.id.toPech);
toPech.setOnClickListener(this);
ImageButton toInfo = (ImageButton)findViewById(R.id.toInfo);
toInfo.setOnClickListener(this);
//this button is not in the same layoutactivity as the other 2.
ImageButton backPech = (ImageButton)findViewById(R.id.backPech);
backPech.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.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);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.toPech:
startActivity(new Intent(getApplicationContext(), LocationActivity.class));
break;
case R.id.toInfo:
startActivity(new Intent(getApplicationContext(), InfoActivity.class));
break;
case R.id.backPech:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
break;
}
}
I would like to know what is wrong so that I can.
From your commented line:
//this button is not in the same layoutactivity as the other 2.
I think you have a NullPointerException.
While the Button "backPech" isn't in activity_main.xml, so calling findViewById(R.id.backPech); will return a null object ( backPech will be null).
And calling setOnClickListener(this); to a null object will cause the NullPointerException.

Whenever I try to add a button to my actionbar, the items are added under the overflow menu

I am trying to add a simple button to my action bar, but when I try to use the following code, the items are added as items under the overflow menu, not on the actionbar itself. Any help would be appreciated,
Jacob
Here is my Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_review, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/seda"
android:icon="#drawable/redpinreal"
android:title="Hello Worldh"
android:showAsAction="withText|always"/>
</menu>
I also have this enabled:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Part of your code (android:showAsAction, instead of yourapp:showAsAction) is attempting to use the native action bar. Part of your code (getSupportActionBar(), instead of getActionBar()) is attempting to use the appcompat-v7 action bar backport. Choose one and stick with it.

Android action overflow drawn on top of ActionBar

In order to have the ActionBar in my application, my main activity extends from android.support.v7.app.ActionBarActivity. This is how it looks:
However, when I click in the overflow action button, the options show up over the ActionBar:
I wanted to show the overflow menu under the ActionBar, as it is supposed to. How can I solve this?
I'm running Android 4.4 (KITKAT). If you need any more details, just ask. Thanks.
Here is the code of my main activity:
public class SendMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_message);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_action_bar_icon);
getSupportActionBar().setDisplayUseLogoEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.activitySendMessage, new SendMessageFragment())
.commit();
}
}
#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_activity_send_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.actionSettings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.actionShowHistory) {
startActivity(new Intent(this, SentMessagesHistoryActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}

Menu Item To New Activity

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.

Categories