Dialog position in android - android

I am creating a store map where image is coming from the sever. I have co-ordinate of specific position of product item. I am showing dialog on different markers which I have drawn through canvas.
Dialog shows perfect when I click over makers, But when I scroll the image & click the marker again, dialog position also changes every-time. Is there any solution for perfect positioning of dialog on image scroll?
Thank you.
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#2567B3" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="First Floor"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/back1"
android:layout_width="25dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:background="#drawable/back" />
<ImageView
android:id="#+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:background="#drawable/selector"
/>
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.shopping.PaintView
android:id="#+id/paint_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/test" />
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends BaseActivity implements OnTouchListener {
static float mX,mY,mX1,mY1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to PaintView
PaintView paintView = (PaintView) findViewById(R.id.paint_view);
paintView.setOnTouchListener(this);
backclick();
}
#SuppressLint("InlinedApi")
#Override
public boolean onTouch(View v,MotionEvent event) {
// TODO Auto-generated method stub
final Dialog dialogMarketList = new Dialog(MainActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewList = inflater.inflate(R.layout.custom_dialog,null);
dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogMarketList.setContentView(viewList);
Window window = dialogMarketList.getWindow();
WindowManager.LayoutParams wmlp = window.getAttributes();
wmlp.gravity = Gravity.START | Gravity.TOP;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wmlp);
TextView header = (TextView) viewList.findViewById(R.id.header);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// // Getting X & Y coordinate
int offers[] = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 };
int pointx[] = { 47, 338, 396, 52, 49, 387, 468, 455, 217, 231 };
int pointy[] = { 77, 59, 297, 294, 564, 534, 56, 407, 478, 187 };
int distance = 35;
mX = event.getX();
mY = event.getY();
for (int i = 0; i < 10; i++) {
if (mX > pointx[i] - distance && mX < pointx[i] + distance && mY >pointy[i] - distance && mY < pointy[i] + distance) {
if (offers[i] == 0){
header.setText("floor: 1" + "\nShelf: Top"
+ "\nPosition: Right");
wmlp.x = (int) mX; // x position
wmlp.y = (int) mY; // y position
}
else {
header.setText("floor: 2" + "\nShelf: Top"
+ "\nPosition: Right" + "\nOffer:10% discount");
wmlp.x = (int) mX; // x position
wmlp.y = (int) mY; // y position
}
dialogMarketList.show();
}
}
break;
}
return false;
}
PaintView.java
public class PaintView extends View {
int offers[]={0,1,1,0,1,0,1,0,1,0};
Paint mPaint;
float mX[]={47,338,396,52,49,387,468,455,217,231};
float mY[]={77,59,297,294,564,534,56,407,478,187};
TextView mTVCoordinates;
// ---add the marker---
Bitmap marker1;
Bitmap marker2;
public PaintView(Context context,AttributeSet attributeSet){
super(context,attributeSet);
/** Initializing the variables */
mPaint = new Paint();
mTVCoordinates = null;
marker1 = BitmapFactory.decodeResource(getResources(),R.drawable.blue);
marker2 = BitmapFactory.decodeResource(getResources(),R.drawable.red);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i=0;i<10;i++)
{
if(offers[i]==0)
{ canvas.drawBitmap(marker1,mX[i], mY[i], null);
invalidate();
}
else
{ canvas.drawBitmap(marker2, mX[i], mY[i], null);
invalidate();
}
}
}

Try this to show dialog at the bottom of the screen:
Dialog dlg = <code to create custom dialog>;
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about

Try to set the anchor of the dialog to the your marker.Google maps also uses the anchors to display the marker window for the markers.
Here's a way to set the anchor.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
hope it helps

Related

Android, overlapping imageViews

