Keep a button within boundaries - java

I'm still incredible new to android programming (like 3 weeks old) but I'm slowly getting the hang of things. I've looked on different websites for an answer but I haven't found anything yet.
My Java code so far (at least what's relevant):
view = (ImageView)findViewById(R.id.imageView3);
backgroundImageName = String.valueOf(view.getTag());
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int myNewX = (int)event.getX(); //this line and the next get the X & Y coordinates of wherever the mouse clicked
int myNewY = (int)event.getY();
Button button = (Button)findViewById(R.id.add_text);
// find a way to keep the button within the borders of the white square
if (event.getAction() == MotionEvent.ACTION_UP) { //checks if the mouse click was released
button.setVisibility(View.VISIBLE);
button.setX(myNewX - 160); //this line and the next set the coordinates of the button (plus the adjustment)
button.setY(myNewY + 70); //to make the button by above and in the middle of where the mouse clicked
}
else
{
button.setVisibility(View.INVISIBLE);
}
return true;
}
});
and my XML code so far (at least what's relevant):
<ImageView
android:id="#+id/imageView3"
android:layout_width="450dp"
android:layout_height="450dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:contentDescription="#string/white_background"
android:tag="white"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="#+id/button"
app:layout_constraintLeft_toLeftOf="#+id/imageView2"
app:layout_constraintRight_toRightOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/textView"
app:srcCompat="#mipmap/white" />
<Button
android:id="#+id/add_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_text"
android:visibility="invisible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="71dp"
tools:layout_editor_absoluteX="16dp" />
What I'm trying to do is keep the button within the boundaries shown in the picture but I'm not sure how to do this. I've got an idea of how it's supposed to work which is:
If mouseclick (the event) is outside the border {
set the x coordinate of the button to the edge of the border
and the same for the y coordinate
}
I've been trying to get it to properly work but it simply refuses to. If anyone can help me figure out the code I need to make it work, I would really appreciate the help.
Thank you

First create a boundary check and then perform your task:
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//obtain the boundaries of the view
if(event.getAction() == MotionEvent.ACTION_DOWN){
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if (event.getAction() == MotionEvent.ACTION_UP){
//Catches out of boundary user's movement.
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() +
(int) event.getY())){
button.setVisibility(View.VISIBLE);
//perform your other calculations
}
return true;
}
});
Note: From API 14 you can use: Android MotionEvent's ACTION_HOVER_ENTER:
https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

Related

TextView set image on last line

Is there any way to place the image on the last line of the TextView
I use setCompoundDrawablesWithIntrinsicBounds. Some ideas about it will be good.
Also i need to handle clicks on my image only. This behavior is represented by the following code :
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.info, 0);
textView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= textView.getRight() - textView.getTotalPaddingRight()){
listener.onAddressClicked(livingAreas);
return true;
}
}
return true;
}
});
With Text view you can set a drawable on a determinated position.
You can use this xml example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/image"
android:text="blablabla"/>
Programmatically:
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
text_view.setCompoundDrawables(null, null, null, getDrawable(drawable_image_id));
Use SpannableString so that you can include image in your text. For code sample, check this question and answer

Android - Detecting when user touches a drawable left

I have a TextView that contains a DrawableRight, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? and if it is how can I do it ?
PS: I am working inside a fragment
TextView XML
<TextView
android:id="#+id/mTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="22dp"
android:drawableRight="#mipmap/icn" //this is the drawable
/>
mTitle.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//drawable pressed
return true;
}
}
return false;
}
});

I can't do the key enter Go

I am beginner in android but not in Java , and have a problem.
Took hours losses in my application ignores keyboard to enter and you put the code below.
What I want is that when I do a thing to enter and go. Since it seems redundant and have not optimal OK button when you have an OK in the Enter itself.
t_Num = (EditText) findViewById(R.id.eTT);
...
t_Num.setFocusableInTouchMode(true);
t_Num.requestFocus();
t_Num.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("entra en Onkey" + event.toString());
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)){
tV.setText("OK");
System.out.println("Press OK");
return true;
}
return false;
}
});
Whit this code i get spam the keypress , but not spam the keypress OK.
And XML:
<EditText
android:layout_width="75dp"
android:layout_height="wrap_content"
android:inputType="numberSigned|phone"
android:digits="-0123456789"
android:ems="10"
android:id="#+id/eTT"
android:imeActionLabel="#string/OK"
android:maxLength="6"
android:maxLines="1"
android:layout_alignBottom="#+id/tV2_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/btnOk"
android:layout_toEndOf="#+id/btnOk" />
thanks for help
PD : I search in this page but not get solution , font1 , font2 , etc...
I use search :)
Try this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
//your code
return true;
}
return false;
}
});
This is if the editText has
android:imeOptions="actionGo"
in its xml. Same should apply with Enter. Just change the EditorInfo action.
EditText is a subclass of TextView.

