TapTargetView is placed behind popup menu - android

I created TapTargetView (or TapTargetSequence) in all of my activities and it displaying well, but for inner views of popup menu, it hides behind my popup menu window! exactly between under activity and popup menu. how can I bring it to top, please? Is parameter of TapTargetSequence ((Activity) context) correct? "context" or "this" occurs error !
public class Popup_Menu implements OnClickListener {
PopupWindow popup; View layout; Context context;
WindowManager wm;
void showPopup(final Activity context) {
this.context=context;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout, Gravity.BOTTOM, 30 ,0 );
tapTarget_menu();
} /////////////////// close of showPopup() !
// TapTaget_menu
public void tapTarget_menu() {
new TapTargetSequence((Activity) context)
.targets(
TapTarget.forView(layout.findViewById(R.id.chkb_menu_remem), "remember", "last lesson")
// first target
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_kmrng)
.dimColor(R.color.sabz_seyedi)
.titleTextSize(22)
.descriptionTextSize(16)
.textColor(R.color.white)
.drawShadow(true)
.transparentTarget(true)
.cancelable(false)
.targetRadius(60),
// second target
TapTarget.forView(layout.findViewById(R.id.ll_call_menu), "call", "connecting friends!")
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_tireh)
.dimColor(R.color.sabz_seyedi)
.titleTextSize(22)
.descriptionTextSize(16)
.textColor(R.color.white)
.drawShadow(true)
.transparentTarget(true)
.cancelable(false)
.targetRadius(60)
).start();
}
}

I found my solution ! I have changed this code after popup.showAtLocation(), like following and it can display tapTargetView on top of PopupWindow.
VIVA ME !!!
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
layoutParams.packageName = context.getPackageName();
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
then, out of showPopup() method:
public void tapTarget_menu1(){
TapTarget target = TapTarget.forView(layout.findViewById(R.id.ll_call_menu), "remember", "last lesson");
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_tireh)
.titleTextSize(24)
.descriptionTextSize(18)
.textColor(R.color.black)
.drawShadow(true)
.cancelable(false)
.transparentTarget(true)
.targetRadius(60);
content = (ViewGroup) layout.findViewById(android.R.id.content);
final TapTargetView tapTarget_menu1 = new TapTargetView(context, wm, content, target, new TapTargetView.Listener(){
#Override
public void onTargetClick(TapTargetView view) {
tapTarget_menu2();
super.onTargetClick(view);
}
});
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).addView(tapTarget_menu1, layoutParams);
}

Related

How to adjust popup menu when pop outside container

I am using a custom popup menu that pops inside a RecycleView container.
When the view touched is the on at the bottom, the popup menu hides the controls bellow the RecycleView.
I want to adjust the popup position in this case, bringing the popup up the exact distance needed so the menu keeps inside the container.
This should be easy, but i am having difficulties getting the coordinates and applying the calculations from inside the Adapter that deals with clicks on items.
Here is what i managed so far:
void show(FragmentActivity activity, View touchedView, DataItem item) {
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
bind(popupWindow, popupView, item);
//draws the menu perfectly bellow the touched element,but doesn't take in account the parent view area
popupWindow.showAsDropDown(touchView);
}
This is what I did:
private void showPopupMenu(final View view, final int position) {
// inflate menu
final PopupMenu popup = new PopupMenu(view.getContext(), view);
popup.setGravity(Gravity.END);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
final EditText editText = (EditText) promptView.findViewById(R.id.input_text);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
And here is the outcome:
Found out. 2 Things were on the way and are explained in comments.
void show(FragmentActivity activity, View touchView, IDataItem dataItem) {
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
bind(popupWindow, popupView, dataItem);
//1º problem: View item is inside ViewHolder.Item, so container is 2 levels up and touched item one level up.
View container = (View) touchView.getParent().getParent();
View item = (View) touchView.getParent();
int containerHeight = container.getHeight();
//2º problem: to get size before the popup view is displayed: ref:https://stackoverflow.com/a/15862282/2700303
popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int yOff=0;
//if popup view will draw pass the bottom, get the amount needed to adjust the y coordinate
if (ContainerHeight < item.getHeight() + item.getTop() + popupView.getMeasuredHeight())
yOff = ContainerHeight- (item.getTop() + item.getHeight() + popupView.getMeasuredHeight());
popupWindow.showAsDropDown(touchView,0,yOff);
}

Add View into RecyclerView

I tried to add view into RecyclerView when click to Button in a ViewHolder.
When click to Button in ViewHolder3, a View (view add more) will add and appear like image above.
ViewAddMore will pin there and RecyclerView can scroll normal.
I tried but don't found any solution for my issue;
Have any suggest for my issue?
Please use PopupWindow to show this View.
// get a reference to the already created main layout
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_main_layout);
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
https://developer.android.com/reference/android/widget/PopupWindow.html
How to make a simple android popup window?
Hope this will help you.