I am creating programatically ImageViews and dragging them in the same layout.
I would like to disable overlapping for this ImageView. Is there a setting to be applied to disable overlap when I create every ImageView programatically or something else ?
public class MainActivity extends ActionBarActivity {
private int offset_x = 0;
private int offset_y = 0;
RelativeLayout canvas;
LinearLayout buttonslayout;
boolean isselected = false;
View selected_item = null;
int test1,test2;
Button save,newdoor,newwindow,rotate;
TextView xcoord,ycoord;
//static final int SNAP_GRID_INTERVAL = 43;
static final int SNAP_GRID_INTERVAL = 44;
static final int SNAP_GRID_INTERVAL2 = 25;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvas = (RelativeLayout) findViewById(R.id.container2);
ViewGroup vg = (ViewGroup)findViewById(R.id.container3);
//buttonslayout = (LinearLayout) findViewById(R.id.container3);
save = (Button) findViewById(R.id.savebtn);
newdoor = (Button) findViewById(R.id.btnnewdoor);
newwindow = (Button) findViewById(R.id.btnnewwindow);
xcoord = (TextView) findViewById(R.id.xcoord);
ycoord = (TextView) findViewById(R.id.ycoord);
rotate = (Button) findViewById(R.id.rotate);
newwindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int lungime = 66;
final int inaltime = 20;
final int []lastcoords = new int[2];
final int canvasWidth = canvas.getWidth();
final int canvasHeight = canvas.getHeight();
RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container2);
final View canvas = ((View) v.getParent());
final int []coordwall = new int[2];
mylayout.getLocationInWindow(coordwall);
final ImageView image = new ImageView(getBaseContext());
image.setBackgroundResource(R.drawable.element_window);
mylayout.addView(image);
image.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
isselected = true;
xcoord.setText("X: "+lastcoords[0]);
ycoord.setText("Y: "+lastcoords[1]);
Log.i("test","Isselected = "+isselected);
break;
default:
break;
}
return false;
}
});
rotate.setOnClickListener(new View.OnClickListener() {
int grad=0;
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(grad!=1)
{
image.setRotation(90);
grad=1;
}
else
{
image.setRotation(180);
grad=0;
}
}
});
mylayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
switch(event.getAction())
{
case MotionEvent.ACTION_UP:
isselected = false;
Log.i("test","Isselected = "+isselected);
break;
case MotionEvent.ACTION_MOVE:
if(isselected == true)
{
View canvas = ((View) v.getParent());
final int width = v.getWidth() / 2, height = v.getHeight() / 2;
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
int rawX = (int) event.getRawX(), rawY = (int) event.getRawY();
int x_coord = (int) event.getRawX() - coordwall[0] - width;
int y_coord = (int) event.getRawY() - coordwall[1] - height;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
if (rawY < coordwall[1] + inaltime / 2)
lp.topMargin = 0;
else
if (rawY >= coordwall[1] + canvasHeight - inaltime )
lp.topMargin = canvasHeight - inaltime*2;
else
lp.topMargin = y / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
if(rawX < coordwall[0] + lungime / 2)
lp.leftMargin = 0;
else
if(rawX >= coordwall[0] + canvasWidth - lungime)
{
lp.leftMargin = canvasWidth - lungime*2;
Log.i("arry","leftmargin = " + lp.leftMargin);
}
else
lp.leftMargin = x / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
lastcoords[0] = lp.leftMargin;
lastcoords[1] = lp.topMargin;
xcoord.setText("X: "+lp.leftMargin);
ycoord.setText("Y: "+lp.topMargin);
lp.setMargins(lp.leftMargin, lp.topMargin, 0, 0);
selected_item.setLayoutParams(lp);
}
break;
default:
break;
}
return true;
}
});
}
});
And the xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="horizontal"
tools:context="com.example.draganddrop.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/container3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#848484"
android:orientation="vertical" >
<Button
android:id="#+id/savebtn"
style="#drawable/new_wall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_wall"
android:textSize="13sp" />
<Button
android:id="#+id/btnnewwindow"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_window"
android:textSize="13sp" />
<Button
android:id="#+id/btnnewdoor"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_door"
android:textSize="14sp" />
<TextView
android:id="#+id/xcoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="X: " />
<TextView
android:id="#+id/ycoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Y: " />
<Button
android:id="#+id/rotate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate" />
</LinearLayout>
<RelativeLayout
android:id="#+id/container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
</RelativeLayout>
</LinearLayout>
It is not very clear what exactly You want to do, but if I am understand You the right way, You want to set some ImageViews to a layout programmatically? Why are Your Views overlapping? I think You have to set the layout Paramaters for Your view container:
//after onCreate in the activity, create the LinearLayout (for example)
//and set the LayoutParams, if You have no one declared in Your xml
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//then create the imageView, set the image inside and add it to the container
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.your_image);
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
//set all as content
setContentView(linearLayout);
Have You done it in this way? If not, try it, also if You want to set multiple imageViews or some other Layout, You have to play a little bit with the LayoutParams.

