How to toggle touch event between nested Relative layouts? - android

I have a Relative layout holds a surface view and another Relative layout (Nested)..
1 ) I want to receive touch events separately for Surface view, if touch events are out of child relative layout. ??
2) if my touch events falls on child relative layout, then the current touch event should not go to surface view.
How to achieve this toggle ???
Thanks in advance..!
Below is my Activity:
public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
private RelativeLayout letterView;
private RelativeLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mainLayout.setOnTouchListener(this);
letterView = (RelativeLayout) findViewById(R.id.mlKnobView);
letterView.setOnTouchListener(this);
}
private boolean dragging = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
dragging = true;
eventConsumed = false;
}
} else if (action == MotionEvent.ACTION_UP) {
dragging = false;
eventConsumed = false;
} else if (action == MotionEvent.ACTION_MOVE) {
if (v != letterView) {
if (dragging) {
setAbsoluteLocationCentered(letterView, x, y);
}
}
}
return eventConsumed;
}
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
private void setAbsoluteLocation(View v, int x, int y) {
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) v.getLayoutParams();
alp.leftMargin = x;
alp.topMargin = y;
v.setLayoutParams(alp);
}
}
Below my Xml File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/mlKnobView"
android:layout_width="260dip"
android:layout_height="260dip"
android:layout_below="#+id/txtView"
android:background="#drawable/border" >
<Button
android:id="#+id/button_down"
style="?android:attr/buttonStyle"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dip"
android:background="#drawable/button_down"
android:text="Down" />
<com.example.RotaryKnob
android:id="#+id/jogView"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_above="#id/button_down"
android:layout_below="#+id/button_up"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button_left"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="#id/button_down"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/jogView"
android:background="#drawable/button_left"
android:text="Left" />
<Button
android:id="#+id/button_right"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="#id/button_down"
android:layout_alignBaseline="#id/button_left"
android:layout_alignBottom="#id/button_left"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/jogView"
android:background="#drawable/button_right"
android:text="Right" />
<Button
android:id="#+id/button_up"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_above="#+id/button_left"
android:layout_alignLeft="#+id/button_down"
android:layout_below="#+id/windowHeader"
android:background="#drawable/button_up"
android:text=" UP " />
</RelativeLayout>
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Hai" />
</RelativeLayout>

Related

unable to access Clipboard Content from foreground service in Android 10+

I know that we can't access clipboard content from background services in 10+ but there is an application that shows copied text on a floating widget in Android 10+ devices using foreground service here's that app. I want to achieve that functionality somehow! please help me.
My app is working fine in android P and lower devices!
FloatingViewService.java
public class FloatingViewService extends Service implements View.OnClickListener {
private WindowManager wm;
private View floatv;
View collapsed;
View expanded;
CharSequence copiedText;
EditText editText;
public FloatingViewService(){
}
#Override
public void onCreate() {
super.onCreate();
// getting the widget layout from xml using layout inflater
floatv = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,null);
// setting window manager layout parameters
// set foucus able ,, wrap content, set on lock screen etc...
final WindowManager.LayoutParams wrules = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
// getting window service and adding floating view to it
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(floatv,wrules);
//adding click listener to close button and expanded view
collapsed = floatv.findViewById(R.id.layoutCollapsed);
expanded = floatv.findViewById(R.id.layoutExpanded);
editText = floatv.findViewById(R.id.pastehere);
// adding an touch listener to make drag movement of the floating view
floatv.findViewById(R.id.relativeLayoutParent).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float touchX;
private float touchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
initialX = wrules.x;
initialY = wrules.y;
touchX = event.getRawX();
touchY = event.getRawY();
return true;
/*as we implemented OnTouchListener() to the root view, OnClickListner() won’t work. So, to detect clicks we will detect clicks in MotionEvent.ACTION_MOVE in the OnTouchListener() using below code:*/
case MotionEvent.ACTION_UP:
int diffX = (int) (event.getRawX() - touchX);
int diffY = (int) (event.getRawY() - touchY);
if(diffX < 10 && diffY < 10){
if (isViewCollapsed()){
collapsed.setVisibility(View.GONE);
expanded.setVisibility(View.VISIBLE);
wrules.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
wm.updateViewLayout(floatv,wrules);
}else {
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
wrules.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wm.updateViewLayout(floatv,wrules);
}
}
return true;
case MotionEvent.ACTION_MOVE:
// moving the widget across the screen
wrules.x = initialX + (int) (event.getRawX() - touchX);
wrules.y = initialY + (int) (event.getRawY() - touchY);
wm.updateViewLayout(floatv,wrules);
return true;
}
return false;
}
});
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
copiedText = clipboardManager.getPrimaryClip().getItemAt(0).getText();
if(copiedText != null){
editText.setText(String.valueOf(copiedText));
wm.updateViewLayout(floatv,wrules);
}
Toast.makeText(getBaseContext(), copiedText, Toast.LENGTH_SHORT).show();
}
});
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
if(floatv != null) wm.removeView(floatv);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.layoutExpanded){
collapsed.setVisibility(View.VISIBLE);
expanded.setVisibility(View.GONE);
}else if(v.getId() == R.id.buttonClose){
stopSelf();
}
}
private boolean isViewCollapsed() {
return floatv == null || floatv.findViewById(R.id.layoutCollapsed).getVisibility() == View.VISIBLE;
}
}
layout_floating_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relativeLayoutParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- this is the collapsed layout -->
<RelativeLayout
android:id="#+id/layoutCollapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="#+id/collapsed_iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_baseline_menu_book_24" />
<ImageView
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="50dp"
android:src="#drawable/ic_baseline_close_24"
android:background="#color/teal_200"/>
</RelativeLayout>
<!-- this is the expanded layout -->
<LinearLayout
android:id="#+id/layoutExpanded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#a0c3d7"
android:orientation="horizontal"
android:padding="8dp"
android:visibility="gone">
<ImageView
android:id="#+id/buttonSimplifiedCodingExpanded"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_baseline_markunread_24"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/buttonSimplifiedCoding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:text="Dictionary"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pastehere"
android:textAlignment="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
see these images
text should appear in editText, working in android 9