move image from outside left screen to outside right screen Android

I have a cutstom DialogFragment to show a loading message to the user. For the loading message image, I created an animation that goes from the left part of the screen (starting from outside the screen) to the right of the screen (finishing outside the screen). To do so, I thought of starting the animation on y=-imageWith and finish in y=imageWith+dialogWith:
public class MensajeDialogFragment extends DialogFragment {
TextView mTvMensaje;
TextView mTvTitulo;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
ImageView loadingImage = (ImageView) dialogView.findViewById(R.id.ivBus);
View dialogView = inflater.inflate(R.layout.layout_mensaje_dialog, null);
TextView mTvTitulo = (TextView) dialogView.findViewById(R.id.tvTitulo);
TextView mTvMensaje = (TextView) dialogView.findViewById(R.id.tvMensaje);
mTvTitulo.setText(getArguments().getString(getString(R.string.bundle_titulo), ""));
mTvMensaje.setText(getArguments().getString(getString(R.string.bundle_mensaje), ""));
builder.setView(dialogView);
TranslateAnimation izqADerAnimacion = new TranslateAnimation(-loadingImage.getWidth(), dialogView.getWidth()+loadingImage.getWidth(), 0, 0);
izqADerAnimacion.setDuration(3500);
izqADerAnimacion.setRepeatCount(Animation.INFINITE);
loadingImage.startAnimation(izqADerAnimacion);
return builder.create();
}
}
But for some reason, the loadingImage.getWidth() and dialogView.getWidth() are returning 0. What can I do to solve this without hardcoding the starting and finishing positions?
loadingImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT > 16)
loadingImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
loadingImage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//do animation
}
});
hope this works
TranslateAnimation izqADerAnimacion =
new TranslateAnimation(-loadingImage.getWidth(),
dialogView.getWidth()+loadingImage.getWidth(), 0, 0);
In your code above mentioned you can directly give some value like
TranslateAnimation animation = new TranslateAnimation(-970.0f, 2000.0f, 0.0f, 0.0f);
It works nicely in Nexus 9 tab.

Display a popup at the left edge of a view

I am using a custom popup in a list view, this popup needs to be drawn half way through the height of triggerview and on the left side of the triggerview.
I am able to achieve the first part, second seems elusive, here is my code:
public class QuickAction implements View.OnTouchListener {
private View triggerView;
private PopupWindow window;
protected final WindowManager windowManager;
public QuickAction(View triggerView) {
this.triggerView = triggerView;
window = new PopupWindow(triggerView.getContext());
window.setTouchable(true);
window.setTouchInterceptor(this);
windowManager = (WindowManager) triggerView.getContext().getSystemService(Context.WINDOW_SERVICE);
ImageView imageView = new ImageView(triggerView.getContext());
imageView.setImageResource(R.drawable.ic_action_email);
LayoutInflater layoutInflater = (LayoutInflater) triggerView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.quick_action_popup_layout, null, false);
window.setContentView(layout);
// window.setBackgroundDrawable(null);
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(false);
window.setOutsideTouchable(true);
}
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
this.window.dismiss();
return true;
}
return false;
}
public void show() {
int[] location = new int[2];
triggerView.getLocationOnScreen(location);
window.showAtLocation(triggerView, Gravity.NO_GRAVITY, location[0] + (triggerView.getLeft()), location[1] + (triggerView.getHeight() / 2));
}
}
What I want is to align the right edge of my popup window to the left egde of my triggerview
Triggerview is my button where I want to generate the popup, the relevant parts of code being:
public void show() {
int[] location = new int[2];
triggerView.getLocationOnScreen(location);
window.showAtLocation(triggerView, Gravity.NO_GRAVITY, location[0] + (triggerView.getLeft()), location[1] + (triggerView.getHeight() / 2));
}
However this does nto give me the desired result. Any ideas?
Specifying a gravity of NO_GRAVITY is similar to specifying
Gravity.LEFT | Gravity.TOP.
If you want to place the popup window's right side to your specified location you need Gravity.TOP | Gravity.RIGHT instead of Gravity.NO_GRAVITY.