Partial redraw -> invalidate (Rect rect)

In my onDraw I have all code needed to build my entire view but how can I detect if I want to perform only a partial redraw.
I guess a partial redraw should be triggered by calling canvas.invalidate(Rect rect);
Right?
In developer settings of my device I enabled “Show screen updates” but this always tells me that my entire screen is redrawn…
Below you see a screenshot of my app:
As you can see it is a calendar and I want to give the user a visual feedback when an entry is clicked (let’s say a red border around)…
I’ve already seen some samples but they either use a bitmap or lots of member variables to execute just the code needed for redrawing specific region in onDraw…
Can anyone tell me what’s the best way to implement such a feature?
UPDATE:
On my first draw Canvas.getClipBounds() returns the following rect:
Rect(0, 0 - 1200, 1800)
when I call invalidate(new Rect(304, 748 - 529, 902)) and check getClipBounds() again in onDraw() it still has the same value.
UPDATE2 (my code):
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
_pTouchDown = new PointF(event.getX(), event.getY());
downX = event.getX();
downY = event.getY();
entrySelected = hasTimeRecordAt(downX, downY);
if (entrySelected != null) {
Rect rInvalidate = new Rect((int) entrySelected.get_Selected().left, (int) entrySelected.get_Selected().top, (int) entrySelected.get_Selected().right,
(int) entrySelected.get_Selected().bottom);
invalidate(rInvalidate);
}
return false;
}
UPDATE 3 (my Layout):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MultiDayCalendarActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rlStatusline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvStatusline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="asdf" >
</TextView>
<TextView
android:id="#+id/tvStatusline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="1234" >
</TextView>
</RelativeLayout>
<com.mxp.time.calendar.DayHeader
android:id="#+id/dayHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ScrollView
android:id="#+id/m_svMultiRoot1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<com.mxp.time.calendar.Calendar
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/brushBackgroundLight" >
</LinearLayout>
<RelativeLayout
android:id="#+id/rlMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<ImageButton
android:id="#+id/ibtCreateNewTimeRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu" />
<ImageButton
android:id="#+id/ibtCalendarStopwatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stopwatch" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/ibtCalendarBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/previous" />
<ImageButton
android:id="#+id/ibtCalendarForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/next" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageButton
android:id="#+id/ibtCalendarToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/today" />
<ImageButton
android:id="#+id/ibtGotoJobs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/jobs" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
UPDATE4:
setContentView(R.layout.test_calendar);
// _cal = (Calendar) findViewById(R.id.calendar);
_cal = new Calendar(this);
_dayHeader = (DayHeader) findViewById(R.id.dayHeader);
final ScrollView sv = (ScrollView) findViewById(R.id.m_svMultiRoot1);
sv.addView(_cal);
same result:
I in onTouch I pass Rect(172, 748 - 265, 902) and in onDraw I get Rect(0, 0 - 720, 1800)
UPDATE 5:
package com.example.testclip;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
class V extends View {
private static final String TAG = "null";
Rect clip = new Rect();
public V(Context context) {
super(context);
int[] colors = { 0xff000000, 0xffff0000, 0xffffffff };
Drawable d = new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM, colors);
setBackgroundDrawable(d);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
StringBuilder sb = new StringBuilder();
sb.append("left: ");
sb.append(x);
sb.append(", top: ");
sb.append(y);
sb.append("right: ");
sb.append(x + 10);
sb.append(", bottom: ");
sb.append(y + 10);
Log.d(TAG, "onTouchEvent clip rect: " + sb.toString());
invalidate(x, y, x + 10, y + 10);
return false;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(w, w * 4);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
StringBuilder sb = new StringBuilder();
sb.append("left: ");
sb.append(clip.left);
sb.append(", top: ");
sb.append(clip.top);
sb.append("right: ");
sb.append(clip.right);
sb.append(", bottom: ");
sb.append(clip.bottom);
Log.d(TAG, "onDraw clip rect: " + sb.toString());
}
}
Activity:
package com.example.testclip;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ScrollView;
public class TestClipMainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_test_clip_main);
ScrollView sv = new ScrollView(this);
V v = new V(this);
sv.addView(v);
setContentView(sv);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_clip_main, menu);
return true;
}
}
This code produces the following output
02-15 10:47:54.011: D/OpenGLRenderer(833): Enabling debug mode 0
02-15 10:47:54.926: D/dalvikvm(833): threadid=1: still suspended after undo (sc=1 dc=1)
02-15 10:48:03.806: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
02-15 10:48:05.381: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
02-15 10:48:07.181: D/null(833): onTouchEvent clip rect: left: 409, top: 358right: 419, bottom: 368
02-15 10:48:09.806: D/null(833): onDraw clip rect: left: 0, top: 0right: 720, bottom: 2880
you must be doing something wrong, see this custom View:
class V extends View {
Rect clip = new Rect();
private int cnt = 20;
public V(Context context) {
super(context);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
cnt++;
Log.d(TAG, "calling invalidate " + cnt);
invalidate(10, 10, cnt, cnt);
return false;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
Log.d(TAG, "onDraw clip " + clip);
}
}
UPDATE: custom view inside a ScrollView:
class V extends View {
Rect clip = new Rect();
public V(Context context) {
super(context);
int[] colors = {0xff000000, 0xffff0000, 0xffffffff};
Drawable d = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
setBackgroundDrawable(d);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
invalidate(x, y, x + 10, y + 10);
return true;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(w, w * 4);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.getClipBounds(clip);
Log.d(TAG, "onDraw clip height: " + clip.height());
}
}
add this to onCreate:
ScrollView sv = new ScrollView(this);
V v = new V(this);
sv.addView(v);
setContentView(sv);