ANDROID Drag views anywhere on the layout

I would like to drag a view to any location on screen
I have implemented the touch listener and the only problem is that views that precede others in the hierarchy tend to drag them along. I want each view to be able to be dragged anywhere independent of the others.
I made sure I removed the view then added it with different parameters at the end of the drag.
public class MainActivity extends Activity {
int clickCount;
private float dx, dy;
private boolean isDragging;
private ViewGroup RootLayout;
private int Position_X;
private int Position_Y;
private TextView txtV01, txtV02, txtV03;
long startTime = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RootLayout = (ViewGroup) findViewById(R.id.main_layout);
txtV02 = (TextView) findViewById(R.id.txt02);
txtV02.setOnTouchListener(myTouchListener());
txtV01 = (TextView) findViewById(R.id.txt01);
txtV01.setOnTouchListener(myTouchListener());
txtV03 = (TextView) findViewById(R.id.txtV03);
txtV03.setOnTouchListener(myTouchListener());
}
private View.OnTouchListener myTouchListener() {
return new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View view, MotionEvent event) {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams =
(LinearLayout.LayoutParams)
view.getLayoutParams();
dx = x - lParams.leftMargin;
dy = y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = x - (int) dx;
layoutParams.topMargin = y - (int) dy;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
view.setLayoutParams(layoutParams);
break;
}
RootLayout.invalidate();
return true;
}
};
}
}
the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="space">
<Space
android:layout_width="match_parent"
android:layout_height="387dp"
android:tag="space_top" />
<TextView
android:id="#+id/txt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="cursive"
android:tag="line1"
android:text="This is line one ONE ONE ONE"
android:textAlignment="center"
android:textSize="33dp">
</TextView>
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="#+id/txt02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:tag="line2"
android:text="This is line TWO TWO NOT Line 1"
android:textAlignment="center"
android:textSize="18dp">
</TextView>
<Space
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="#+id/txtV03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:tag="line2"
android:text="This is line THREE THREE Leave me"
android:textAlignment="center"
android:textSize="18dp">
</TextView>
</LinearLayout>
Your help will be appreciated.

drag and drop auto scroll with multiple layout in android