Creating a Drag and Drop ImageButton

I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.
Here is my code for the imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
layout = (LinearLayout) findViewById(R.id.gllayout);
layout.removeView(mainHut);
layout.addView(mainHut, params);
}//end if
}//end else
return false;
}//end onTouch function
});
here is the xml for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgm" >
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mainhut" />
</LinearLayout>
Any help is appreciated. Thanks.
To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:
button.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (me.getAction() == MotionEvent.ACTION_MOVE )
{
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
v.setLayoutParams(params);
}
return false;
}
});

Transfer scroll event from sliding view to ScrollView - Sliding panel with ScrollView like Google Maps

So I'm using the Sliding Up Panel Library in my application, and I'm trying to implement a ScrollView inside the sliding panel. Since both the sliding panel and the ScrollView are controlled by vertical scrolls, this is causing me some issues.
I've partially got it to work by switching the panel's dragview once the panel has been slid all the way up, and when the ScrollView has been scrolled to the top.
The problem I'm facing now is that, when scrolling the panel to top the scrolling doesn't transfer to the ScrollView, like it does in Google Maps. Little hard to explain, so look at the video here:
www.youtube.com/watch?v=9MUsmQzusX8&feature=youtu.be
This is the panel slide listener:
...
slidePanel.setEnableDragViewTouchEvents(true);
slidePanel.setPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
#Override
public void onPanelSlide(View panel, float slideOffset) {
// Change the dragview to panelheader when panel is fully expanded
// I'm doing this here instead of in onPanelExpanded,
// because onPanelExpanded first gets called once scroll
// is released.
if (slideOffset <= 0) {
slidePanel.setDragView(layoutPanelTop);
}
// If the panel is not fully expanded set the whole
// panel as dragview
else if(slideOffset > 0) {
slidePanel.setDragView(layoutPanel);
}
}
}
#Override
public void onPanelExpanded(View panel) {
// layout.setDragView(layoutPanelTop);
panelCollapsed = false;
panelExpanded = true;
panelAnchored = false;
Log.v("TAG, "panelExpanded");
}
#Override
public void onPanelCollapsed(View panel) {
slidePanel.setDragView(layoutPanel);
panelCollapsed = true;
panelExpanded = false;
panelAnchored = false;
Log.v(TAG, "panelCollapsed");
}
#Override
public void onPanelAnchored(View panel) {
slidePanel.setDragView(layoutPanel);
panelCollapsed = false;
panelExpanded = false;
panelAnchored = true;
Log.v(TAG, "panelAnchored");
}
});
And I have managed to create a fully working scrollview listener by extending scrollview, which can detect scroll direction and onDown and onUp motion events:
private boolean atScrollViewTop = false;
#Override
public void onScrollChanged(int scrollY) {
scrollY = Math.min(mMaxScrollY, scrollY);
if (scrollY <= 0) {
Log.v("myTag", "You at scrollview top");
atScrollViewTop = true;
} else {
atScrollViewTop = false;
}
mScrollSettleHandler.onScroll(scrollY);
switch (mState) {
case STATE_SCROLL_UP:
if (panelExpanded && atScrollViewTop) {
slidePanel.setDragView(layoutPanel);
} else {
slidePanel.setDragView(layoutPanelTop);
}
Log.v("myTag", "scrolling up");
break;
case STATE_SCROLL_DOWN:
slidePanel.setDragView(layoutPanelTop);
Log.v("myTag", "scrolling down");
break;
}
}
#Override
public void onDownMotionEvent() {
}
#Override
public void onUpOrCancelMotionEvent() {
}
I've been struggling with this the last two days.. So really hope on some pointer at least. Thanks very much in advance. Regards Jakob Harteg.
Sorry for delay.. i find the solution.
image = (ImageView) findViewById(R.id.image); //Layout to slide
SlidingUpPanelLayout layout = (SlidingUpPanelLayout)
findViewById(R.id.sliding_layout);
layout.setDragView(image);
/*This method sets the layout to be used only
in the sliding panel, while all layouts children
are able to perform other functions such as scrolling */
And this is the layout
<..SlidingUpPanelLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" />
<LinearLayout
android:id="#+id/slide_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#drawable/ec_image"/>
<!-- FINALLY SCROLLVIEW -->
<ScrollView .... />
Hope it is useful.
I'm guessing ScrollView is child of the SlidingPanel?
In that case, override onInterceptTouchEvent to your SlidingPanel to intercept the onTouch event of your ScrollView when y = 0.
onInterceptTouchEvent does the following two:
child gets action cancel event
parent get the event trough onTouch
I don't know if I've arrived to late but after working hard some days I've found that AndroidSlidingUp panel has a method called setScrollView who handles scroll events properly.
I hope that this post will be useful because I was spending much time searching and I didn't find some tip that help me.

Categories

Resources