when move the image from bottom to up with animation,not getting the full image

when move the image from bottom to up with animation,not getting the full image,i am declaring imageview width wrap content.when i was moving the down its working perfectly,but when i image dragging up,the showing part comes with animation not getting the full image.
public class MainActivity extends Activity {
// mainLayout is the child of the HorizontalScrollView ...
private LinearLayout mainLayout;
// this is an array that holds the IDs of the drawables ...
private int[] images = { R.drawable.gg, R.drawable.gg, R.drawable.gg,
R.drawable.gg, R.drawable.gg, R.drawable.gg };
int status = 0;
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
boolean first = true;
ImageButton btn;
View cell = null;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF startPoint = new PointF();
static final int NONE = 0;
static final int DRAG = 1;
Bitmap icon;
int mode = NONE;
AnimationListener animx;
/** Called when the activity is first created. */
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < images.length; i++) {
cell = getLayoutInflater().inflate(R.layout.image, null);
final ImageView img = (ImageView) cell.findViewById(R.id.imageView);
img.setImageResource(images[i]);
// mainLayout.addView(cell);
// }
final LinearLayout ll = (LinearLayout) cell
.findViewById(R.id.lllayout);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
// final AbsoluteLayout aalayout = (AbsoluteLayout) cell
// .findViewById(R.id.absLayout);
btn = (ImageButton) cell.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button 1 pressed",
2000).show();
}
});
img.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(final View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
System.out.println("sdhsjdkklkl");
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (first) {
x = event.getX();
first = false;
System.out.println("matrix: " + matrix);
savedMatrix.set(matrix);
mode = DRAG;
startPoint.set(img.getLeft(), event.getY());
} else {
startPoint.set(img.getLeft(), event.getY());
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float dx = event.getX() - startPoint.x;
final float dy = event.getY() - startPoint.y;
float setheight = btn.getBottom() + 6;
if (dy > 0) {
System.out.println("saved matrix:" + savedMatrix);
matrix.set(savedMatrix);
System.out.println("y value: " + dy);
matrix.postTranslate(img.getLeft(), 80);
ll.setVisibility(View.VISIBLE);
img.setImageMatrix(matrix);
}
else if (dy < 0) {
int fromLoc[] = new int[2];
img.getLocationOnScreen(fromLoc);
final float startX = fromLoc[0];
final float startY = fromLoc[1];
int toLoc[] = new int[2];
img.getLocationOnScreen(toLoc);
final float destX = 0;
final float destY = img.getTop();
ll.setVisibility(View.GONE);
TranslateAnimation mAnimation = new TranslateAnimation(
0, 0, 0, -80);
mAnimation.setDuration(10000);
mAnimation.setRepeatCount(0);
mAnimation
.setInterpolator(new LinearInterpolator());
mAnimation.setFillAfter(true);
img.setAnimation(mAnimation);
}
}
return true;
}
});
System.out.println("bmgbgklb");
mainLayout.addView(cell);
}
}
}
my main xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
my sub xml is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="2dip"
android:scaleType="matrix"
android:src="#drawable/dd" >
</ImageView>
<LinearLayout
android:id="#+id/lllayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible" >
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dip"
android:background="#drawable/orangearrow_over"
android:focusable="true" />
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#drawable/orangearrow_over" />
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:background="#drawable/orangearrow_over" />
</LinearLayout>
</FrameLayout>