"auto scroll not working very well when i drag and drop the image with multiple layout"
I searched all over, but could not find a solution.
i got a solution from this link:
Make a scrollView autoscroll with drag and drop in Android
#thehayro thanks for such a nice example.
but it is working for only one layout and auto scroll is also worked. but i have more than 4-5 child layout in one linear layout and this layout is in the scroll view my layout file is like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.dragvdropdemo.MyScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scroll_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/lldrag">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll1"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/dr_logo" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fb" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll2"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll3"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView1_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView2_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView3_l3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/ll4"
android:layout_marginTop="10dip"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/ImageView1_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/twitter" />
<ImageView
android:id="#+id/ImageView2_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView3_l4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</com.example.dragvdropdemo.MyScrollView>
i want to drag and drop image from one to another layout.i don't know how to solve it. i have tried but i could do it.i'm don't know veryand my code is:
public class MyActivity extends Activity {
ImageView img1,img2,img3,img01;
ImageView img02,img03,img1_l3,img2_l3,img3_l3,img1_l4,img2_l4,img3_l4;
LinearLayout ll1,ll2,ll3,ll4,lldrag;
MyScrollView myScrollView;
int mScrollDistance;
private static int oldvalue;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
//SCROLLVIEW
myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
myScrollView.setOnScrollViewListener(new MyScrollView.OnScrollViewListener() {
#Override
public void onScrollChanged1(OnScrollViewListener listener) {
// TODO Auto-generated method stub
mScrollDistance = myScrollView.getScrollY();
}
});
img1 = (ImageView)findViewById(R.id.imageView1);
img2 = (ImageView)findViewById(R.id.imageView2);
img3 = (ImageView)findViewById(R.id.imageView3);
img01 = (ImageView)findViewById(R.id.ImageView01);
img02 = (ImageView)findViewById(R.id.ImageView02);
img03 = (ImageView)findViewById(R.id.ImageView03);
img1_l3 = (ImageView)findViewById(R.id.ImageView1_l3);
img2_l3 = (ImageView)findViewById(R.id.ImageView2_l3);
img3_l3 = (ImageView)findViewById(R.id.ImageView3_l3);
img1_l4 = (ImageView)findViewById(R.id.ImageView1_l4);
img2_l4 = (ImageView)findViewById(R.id.ImageView2_l4);
img3_l4 = (ImageView)findViewById(R.id.ImageView3_l4);
ll1 = (LinearLayout)findViewById(R.id.ll1);
ll2 = (LinearLayout)findViewById(R.id.ll2);
ll3 = (LinearLayout)findViewById(R.id.ll3);
ll4 = (LinearLayout)findViewById(R.id.ll4);
lldrag = (LinearLayout)findViewById(R.id.lldrag);
img1.setOnTouchListener(new ChoiceTouchListener());
img2.setOnTouchListener(new ChoiceTouchListener());
img3.setOnTouchListener(new ChoiceTouchListener());
img01.setOnTouchListener(new ChoiceTouchListener());
img02.setOnTouchListener(new ChoiceTouchListener());
img03.setOnTouchListener(new ChoiceTouchListener());
img1_l3.setOnTouchListener(new ChoiceTouchListener());
img2_l3.setOnTouchListener(new ChoiceTouchListener());
img3_l3.setOnTouchListener(new ChoiceTouchListener());
img1_l4.setOnTouchListener(new ChoiceTouchListener());
img2_l4.setOnTouchListener(new ChoiceTouchListener());
img3_l4.setOnTouchListener(new ChoiceTouchListener());
ll1.setOnDragListener(new ChoiceDragListener());
ll2.setOnDragListener(new ChoiceDragListener());
ll3.setOnDragListener(new ChoiceDragListener());
ll4.setOnDragListener(new ChoiceDragListener());
lldrag.setOnDragListener(new ChoiceDragListener());
}
private final class ChoiceTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
ClipData data=ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched''
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
}
/**
* DragListener will handle dragged views being dropped on the drop area
* - only the drop action will have processing added to it as we are not
* - amending the default behavior for other parts of the drag process
*
*/
class ChoiceDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//no action necessary
Log.i("", "DRAGSTARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
Log.i("","DRAGENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
Log.i("","DRAGEXITED");
break;
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
Log.i("","DROP");
View view = (View) event.getLocalState();
LinearLayout ll=(LinearLayout) view.getParent();
ll.removeView(view);
LinearLayout dropTarget = (LinearLayout) v;
ImageView dropped = (ImageView) view;
dropTarget.addView(dropped,0);
Object tag = dropTarget.getTag();
if(tag!=null)
{
int existingID = (Integer)tag;
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view being dropped on - to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
break;
case DragEvent.ACTION_DRAG_LOCATION:
//no action necessary
int y = Math.round(event.getY());
int translatedY = y - mScrollDistance;
Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
int threshold =50 ;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold) {
// make a scroll up by 30 px
myScrollView.scrollBy(0, -30);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 200) {
// make a scroll down by 30 px
myScrollView.scrollBy(0, 30);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
//no action necessary
break;
default:
break;
}
return true;
}
}
}
MySrollView
public class MyScrollView extends ScrollView {
public OnScrollViewListener mListener;
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
if (mListener != null) {
mListener.onScrollChanged1(mListener);
}
}
public void setOnScrollViewListener(OnScrollViewListener listener) {
mListener = listener;
}
public static interface OnScrollViewListener {
public void onScrollChanged1(OnScrollViewListener listener);
}
}
i'm android beginner so don't know it very well so tell me how to solve it.
sorry for my bad english and grammer mistake.
thanks in advances
I solved this problem by myself. Here is the solution:
DragEvent.ACTION_DRAG_LOCATION:
//no action necessary
int y = Math.round(v.getY())+Math.round(event.getY());
int translatedY = y - mScrollDistance;
Log.i("translated",""+translatedY+" "+ mScrollDistance+" "+y);
int threshold =50 ;
// make a scrolling up due the y has passed the threshold
if (translatedY < 200) {
// make a scroll up by 30 px
myScrollView.smoothScrollBy(0, -15);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 500) {
// make a scroll down by 30 px
myScrollView.smoothScrollBy(0, 15);
}
break;

