Reputation: 785
I am working on dragview for that I want one textview which is allign at center for giving (message tap to write on),I am using following layout but textview is not coming in center it is coming at top of the layout,can you please suggest me
Following is my code,
<com.snapquote.DragLayer
android:id="@+id/drag_layer"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/taketext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80313131"
android:padding="10.0dip"
android:shadowColor="#b3000000"
android:shadowDx="0.0"
android:gravity="center_vertical"
android:shadowDy="-1.0"
android:shadowRadius="1.0"
android:singleLine="false"
android:text="@string/double_tap_to_write"
android:textColor="#b3ffffff"
android:textSize="16.0sp"
android:visibility="visible" />
</com.snapquote.DragLayer>
class:
public class MyAbsoluteLayout extends ViewGroup {
public MyAbsoluteLayout(Context context) {
super(context);
}
public MyAbsoluteLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyAbsoluteLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
int maxHeight = 0;
int maxWidth = 0;
// Find out how big everyone wants to be
measureChildren(widthMeasureSpec, heightMeasureSpec);
// Find rightmost and bottom-most child
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
int childRight;
int childBottom;
MyAbsoluteLayout.LayoutParams lp
= (MyAbsoluteLayout.LayoutParams) child.getLayoutParams();
childRight = lp.x + child.getMeasuredWidth();
childBottom = lp.y + child.getMeasuredHeight();
maxWidth = Math.max(maxWidth, childRight);
maxHeight = Math.max(maxHeight, childBottom);
}
}
// Account for padding too
maxWidth += getPaddingLeft () + getPaddingRight ();
maxHeight += getPaddingTop () + getPaddingBottom ();
/* original
maxWidth += mPaddingLeft + mPaddingRight;
maxHeight += mPaddingTop + mPaddingBottom;
*/
// Check against minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
resolveSize(maxHeight, heightMeasureSpec));
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
}
@Override
protected void onLayout(boolean changed, int l, int t,
int r, int b) {
int count = getChildCount();
int paddingL = getPaddingLeft ();
int paddingT = getPaddingTop ();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
MyAbsoluteLayout.LayoutParams lp =
(MyAbsoluteLayout.LayoutParams) child.getLayoutParams();
int childLeft = paddingL + lp.x;
int childTop = paddingT + lp.y;
/*
int childLeft = mPaddingLeft + lp.x;
int childTop = mPaddingTop + lp.y;
*/
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(),
childTop + child.getMeasuredHeight());
}
}
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MyAbsoluteLayout.LayoutParams(getContext(), attrs);
}
// Override to allow type-checking of LayoutParams.
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof MyAbsoluteLayout.LayoutParams;
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
public static class LayoutParams extends ViewGroup.LayoutParams {
public int x;
public int y;
public LayoutParams(int width, int height, int x, int y) {
super(width, height);
this.x = x;
this.y = y;
}
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public String debug(String output) {
return output + "Absolute.LayoutParams={width="
+ sizeToString(width) + ", height=" + sizeToString(height)
+ " x=" + x + " y=" + y + "}";
}
protected static String sizeToString(int size) {
if (size == WRAP_CONTENT) {
return "wrap-content";
}
if (size == MATCH_PARENT) {
return "match-parent";
}
return String.valueOf(size);
}
} // end class
DragLayer.java
public class DragLayer extends MyAbsoluteLayout
implements DragSource, DropTarget
{
DragController mDragController;
public DragLayer (Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setDragController(DragController controller) {
mDragController = controller;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mDragController.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return mDragController.onTouchEvent(ev);
}
@Override
public boolean dispatchUnhandledMove(View focused, int direction) {
return mDragController.dispatchUnhandledMove(focused, direction);
}
public boolean allowDrag () {
// In this simple demo, any view that you touch can be dragged.
return true;
}
public void onDropCompleted (View target, boolean success)
{
toast ("DragLayer2.onDropCompleted: " + target.getId () + " Check that the view moved.");
}
public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
View v = (View) dragInfo;
toast ("DragLayer2.onDrop accepts view: " + v.getId ()
+ "x, y, xO, yO :" + new Integer (x) + ", " + new Integer (y) + ", "
+ new Integer (xOffset) + ", " + new Integer (yOffset));
int w = v.getWidth ();
int h = v.getHeight ();
int left = x - xOffset;
int top = y - yOffset;
DragLayer.LayoutParams lp = new DragLayer.LayoutParams (w, h, left, top);
this.updateViewLayout(v, lp);
}
public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
}
public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
}
public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
}
public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
return true;
}
public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo, Rect recycle)
{
return null;
}
public void toast (String msg)
{
if (!DragActivity.Debugging) return;
Toast.makeText (getContext (), msg, Toast.LENGTH_SHORT).show ();
} // end toast
} // end class
Upvotes: 1
Views: 245
Reputation: 20041
use this below properties in your parent Layout
android:layout_gravity="center"
android:gravity="center"
Note: AbsoluteLayout
class was deprecated in API level 3.
Use FrameLayout, RelativeLayout
or a custom layout
instead.
Edit: on your MyAbsoluteLayout.java
onLayout
method your need add few lines
// Use the child's gravity and size to determine its final
// frame within its container.
Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);
GO thru this link and make few changes to apply gravity for your custom layout
Upvotes: 1
Reputation: 1814
AbsoluteLayout is depreciated, you should really use a FrameLayout, LinearLayout or RelativeLayout.
If you use a RelativeLayout, add add the following to your TextView:
android:layout_centerInParent="true"
For a FrameLayout or LinearLayout add the following to the parent:
android:gravity="center"
Upvotes: 0