setTranslation not working layout android - android

This is code for setTranslation . But transation not working. This code is calling out side activity . Any problem with this. Same code working perfectly in an activity.
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.activity_home, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(myView, params);
lockParent = myView.findViewById(R.id.lockParent);
lockParent.setOnTouchListener(new OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
System.out.println(arg1.getAction());
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("down");
downX = arg1.getRawX();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("move");
deltaX = arg1.getRawX() - downX;
lockParent.setTranslationX(deltaX);
break;
default:
break;
}
return true;
}
});

Move everything in your activity_home into FrameLayout and translate content inside that layout.
Suppose your activity_home layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lockParent>
...
<RelativeLayout>
Modified activity_home layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lockParent>
...
<RelativeLayout>
<FrameLayout>

Related

Android : How to set onCick listener to custom layout which is displayed using the WINDOW_SERVICE as floating window?

In service for displaying custom view over all windows I need to set onClick listener.
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.result_layout,
null);
// Here i need to add onClick listener
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(parent, params);
}
I tried to create button lister from inflater but without luck. What is the right approach to set click event listener for selected button by button ID?
Many thanks for any advice.
Resolved using the following code which is works in the service well:
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mParentLayout = (LinearLayout) inflater.inflate(R.layout.result_layout,
null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(mParentLayout, params);
Button b = (Button) mParentLayout.findViewById(R.id.closeButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windowManager.removeView(mParentLayout);
}
});
}
Inspired from:
Could not get Touch event for TYPE_SYSTEM_OVERLAY
Android can play tricks on what you think might work, but doesn't.
First, What I typically do is from onCreate() I place a call to:
InitializePage(); where I then put all my initialize code. It's just my style.
Then in my InitializePage() function I initialize the button, then enter the OnClickListener. I use Android Studio with Intellisense so it writes the code for me....
mSendButton = (TextView)findViewById(R.id.send_button1);
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{.....
});
It should be that easy. Good luck

Scaling ImageView crops the image (added dynamically to windowmanager)

i created a floating button, not connected to any activity or viewgroup
(added it directly to the window manager), which i want to grow and shrink on touch.
However, scaling it more than 1 crops the image that i want to display.
How can i grow the imageview to match the rescaled image inside?
this is some relevant code:
Creating the AnimatedButton, which extends ImageView in the service class OnCreate
(AnimatedButton extends ImageView and just adds some extra drawing on top of the image when OnDraw is created,
don't think its code is relevant here):
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mFloatingButton = new AnimatedButton(this);
mFloatingButton.setImageResource(R.drawable.ax);
...
...
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
//this code is for dragging the chat head
mFloatingButton.setOnTouchListener(new View.OnTouchListener() {
...
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
...
case MotionEvent.ACTION_UP:
if (isAClick(initialTouchX, event.getRawX(), initialTouchY, event.getRawY())) {
...
ObjectAnimator anim = ObjectAnimator.ofFloat(mFloatingButton, "Scale", 1.0f, 3.0f);
anim.setDuration(700);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(1);
...
}
...
}
...
}
...
});
windowManager.addView(mFloatingButton, params);
...
}
and this is setScale of AnimatedButton:
public void setScale(float scale)
{
mScale = scale;
this.setScaleX(mScale);
this.setScaleY(mScale);
invalidate();
}
tnx

Dragging an imageview to certain position