click event of button not getting raised

I am trying to make something like customised quick badge with some buttons on it. I have successfully desgined it but when i am clicking on those buttons it does nothing. Can someone tell me where am i going wrong.
demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/likemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<Button
android:id="#+id/likequickaction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
</LinearLayout>
popup_grid_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip"
android:background="#drawable/quickaction_top_frame" />
<ImageView
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/quickaction_arrow_up" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header2"
android:background="#drawable/quickaction_slider_background"
android:fadingEdgeLength="0.0dip"
android:paddingLeft="1.0dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_left" />
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/api_buttons" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_right" />
</LinearLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scroll"
android:background="#drawable/quickaction_bottom_frame" />
</RelativeLayout>
api_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/quickaction_slider_background" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/one"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="One" />
<Button
android:id="#+id/two"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Two" />
<Button
android:id="#+id/three"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Three" />
<Button
android:id="#+id/four"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Four" />
<Button
android:id="#+id/five"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Five" />
<Button
android:id="#+id/six"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Six" />
</TableRow>
</HorizontalScrollView>
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext()
.getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
protected void onCreate() {
}
protected void onShow() {
}
private void preShow() {
if (this.root == null) {
throw new IllegalStateException(
"setContentView was not called with a view to display.");
}
onShow();
if (this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);
this.window.setContentView(this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ this.anchor.getWidth(), location[1] + this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
// display on bottom
if (rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
}
LikeQuickActionDemo.java
public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.demo_layout);
this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
this.likemenuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
this.likequickactionButton = (Button) this
.findViewById(R.id.likequickaction);
this.likequickactionButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu(0, 200);
}
});
}
private static class DemoPopupWindow extends BetterPopupWindow implements
OnClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
protected void onCreate() {
// inflate layout
LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
// setup button events
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
View item = row.getChildAt(j);
if (item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}
// set the inflated view as what we want to display
this.setContentView(root);
}
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(),
Toast.LENGTH_SHORT).show();
this.dismiss();
}
}
}
The problem is in DemoPopupWindow.onCreate method:
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
root will be RelativeLayout defined in popup_grid_layout.xml. Later you iterate through all its children and set onclick listeners only if child view is TableRow, which is never the case. I guess what you want there is to find horizontalScrollView1 view and do the same with its children instead, something like:
View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
View v = horizontalScrollView1.getChildAt(i);
if (v instanceof TableRow) {
...

Drag And Drop textview on imageview in android

I tried my best can anyone help to drag and drop textview on imageview in android
my xml with frame layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</FrameLayout>
i tried drag and drop text as below as i have used here linear layout the text is dragging in some part only not on image
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DragNDropActivity extends Activity {
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
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 = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().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;
}
});
TextView img = (TextView)findViewById(R.id.img);
img.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;
}
});
}
}
Try to use Frame Layout it should figure out what you want.
Use RelativeLayout like this. Note:You may place it inside your ParentView
<RelativeLayout android:layout_width="100dp" android:id="#+id/photoframe" android:layout_height="320dp" android:layout_weight="1.0">
<ImageView android:layout_height="fill_parent" android:scaleType="fitXY" android:id="#+id/framephoto" android:src="#drawable/icon" android:layout_width="fill_parent"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:id="#+id/textView1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="188dp"></TextView>
</RelativeLayout>
inside your layout.xml. By using Relative you can drag your textview over your image.
Please try this one....
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="#drawable/ic_launcher" android:scaleType="center" />
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#AA000000" android:textColor="#ffffffff" android:text="Golden Gate"/>
</FrameLayout>
I checked it with my eclipse.And let me know is it working or not.

Categories

Resources