I'm a noob to android development and I am trying to figure out how to display the NewQuickAction3D popup dialog at a specific coordinate in a view. I am integrating the popup with this tutorial. Essentially, I want to use the popup dialog to display data where the users touch instead painting on the canvas using "infoview" Currently, the popup displays at the top¢er of the view that I anchor it to. How can i make it display a particular coordinate? Any help is greatly appreciated.
MY CODE
public void updateMsg(String t_info, float t_x, float t_y, int t_c){
infoView.updateInfo(t_info, t_x, t_y, t_c); //Infoview paints to on a specific coordinate
quickAction.show(infoView); //How do I use the t_x & t_y coordinates here instead of just anchoring infoview
EDIT
public void updateMsg(String t_info, float t_x, float t_y, int t_c){
infoView.updateInfo(t_info, t_x, t_y, t_c);
WindowManager.LayoutParams wmlp = quickAction.getWindow().getAttributes(); //Error here getting window attributes
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
quickAction.show(infoView);
}
Override onTouch() of your view
AlertDialog dialog;
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showDialog(); // display dialog
break;
case MotionEvent.ACTION_MOVE:
if(dialog!=null)
dialog.dismiss();
// do something
break;
case MotionEvent.ACTION_UP:
// do somethig
break;
}
return true;
}
public void showDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(FingerPaintActivity.this);
dialog = builder.create();
dialog.setTitle("my dialog");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
dialog.show();
}
Even to draw user touches the screen then also dialog is displayed. so dismiss dialog in on move.
Related
I am trying to get the view position (Right upper corner position and left bottom corner position) and display custom dialog right or left side of view but i am not getting exact position of view. This is my code
int x=view.getLeft()+ (view.getHeight()*4);
int y= view.getBottom()+(view.getWidth()*2);
showDialog(x,y,Gravity.TOP,Gravity.LEFT);`
public void showDialog(int x, int y,int gravity_y,int gravity_x){
final Dialog dialog = new Dialog(this, R.style.ActivityDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
dialog.setContentView(R.layout.cofirm_sell);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
dialog.getWindow().getAttributes().windowAnimations=R.style.DialogTheme;
wmlp.gravity = gravity_y | gravity_x;
wmlp.x = x;
wmlp.y = y;
ImageButton close=(ImageButton)dialog.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
I want this
Check this:
int[] outLocation = new int[2];
view.getLocationOnScreen(outLocation);
Rect rect = new Rect(
outLocation[0],
outLocation[1],
outLocation[0] + view.getWidth(),
outLocation[1] + view.getHeight());
//right upper corner rect.right,rect.top
//left bottom corner rect.left,rect.bottom
Try This
int[] location = new int[2];
view.getLocationOnScreen(location);
int x1=location[0]-(view.getWidth());
int y1= location[1]-(view.getHeight());
Thanks to #Sree, in Kotlin:
val location = IntArray(2)
view.getLocationInWindow(location)
val left = location[0]
val right = left + view.width
val top = location[1]
val bottom = top + view.height
I have spend like 2 hours,
I'm unable to figure out what is the issue in service. I'm not calling stopService() or stopSelf from anywhere else. Below is the code ,
public class FloatingViewService extends Service {
private WindowManager mWindowManager;
private View mFloatingView;
WindowManager.LayoutParams params,landscapeParams,nonTouchableParams;
public FloatingViewService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
//Add the view to the window.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window.
landscapeParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
landscapeParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
landscapeParams.x = 0;
landscapeParams.y = 100;
//Add the view to the window.
nonTouchableParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
nonTouchableParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
nonTouchableParams.x = 0;
nonTouchableParams.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
//The root element of the collapsed view layout
final View collapsedView = mFloatingView.findViewById(R.id.collapse_view);
//The root element of the expanded view layout
final View expandedView = mFloatingView.findViewById(R.id.expanded_container);
//Set the close button
ImageView closeButtonCollapsed = (ImageView) mFloatingView.findViewById(R.id.close_btn);
closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});
//Set the close button
ImageView closeButton = (ImageView) mFloatingView.findViewById(R.id.close_button);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
collapsedView.setVisibility(View.VISIBLE);
expandedView.setVisibility(View.GONE);
}
});
//Set the close button
ImageView lock = (ImageView) mFloatingView.findViewById(R.id.lock_button);
lock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, nonTouchableParams);
}
});
//Set the close button
ImageView expand = (ImageView) mFloatingView.findViewById(R.id.expand);
expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, landscapeParams);
}
});
//Drag and move floating view using user's touch action.
mFloatingView.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
if (isViewCollapsed()) {
//When user clicks on the image view of the collapsed layout,
//visibility of the collapsed layout will be changed to "View.GONE"
//and expanded view will become visible.
collapsedView.setVisibility(View.GONE);
expandedView.setVisibility(View.VISIBLE);
}
}
return true;
case MotionEvent.ACTION_MOVE:
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});
}
/**
* Detect if the floating view is collapsed or expanded.
*
* #return true if the floating view is collapsed.
*/
private boolean isViewCollapsed() {
return mFloatingView == null || mFloatingView.findViewById(R.id.collapse_view).getVisibility() == View.VISIBLE;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
}
I am not able to figure out anomalous behaviour help me out.
Start your service as forground.
https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
Since you want your service to run continuously you will have to override the onStartCommand method and return START_STICKY
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
https://developer.android.com/reference/android/app/Service.html
I am working on android.I have some little problem to display dialog fragment in specific position.
Following is some information regarding my code.
I'm taking one Costume adapter for display data.in Adpater Layout i am having one image view and i want to display Dialog Fragment Exact left side of this image view When i Tapped in Image.So I'm Doing Following Code to get X and y Co-ordinate where I tapped in Screen.
holder.img_Infoimage.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//showDialog( holder.img_Infoimage.getX(), holder.img_Infoimage.getY());
//int position = (int) v.getTag();
Bundle bundle = new Bundle();
bundle.putInt("X", (int) event.getX());
bundle.putInt("Y", (int) event.getY());
//bundle.putInt("X", holder.img_Infoimage.getLeft());
//bundle.putInt("Y", holder.img_Infoimage.getTop());
// bundle.putInt("X", (int) holder.img_Infoimage.getLeft());
// bundle.putInt("Y", (int) holder.img_Infoimage.getTop());
Log.e(this.getClass().getName(), ":::::: position x ::::" +event.getX());
Log.e(this.getClass().getName(), ":::::: position Y ::::" + event.getY());
InfoAlertDialog alertDialog = new InfoAlertDialog();
alertDialog.setArguments(bundle);
alertDialog.show(fragmentManager, NearByFriendsFragment.class.getName());
}
return true;
}
});
And I am Also having on InfoAlertDialog class which is extends by DialogFragment.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
/*wmlp.x = 422; //x position
wmlp.y = 49;*/ //y position
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
getDialog().setCancelable(true);
Bundle bundle = getArguments();
//Log.e("", "X :::::::" + wmlp.x + " Y:::::" + wmlp.y);
wmlp.x = bundle.getInt("X"); //x position
wmlp.y = bundle.getInt("Y"); //y position
/*
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
wmlp.height=ViewGroup.LayoutParams.WRAP_CONTENT;
wmlp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = bundle.getInt("X"); //x position
wmlp.y = bundle.getInt("Y"); //y position
wmlp.gravity = Gravity.TOP | Gravity.RIGHT;
wmlp.y = 500;
*/
Log.e("", "X :::::::" + wmlp.x + " Y:::::" + wmlp.y);
//getDialog().getWindow().setAttributes(wmlp);
View view = inflater.inflate(R.layout.dialog_info_alert, container, false);
return view;
}
in All Above code we can have some Comment it's Just for your Reference because i also try this but it's not display dialog at any specific position.
Please anyone can tell me how can i fix it...?
I would recommend looking into PopupWindow. It's a lot more flexible than a Dialog in cases like yours.
I Really try hard to solve this problem and Finally I got solution of this problem.I am share this code with you.
Just making following change in my code.
First Pass Listview in Constructor of Adapter.
int xCoordinates,yCoordinates;
ListView lv;
public NearByFriendsAdapter(Context context, List<NearByFriendsSupportClass> list, FragmentManager fragmentManager, ListView lst_NearByFriends) {
super(context, 0,list);
this.context = context;
this.list = list;
this.fragmentManager=fragmentManager;
this.lv=lst_NearByFriends;
}
and Also taking Two Seperate event to get X and Y co-ordinate of your screen and Display Dialog Like Following.
holder.img_Infoimage.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int[] listViewCoords = new int[2];
lv.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
//xCoordinates=x-1500;
yCoordinates=y-490;
return false;
}
});
holder.img_Infoimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle=new Bundle();
//bundle.putInt("X", xCoordinates);
bundle.putInt("Y", yCoordinates);
InfoAlertDialog alertDialog=new InfoAlertDialog();
alertDialog.setArguments(bundle);
alertDialog.show(fragmentManager, NearByFriendsFragment.class.getName());
}
});
And Also making Following change in AlertDialog
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
getDialog().setCancelable(true);
Bundle bundle = getArguments();
wmlp.y = bundle.getInt("Y");
getDialog().getWindow().setAttributes(wmlp);
View view=inflater.inflate(R.layout.dialog_info_alert,container,false);
Log.e("", "X :::::::" + wmlp.x + " Y:::::" + wmlp.y);
return view;
}
Thanks,
I hope it's Help full to you.
int[] locations = new int[2];
view.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
otherView.setLocation(x, y);
This results in otherView being verticaly ~somewhere in the middle of first view and not exactly a top of it.
This is basically the logic of setLocation method:
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = mX;
layoutParams.y = mY;
dialog.getWindow().setAttributes(layoutParams);
return dialog;
Just a guess, but I notice you didn't ensure the margins of the default-generated dialog are zero. Perhaps try this:
WindowManager.LayoutParams lp...
lp.horizontalMargin = 0;
lp.verticalMargin = 0;
I have a list, it captured moving finger, and you can delete an item by sliding your finger
if the image is not loaded in my container, with ImageView.setImage(), the container moves correctly.
However, if I loaded one image the container moves slowler.
Why is this happening?
Attach images below.
1.moves correctly
not moves correctly
always moves the container from left to rigth
Why is it?
Thanks.
UPDATE 19/01/2014
i reduced bitmap to 150 kb and continue equal.
leave my code
objelementos.lnyDatosCliente.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
final CCliente objcCCliente = lstLista.get(objelementos.posicion);
CAnimaciones objAnmanim = new CAnimaciones();
Display pantalla = afrmGstionClientes.getActivity().getWindowManager().getDefaultDisplay();
final int ancho = pantalla.getWidth();
switch ( event.getAction()) {
case MotionEvent.ACTION_DOWN:
fdYPrimeroPulsado = event.getRawY();
fdXPulsadoInicio = event.getRawX();
fdXUltimaPulsado = event.getRawX();
iTamanioLnyPulsado =0;
break;
case MotionEvent.ACTION_MOVE:
float fdXPulsado = event.getRawX();
float fdXMovimiento = fdXPulsado- fdXUltimaPulsado;
objelementos.lnyDatosCliente.setVisibility(4);
objelementos.lnyDatosClienteToMove.setVisibility(0);
objelementos.lnyDatosClienteToMove.setX(fdXMovimiento);
iTamanioLnyPulsado =1;
break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL:
float fdXPulsad = event.getRawX();
float xmov =fdXPulsad- fdXUltimaPulsado;
fdYUltimoPulsado = event.getRawY();
int difx = (int)fdXPulsad- (int)fdXPulsadoInicio;
if (difx>120){
ObjectAnimator animaciion = objAnmanim.CrearAnimacion(objelementos.lnyDatosClienteToMove, xmov, ancho+10, "x", 85);//(xFin-20, ancho+10, 200) ;
animaciion.start();
new Handler().postDelayed(new Runnable() {
public void run() {
float num = objcCCliente.getposicionXInicio();
AlertDialog.Builder builder = new AlertDialog.Builder(
getContext());
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("!AVISO!");
builder.setMessage("¿Estas seguro de eliminar el cliente?")
.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomAdapterListadoClientes.this.lstLista.remove(objelementos.posicion);
CustomAdapterListadoClientes.this.notifyDataSetChanged();
dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
vol(ancho-10, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}, animaciion.getDuration());
}else{
if (iTamanioLnyPulsado==0){
float dify = fdYPrimeroPulsado -fdYUltimoPulsado;
if (dify<0){
dify*=-1;
}
if (dify<10){
float fddondeesta = objelementos.lnyDatosCliente.getY();
ObjectAnimator animaciion = objAnmanim.CrearAnimacion(objelementos.lnyDatosCliente, fddondeesta-1, fddondeesta, "x",50);
animaciion.start();
Cnavegar objNavegar = new Cnavegar();
AfrmHigthModificationCliente afrmModificacion = new AfrmHigthModificationCliente(true, objcCCliente,afrmGstionClientes);
objNavegar.RemplazarFragmento(R.id.rtlAltas, afrmModificacion, "AfrmHigthModificationCliente", afrmGstionClientes.getActivity().getSupportFragmentManager());
}
}else{
if (difx>0){
vol(xmov, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
}else{
vol(xmov, objelementos.lnyDatosCliente.getX(), objelementos.lnyDatosClienteToMove, 180, objelementos.lnyDatosCliente);
}
}
}
break;
}
return true;
}
});
Are you using ViewHolder pattern?
Are you loading your images on another thread?
For load images I use Picasso library.
solve my problem of slow , which resided not in the size of the image
I used two linear layout changing your visibility associated with this adapter to change it to visible or invisiblo gone.
he adapter had to rewrite the code again and has this was because my problem.
now only work with linerar layout unchanged visibility, position and only my application I run to perfection
thanks for your help everyone