Android - Show/Hide Action Menu Bar in Fragments - android

This is my scenario: I have three fragments and I only want to show an action bar in one of those fragments. In the activity that creates the fragments, I currently have this code which correctly creates the menu bar:
#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_view_report, 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_view_report) {
Intent intent = new Intent(getApplicationContext(), ViewReportActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Then in one of the fragments of which I am trying to hide the menu bar I have this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_view_report).setVisible(false);
super.onPrepareOptionsMenu(menu);
}
Does anyone have any idea of what I am doing wrong or a suitable solution?
Thanks in advance

Related

How to dismiss overflow menu when home button is pressed android?

I am displaying overflow menu list when we click the three dots. When I press home button and again when I launch the app, overflow menu list is still displaying. How to dismiss overflow menu list window?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v("RAMKUMARV ONCREATEOPTION", "RAMKUMARV");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_settings);
item.setVisible(true);
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 have an activity field that stores the options/overflow menu whenever the onCreateOptionsMenu() gets triggered, and then use close() method to dismiss the menu when the home button is clicked i.e. in onUserLeaveHint().
public class MainActivity extends AppCompatActivity {
// Field to store the overflow menu
private Menu mOverflowMenu;
// omitted rest of your code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mOverflowMenu = menu;
// omitted rest of your code
return true;
}
// Dismissing the overflow menu on home button click
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
mOverflowMenu.close();
}
}
Declare the menu item like at MainActivity level, like this:
public class MainActivity extends AppCompatActivity {
private MenuItem overflowItem;
...
}
Now you should instanciate the object inside the onCreateOptionsMenu method, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
overflowItem = menu.findItem(R.id.action_settings);
overflowItem.setVisible(true);
return true;
}
Then you can use the onPause method to set the overflowItem not visible anymore:
#Override
public void onPause() {
super.onPause();
overflowItem.setVisible(false);
}
Finally remember to replace your MenuItem item object with overflowItem where it is used.
To dismiss the overflow menu you can simply call closeOptionsMenu() inside Activity.
closeOptionsMenu()
Progammatically closes the options menu. If the options menu is
already closed, this method does nothing.
You can call it in onPause() method like below:
#Override
public void onPause() {
super.onPause();
closeOptionsMenu();
}
or in case you want to call it only when the user presses the Home key you can use onUserLeaveHint() like below:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
closeOptionsMenu();
}

How make an app without menu? Android

I use this for change the menu:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_log_in, menu);
return true;
}
but...
If i want that my activity haven't menu, how make it?
I want make activities without menu because i don't have navigation in this scenes. How make it?
Remove these two methods in your Activity:
#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_del, 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);
}

Menu not displayed in android lollipop?

I created a new android project with the target being android Lollipop.
And I am testing the app on the device running lollipop LG G3. If I remove the title bar from the activity menu options are not displayed. Where in fact in the below versions the menu used to be visible at the the right bottom of the screen.
This is my code.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
#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);
}
}
Just needed to long press the task manager button and the menu options appears.

Android back option in action bar

When I created an activity it automatically added a back to main activity option in the action bar. It looks like those are the functions that do it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app_settings, 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);
}
Is there a way to make it go back using finish()? Right now it looks like it resets some values that are saved and I want to keep them. When I tried working with finish() it didn't do it but i'm not sure how to use it in the action bar.
This is simpler and it works perfect:
public class BackButtonExample extends Activity {
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.duahs);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0B4C5F")));
bar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home {
finish();
return true; }
else{
return super.onOptionsItemSelected(item);
}
}

Android Telephony System using Wireless Network

Error here is unreachable statement, but I have imported Sipmanager. Still I'm getting error.
public class MainActivity extends ActionBarActivity {
public SipManager mSipManager = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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(mSipManager == null){
mSipManager = SipManager.newInstance(this);
}
}
#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);
}
}

Categories

Resources