Show context menu from code behind - android

This might be a simple question, but I've been looking around and can't find the answer. Is there any code to show the context menu on Android from a code, instead of pressing the menu button? E.g. when I touch the screen then it'll call the context menu?

Call openContextMenu() on your Activity whenever you want to open it. Note that this is a rather unusual UI pattern, one that your users may not expect.

OnClickListener onClick_Show_Contextmenu = new OnClickListener() {
#Override
public void onClick(View v) {
((Activity) context).openContextMenu(v);
}
};
findViewById(R.id.xxx).setOnClickListener(onClick_Show_Contextmenu);
registerForContextMenu(findViewById(R.id.xxx));
findViewById(R.id.xxx).setLongClickable(false);

you can use any of the following:
openContextMenu as shown here:
registerForContextMenu(view);
openContextMenu(view);
unregisterForContextMenu(view);
setOnCreateContextMenuListener
showContextMenuForChild

You can use
view.showContextMenu();
on your view.

Related

Programmatically show Layout to preface an activity

What I want to do:
I want to have multiple activities each prefaced with a page explaining to the user what the activity is about.
What I'm currently doing:
So my main class BaseModuleActivity extends Activity and I am trying to write a function called showTutorial() which will explain the next steps to the users.
Here is my attempt in doing so:
public void showTutorial(String title, String explanation){
setContentView(R.layout.tutorial_screen);
TextView tv1 = (TextView)findViewById(R.id.tutoTextTitle);
tv1.setText(title);
TextView tv2 = (TextView)findViewById(R.id.tutoTextExplanation);
tv2.setText(explanation);
findViewById(R.id.tutoButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//remove the tutorial's view
findViewById(R.id.tutoLayout).setVisibility(View.GONE);
}
});
}
And this method is called in the following:
public class myFirstActivity extends BaseModuleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//First show tuto
super.showTutorial(getString(R.string.upTitle),getString(R.string.upExplanation));
//TODO then actually do the activity stuff
/*
(findViewById(R.id.next_button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
*/
}
}
Problem:
I think the problem is mainly conceptual. I don't know the best approach to do this and the approach I'm taking is not working.
What I'm doing is not working because the view just become empty. I thought setting the visibility of the linearLayout to gone would make it disappear and the actual activity will able to take place.
What I need:
I need to understand if I can do what I want with my approach or not? Or what approach should I take.
I found some similar questions. However, the answer to these questions didn't seem to fit my problem.
I also looked into layout inflater and fragment, but layout inflater seem to be more for listView and fragment uses layout inflater.
Well, there are some approaches to show a guide for your activity (or application).
First one, and probably the easiest, is to show a dialog/TextView when user enters an activity and explain the activity guide in that dialog/TextView using plain text. From your explanation, I think this one is what your are trying to do.
Second one is to use something like slides with pictures to explain about your activity (like Google Sheets application).
Third one is to explain each control in your activity separatly by highlighting them (similar to how Go Launcher explains its feature on first launch)
You can find more info in below links:
How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
Android - first launch interactive tutorial
Seems that what you want is actually an introduction. Take a look at this project:
https://github.com/rubengees/introduction
From each introduction page you can launch the correspondent activity.

Context menu for a button using android?

I have a button that when pressed, will call the company. Now, I was doing some research and found that there is a way to include a context menu. I really like the context menu because it gives you so many options.
Do you think it would be a waste of code to set a context menu for a click of the button that when pressed will open up the options to add contact, call contact, sms contact, etc.? Is it necessary?
I did come across these:
Android opening context menu after button click
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#tour_of_the_menus
I think it would be a good feature to include. Thats what context menu is there for, to give more options. I think it would be good to give the user more options when the button is clicked. Well it makes more since anyway.
Heres how you would get the long click
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
EDIT:
If you just want one click on the button just register its click listener like this..
downSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
It would be nice to provide a big main button to call the number, and some additional mechanism, let's say a smaller + button to do more stuff with as you sugggested. Also a long click could be considered a right user interaction to provide with more features.
Just a user feeling...

Why is there no getOnClickListener in the Button class? (Android)

Why is there no getOnClickListener in the Button class? I think this is really strange considering there is a getOnFocusChangeListener function. Why make it for the FocusChangeListener and not for the ClickListener?
Added comment:
For those below that are wondering why I need this: We are developing a large application with a lot of viewgroups on the screen. I want to add some code to a button on the screen but not replace the complete OnClickListener. I want to implement a new OnClickListener that will run some code and call the old OnClickListener. But for that I need to retrieve the old one.
I don't know why there is not, but you can do what you want to do by extending the button class:
public class Button extends android.widget.Button implements OnClickListener {
public void onClick(View v) {
/* Your code here...*/
super().onClick(v);
}
}
I think it's a question to Google :D
Why do you need to get a onClickListener back? If you are so desperate, store it in a tag (Views.setTag(...));

Which is better in implementing click listener?

Which is a good practice in implementing click listener and why? Or is there a better way other than the two? Thanks.
First :
sampleButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// do something
}
});
Second : implement OnClickListener then override onClick method?
The third option is to set the listener directly in your XML layout:
android:onClick="myClickHandler"
and then implement it in your Activity:
public void myClickHandler(View v){
// do something
}
You're technically doing the 2nd thing with the 1st one. The 1st case uses whats called an anonymous class which implements OnClickListener, but since is anonymous, doesn't have a class name and isn't editable from external classes. Explicitably implementing OnClickListener is useful if you expect to use the same onClick functionality in multiple different locations, or if the click code is long
The first approach is used when you want to perform the action only for a particular case, if many click events require the same action then use the second one.

Preventing a View from showing two Context Menus

In my Android application I have a Button which opens up a context menu when clicked. The issue is that if a user clicks quickly, they can open multiple instances of the menu.
b.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View view ) {
// popup options
view.showContextMenu();
}
} );
How can I prevent the user from opening up more than one copy? I am looking for a 'boolean' like checking the Visible status, but can't seem to find anything. My hope was that there was a function somehow that would result in code similar to:
if (context menu is not open)
open context menu
else
don't do anything
I really don't like this UI pattern. It's this sort of thing that cause iOS developers (and users) to think that Android developers lack discipline. Context menus are for long-presses, period. Use something else, like an AlertDialog or PopupMenu, elsewhere.
That being said, set a boolean flag when you show the context menu, checking it first to prevent duplicate menus. Clear the flag in onContextMenuClosed().

Categories

Resources