I am trying to create a draggable imageView ("chatHead" in the code, kinda like the chat head of Facebook)
I followed the instruction on the android website but whereever I drag the imageview, it always ends up at the left upper corner. (with x,y cordinate being (0,0) ~ (90,90))
Later on I found out the problem is that the case: MotionEvent.ACTION_MOVE is only entered when my touch is around the imageView location.
And when I drag a bit further away from the imageView, ACTION_MOVE is no longer entered. And instead the case DragEvent.ACTION_DRAG_EXIT is executed.
Can someone tell me how can I expand the area for the ACTION_MOVE to be executed or what is the problem. I would like to locate the imageView at whichever location I want on the screen.
( you may just focus on onDragListener, the MyDragShadowBuilder is a customized extending DragShawdowBuilder and onLongClickListener is just to trigger the drag)
How to keep the imageView even the app is closed?
public class ChatHead extends Service {
private WindowManager windowManager;
private ImageView chatHead;
private WindowManager.LayoutParams params;
private final String CHAT_TAG ="CHAT";
private int cor_x = 0;
private int cor_y = 0;
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override public void onCreate() {
super.onCreate();
ImageView image = new ImageView(this);
Log.d("", "created");
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setTag(CHAT_TAG);
chatHead.setImageResource(R.drawable.chat_icon);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 300;
params.y = 300;
chatHead.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item(v.getTag().toString());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes,item);
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(chatHead);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return false;}
});
chatHead.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
//cor_x = ((int) chatHead.getX());
//cor_y = ((int) chatHead.getY());
switch (event.getAction()){
case DragEvent.ACTION_DRAG_ENTERED : { Log.d("", "x:"+ event.getX()+"y:"+event.getY());break;}
case DragEvent.ACTION_DRAG_STARTED : { Log.d("", "x:"+ event.getX()+" y:"+event.getY());break;}
case MotionEvent.ACTION_MOVE: {
cor_x = ((int) event.getX());
cor_y = ((int) event.getX());
Log.d("", "x:"+ cor_x+" y:"+cor_y);
break;}
case DragEvent.ACTION_DRAG_EXITED:{
Log.d("", "x:"+ cor_x+" y:"+cor_y);
break;
}
case DragEvent.ACTION_DRAG_ENDED : {
if(windowManager!=null && params!=null){
params.x = cor_x;
params.y = cor_y;
Log.d("", "x:"+ cor_x+" y:"+cor_y);
windowManager.removeView(chatHead);
windowManager.addView(chatHead, params);
}
return false;
}
}
return false;
}
});
windowManager.addView(chatHead, params);
}
If I understand what you mean, your problem is that you don't get the MotionEvent.ACTION_MOVE unless your finger is above your image.
Well that is because the concept of DragListener is that the View that register that listener, accept the events. So the only view in your layout that register to those events is chatHead, and he gets only the events that occur above him.
So if you want to get all the events in layout you must register view that take the whole screen space. So the quickest solution is to wrap the view with container that take the whole screen, and register it instead the chatHead to the DragListener. Somethong like:
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
RelativeLayout container = new RelativeLayout(this);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
chatHead = new ImageView(this);
chatHead.setTag(CHAT_TAG);
chatHead.setImageResource(R.drawable.chat_icon);
RelativeLayout.LayoutParams chatHeadParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
chatHeadParams.addRule(Gravity.TOP | Gravity.LEFT) ;
chatHeadParams.leftMargin = 300;
chatHeadParams.topMargin = 300;
container.addView(chatHead, chatHeadParams);
windowManager.addView(container, params);
And changing the line:
chatHead.setOnLongClickListener(new View.OnLongClickListener() {
to:
container.setOnLongClickListener(new View.OnLongClickListener() {
You may also adjust additional things because the DragEvent event.getX() and event.getX() will return different values according to the View itself and the DragEvent.Action type.

PopUpWindow not showing Webview when start from service

I am trying to display a webview inside popupwindow which is being displayed by a service. When a user
touch on an image displayed by a service then a popupwindow should appear displaying a webview in it.
The code for the whole process is given below:
ChatHeadService.java
public class ChatHeadService extends Service {
private WindowManager windowManager;
float screenWidth=0, screenHeight=0;
private ImageView chatHead;
WindowManager.LayoutParams params;
WebView popUpWebView;
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
private GestureDetector gestureDetector;
#Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
#Override public void onCreate() {
super.onCreate();
screenWidth= getResources().getDisplayMetrics().widthPixels;
screenHeight= getResources().getDisplayMetrics().heightPixels;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
windowManager.addView(chatHead, params);
addChatHeadListener();
}
public void addChatHeadListener(){
chatHead.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showPopUpWindow();
}
});
}
public void showPopUpWindow(){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
(int)screenWidth,
(int)screenHeight);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popUpWebView = (WebView) popupView.findViewById(R.id.idWebView1);
popUpWebView.getSettings().setJavaScriptEnabled(true);
popUpWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
popupWindow.showAsDropDown(chatHead, 0, 0);
popUpWebView.loadUrl("http://wwww.google.co.in");
}
}
And here is my xml file
popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:id="#+id/idWebView1"/>
</LinearLayout>
Problem is nothing displayed when i clicked on the icon but when i use textview instead of webview
it diplayed textview successfully.

How to create a button which will stay on top of all window and can take click to open another similar overlay activity in android?

I am new at android programming. I have created a button using system overlay and a service. Now what I want is to click that button and get another overlay window of more 5 buttons. But the problem is that button doesn't take any click and don't allow me to open another overlay window. Here is the xml code I have used to draw the button--
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:id="#+id/l1"
android:layout_height="fill_parent"
tools:context=".OverlayActivity" >
<Button
android:id="#+id/mainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:clickable="true"
android:visibility="visible"
android:focusable="true">
</Button>
and here is the code I have used to show the overlay button
#Override
public void onCreate() {
super.onCreate();
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.activity_overlay, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
Button button = (Button)oView.findViewById(R.id.mainButton);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(OverlayService.this, "GotCha!!! ", Toast.LENGTH_LONG).show();
}
});
ViewGroup vg = (ViewGroup)oView.findViewById(R.id.l1);
final Display metrics = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
vg.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()){
case MotionEvent.ACTION_MOVE:
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = metrics.getWidth()-100;
int h = metrics.getHeight()-100;
if(x > w){
x = w;
}
if(y > h){
y = h;
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
selected_item.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
break;
default:
break;
}
return false;
}
});
}
Any ideas guys? I am running out of time..Please
Better you use fragment for this
Create views using fragments, overlay a button above the fragment.

Categories

Resources