Setting the size of a DialogFragment

I have been trying many commands to setup the size of my DialogFragment. It only contains a color-picker, so I have removed the background and title of the dialog:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
However I also want to position the dialog where I want and it is problematic. I use:
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.LEFT;
getDialog().getWindow().setAttributes(params);
But one (big) obstacle remains: even though my dialog pane is invisible, it still has a certain size, and it limits the positions of my dialog. The LayoutParams.WRAP_CONTENT are here to limit the size of this pane to my color-picker, but for some reason it does not work.
Has anyone been able to do something similar?
i met a similar question that is you can't set the dialogFragment's width an height in code,after several try ,i found a solution;
here is steps to custom DialogFragment:
1.inflate custom view from xml on method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
View view = inflater.inflate(R.layout.XXX,
container, false);
//TODO:findViewById, etc
return view;
}
2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(width, height);
window.setGravity(Gravity.CENTER);
//TODO:
}
After some trial and error, I have found the solution.
here is the implementation of my DialogFragment class :
public class ColorDialogFragment extends SherlockDialogFragment {
public ColorDialogFragment() {
//You need to provide a default constructor
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_color_picker, container);
// R.layout.dialog_color_picker is the custom layout of my dialog
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.LEFT;
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle);
// this setStyle is VERY important.
// STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
// so for example the size of the default dialog will not get in my way
// the style extends the default one. see bellow.
}
}
R.style.colorPickerStyle corresponds to :
<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:cacheColorHint">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I simply extend a default Dialog style with my needs.
Finally, you can invoke this dialog with :
private void showDialog() {
ColorDialogFragment newFragment = new ColorDialogFragment();
newFragment.show(getSupportFragmentManager(), "colorPicker");
}
For my use case, I wanted the DialogFragment to match the size of a list of items. The fragment view is a RecyclerView in a layout called fragment_sound_picker. I added a wrapper RelativeLayout around the RecyclerView.
I had already set the individual list item view's height with R.attr.listItemPreferredHeight, in a layout called item_sound_choice.
The DialogFragment obtains a LayoutParams instance from the inflated View's RecyclerView, tweaks the LayoutParams height to a multiple of the list length, and applies the modified LayoutParams to the inflated parent View.
The result is that the DialogFragment perfectly wraps the short list of choices. It includes the window title and Cancel/OK buttons.
Here's the setup in the DialogFragment:
// SoundPicker.java
// extends DialogFragment
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title));
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound);
List<SoundItem> items = getArguments().getParcelableArrayList(SOUND_ITEMS);
soundPickerAdapter.setSoundItems(items);
soundPickerAdapter.setRecyclerView(rv);
rv.setAdapter(soundPickerAdapter);
// Here's the LayoutParams setup
ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = getListItemHeight() * (items.size() + 1);
view.setLayoutParams(layoutParams);
builder.setView(view);
builder.setCancelable(true);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
// ...
});
builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() {
// ...
});
return builder.create();
}
#Override
public void onResume() {
Window window = getDialog().getWindow();
window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
super.onResume();
}
private int getListItemHeight() {
TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true);
DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (int) typedValue.getDimension(metrics);
}
Here is fragment_sound_picker:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_sound_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
use this code for resize of Dialog Fragment android
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(250, 100);
window.setGravity(Gravity.RIGHT);
}

Categories

Resources