when i was dragging down the image,the button is not working

In my xml on button i want to declare image,when i was dragging down the image the button clicking was not working,it was in background,it does come the forground.
How can it be possible to use button? I want to display toast meassage when i click on button.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
ImageView img = null;
AbsoluteLayout aLayout;
int status = 0;
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
boolean first = true;
Bitmap icon;
Button b;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
img = (ImageView) findViewById(R.id.imageView);
icon = BitmapFactory.decodeResource(getResources(), R.drawable.gg);
b = (Button) findViewById(R.id.btnFavorites);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "activity closed",
Toast.LENGTH_LONG).show();
}
});
aLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
// img.setBackgroundColor(Color.TRANSPARENT);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (first) {
x = event.getX();
first = false;
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//
}
if ((event.getX() < (Measuredwidth - icon.getWidth() / 2) && event
.getX() > (icon.getWidth() / 2))
&& (event.getY() < MeasuredHeight
- (icon.getHeight() + 60) && event.getY() > icon
.getHeight() / 2)) {
#SuppressWarnings("deprecation")
// (int) event.getX()-img.getWidth()/2
LayoutParams lp = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (int) 0, (int) event
.getY() - img.getHeight() / 2);
img.setLayoutParams(lp);
}
return true;
}
});
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/ic_launcher"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
<AbsoluteLayout
android:id="#+id/absLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dip" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/gg"
android:scaleType="matrix" >
</ImageView>
</AbsoluteLayout>
</FrameLayout>
if you place image on button , Button will lose control so , you can give drawable (image) to button using background or drawable left or right top bottom ...or else you can implement on click listener even on image view..
Update..
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/favorite_slide"
android:includeFontPadding="false"
android:text="#string/titleFavourites"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
in the code i have given drawableleft, same yo can give for right top left and even padding also possible , and apply your logic for touch

Animate a view to slide in and slide out of the screen in android

Good day i have a view that is programmatically placed at the bottom left part of a SurfaceView and what i want is when a button is clicked, the view slides out of the screen and slides back in but nothing seems to happen in the animation. Here is my code, i will just put the relevant parts:
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6444"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:id="#+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="name" />
<TextView
android:id="#+id/medium_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="med"/>
<TextView
android:id="#+id/dimensions_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="dimen" />
</LinearLayout>
positioning the view:
RelativeLayout.param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 240);
param.leftMargin = 0;
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_info, null);
mylayout = (LinearLayout)view.findViewById(R.id.gallery_Info_root_id); //what i want to animate
view.setLayoutParams(param);
param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.getLayoutParams().width = 380;
RelativeLayout lContainerLayout = new RelativeLayout(VideoPlayback.this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT ));
lContainerLayout.addView(view);
addContentView(lContainerLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
slide in and slide out animation:
private void slideInView(){
Animation slidein = new MyTranslateAnimation(mylayout, -mylayout.getWidth(), 0, 0, 0);
slidein.setDuration(animationDuration);
slidein.setInterpolator(interpolator);
mylayout.startAnimation(slidein);
}
private void slideOutView(){
Animation slidein = new MyTranslateAnimation(mylayout, 0, -mylayout.getWidth(), 0, 0);
slidein.setDuration(animationDuration);
slidein.setInterpolator(interpolator);
mylayout.startAnimation(slidein);
}
MyTranslateAnimation which i got from here
public class MyTranslateAnimation extends Animation {
private View view;
private final float fromX;
private final float toX;
private final float fromY;
private final float toY;
public MyTranslateAnimation(View v, float fromX, float toX, float fromY, float toY) {
view = v;
this.fromX = fromX;
this.toX = toX;
this.fromY = fromY;
this.toY = toY;
setDuration(500);
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float x = (toX-fromX) * interpolatedTime + fromX;
float y = (toY - fromY) * interpolatedTime + fromY;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.setMargins((int)x, (int)y, 0, 0);
view.requestLayout();
}
}
Please can anyone spot anything i am doing wrong? because i can't seem to see why i can't slide "mylayout" in and out. Thanks in advance.

Categories

Resources