I am beginner in android and try to make an app that popup a menu when imageView is clicked. Actually it works but popup at right side and I want to be at center.
So, I searched for it on web and come across ListPopupWindow, PopupWindow classes. I tried various methods of this classes as per my knowledge/ability but I am not able to achieve this.
Guide me to do that. Here is my code
imageview setonclickListener(new View.onclickListener() {
#Override public void onClick(View view){
PopupMenu popup = new PopupMenu(Info4 Activity.this, imageview1);
Menu menu = popup.getMenu();
for (int i = 0; i < (int) (subjects.size()); i++) {
val = subjects.get((int) (i));
menu.add(val);
}
popup.show();
}
});
PopupMenu:
The popup will appear below the anchor if there is room, or above it if there is not. In your situation the anchor is imageView, so you can not center the popup in screen.
PopupWindow:
Try this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);// here v is any View only needed for WindowToken
}
});
Other better ways:
May be you could be think about using DialogFragment, which is customizable and flexible.
Related
The overflow actions menu looks exactly like what I need, but it seems to only work within action bars. However, I'd like this kind of three-dots-menu on the right side of each element of a list view.
Is there a built-in view that can be used for this?
You can use an ImageView and a PopupMenu.
Context context = ...
ImageView overflow = ...
PopupMenu popup = new PopupMenu(context, overflow);
popup.inflate(R.menu.your_menu_resource);
popup.setOnMenuItemClickListener(...);
overflow.setOnClickListener(new View.OnClickListener() {
#Override
public final void onClick(View v) {
popup.show();
}
}
overflow.setOnTouchListener(popup.getDragToOpenListener());
I'm trying to change the style attribute "colorControlNormal" of my app programmatically and during runtime, but I didn't have any results.
This property is the color that will tint the hamburger & back icons of the new Toolbar viewGroup. Beside, I'm using the v7 compatibility library.
I heard that we cannot change app theme during runtime, but I'm looking for an answer, even if it's not so clean way.
Edit:
I just figured that gmail is doing what i want, when you click on the search icon, the white hamburger icon turn into grey back.
Waiting for more.
I spent one day, played with different implementation. So my opinion, the best way todo that it copy paste DrawerArrowDrawable from AppCompat v7 library.
https://gist.github.com/IstiN/5d542355935fd7f0f357 - take a look on the code with some optimization
than you can use it in your main activity with code below
DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this);
ImageView menuButton = (ImageView) findViewById(R.id.arrow);
menuButton.setImageDrawable(drawable);
menuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((DrawerLayout)findViewById(R.id.drawer)).openDrawer(Gravity.START);
}
});
when you start new fragment, you need to create one more view on the same place and add second code to your fragment
private DrawerArrowDrawable mArrowDrawable;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mArrowDrawable = new DrawerArrowDrawable(getActivity(), getActivity());
ImageView topButton = (ImageView) view.findViewById(R.id.arrow);
topButton.setImageDrawable(mArrowDrawable);
topButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeSearch();
}
});
//run animation from hamburger to arrow
animate(0, 1, null);
....
private void animate(int startValue, int endvalue, Animator.AnimatorListener listener) {
ValueAnimator anim = ValueAnimator.ofFloat(startValue, endvalue);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
mArrowDrawable.setProgress(slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(300);
if (listener != null) {
anim.addListener(listener);
}
anim.start();
}
to make animation from arrow to hamburger handle back button and execute code
animate(1, 0, null);
you also need to wait in your fragment while animation will not finish, but it another questions.
If you have any questions ask in comments.
I would like that when uses touches on a spinner, instead of having items drop down below the spinner, the items where in the center of screen with title, like it was in older versions of Android, just like this:
I use API 19. What should I do?
Later I will have to implement multi-selection and I think this way UX is better.
Easy and simple: create a Dialog with a RadioGroup.
You dont even need a Spinner, you just need a View with an OnItemClickListener:
spinner.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Dialog dialog = new Dialog(YourActivity.this);
dialog.setContentView(R.layout.your_dialog_layout);
RadioGroup radioGroup = (RadioGroup) dialog.findViewById(R.id.your_radiogroup);
for(int i=0; i<radioGroup.getChildCount(); ++i){
RadioButton radio = (RadioButton) ((ViewGroup) radioGroup).getChildAt(i);
if(radio.isChecked()) {
// do something with the selection
}
}
dialog.show();
}
});
I am dealing with a view flipper. I have 2 views in my view flipper and in the second view on completing a frame animation an animated popup menu translating from bottom. when I press the back button I could able to flip to first view from second but again when I switch to the second view from first view that popup menu is not disappearing. I used reset() and setfillafter() methods but no result
How to solve this? any Idea?
Here is my code.
final Animation popup = new TranslateAnimation(0, 0, 200, 0);
popup.setDuration(20000);
popup.setFillAfter(true);
hearttap.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
final RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
final ImageView ekgimgview4 = (ImageView) findViewById(R.id.ekgimgview4);
ekgimgview4.setVisibility(ImageView.VISIBLE);
ekgimgview4.setBackgroundResource(R.anim.ekgtimer);
AnimationDrawable ekgframeAnimation4 = (AnimationDrawable) ekgimgview4
.getBackground();
if (ekgframeAnimation4.isRunning()) {
findViewById(R.id.ekgimgview4).postDelayed(new Runnable() {
public void run() {
// openOptionsMenu();
popuplayout.startAnimation(popup);
popup.setFillAfter(true);
popup.setStartTime(30000);
ekgimgview4.setVisibility(view.GONE);
}
}, 30000);
final Button ekgbutton = (Button) findViewById(R.id.ekgbutton);
ekgbutton.setOnClickListener(new View.OnClickListener() {
public void onClick( View view) {
RelativeLayout popuplayout = (RelativeLayout) findViewById(R.id.popuplayout);
popuplayout.setVisibility(View.INVISIBLE);
}
});
You need to use the dismiss() function for popups. So use the following code whenever you transition between Views:
popup.dismiss();
I would edit it into your code myself, but the way StackOverflow handles displaying code tags made your code half into HTML code tags and half not.
The onClick never fires! Why not? Please help me.
for(int i = 0; i < 12; i++) {
String title = "Button" + i;
Button sliderButton = new Button(this);
sliderButton.setText(title);
glideMenuTray.addView(sliderButton,100,40);
sliderButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("gm", "Tapped ");
}
});
}
I'm no expert at stuff like this, but it's probably something to do with garbage collection, and the OnClickListeners passing out of scope.
Though I don't think you can use the super-easy approach to onClickListeners that Dimitar mentions, you can probably use the middle approach that the section he links to discusses, even though it's not a new approach. To repeat the example code here, it's:
View.OnClickListener handler = View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.myButton: // doStuff
break;
case R.id.myOtherButton: // doStuff
break;
}
}
}
findViewById(R.id.myButton).setOnClickListener(handler);
findViewById(R.id.myOtherButton).setOnClickListener(handler);
If the only thing distinguishing the buttons is their title text, well, you could use that to distinguish between them in the master onClick method.
Also, not shure, I once had a problem like that on a TextView and it was because I didnt add setClickable(true)
My code was something like
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText("***");
text.setClickable(true);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//My action
}
});
myViewGroup.addView(text );
Hope this helps
If you are using Donut or Eclair, you can use common click listener, registered in your Activity and hooked with your buttons in the layout XML.
For reference, look here, the category Easier click listeners.
Am I right in assuming that the following line:
glideMenuTray.addView(sliderButton,100,40);
Adds the view to the coords x:100,y:40 onto some View extending ViewGroup?
In that case you are stacking 12 buttons on top of each other, only the last Button (labeled Button11) will be visible (and clickable).
And provided that the question is 3 years old I really hope you already resolved this by now :)
set the setOnClickListener before adding the view.
for(int i = 0; i < 12; i++) {
String title = "Button" + i;
Button sliderButton = new Button(this);
sliderButton.setText(title);
sliderButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("gm", "Tapped ");
}
glideMenuTray.addView(sliderButton,100,40);
}