简体   繁体   中英

how to place the textview exactly where the user Long Presses in Eclipse for Android

I am learning to code in Eclipse for Android. I googled and understood that View.OnLongClickListener can be used to call a method when the user long presses in the application. However, I am stuck at coming at a solution to make the TextView to shift it based on the exact location where the user long presses.

I have implemented the condition to check for a long press from the user. Any idea on how to make the TextVIew appear where the user long presses? I want the TextView to appear exactly where the user long presses.

findViewById(R.id.myimage).setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            showNameInCustomPosition();// Method to call to when user long presses
            return false;
        }
    });

Update: I am not getting the text to move, but when I click on the edge of the screen, the text is partially displayed that it goes outside the scope of the screen. Can anyone suggest how to avoid this? This is my Layout properties now.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myimage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/shrinivas"
    android:onClick="onClick"
    android:clickable="true"
    tools:context="com.example.hello.MainActivity" >

    <TextView
        android:id="@+id/shownamecenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_text2" />

        <TextView
        android:id="@+id/shownamecustom"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="fill"
        android:text="@string/my_text" />




</RelativeLayout>

The one with id shownamecustom is the one causing provblems.

Try this, below code is MainActivity which implements OnTouchListener

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity implements OnTouchListener {

TextView move_textview;
RelativeLayout mainLayout;
int x,y; //used to get coordinates

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
    move_textview = (TextView)findViewById(R.id.textView1);
    mainLayout.setOnTouchListener(this);//listener to get touch coordinates
    mainLayout.setOnLongClickListener(new OnLongClickListener() { //listen when user long click

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            setAbsoluteLocation(move_textview,x,y); //passing textview, x & y param to the method where the textview wanna move
            return false;
        }
    });

}
//methode use to move the text view
private void setAbsoluteLocation(TextView tview, int x, int y) 
{
    RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) tview.getLayoutParams();

    alp.leftMargin = x-50;
    alp.topMargin = y-50;
    tview.setLayoutParams(alp);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

//Overide onTouch method used to get touch coordinates  
@Override
public boolean onTouch(View arg0, MotionEvent event) {
    // TODO Auto-generated method stub
    x = (int)event.getRawX();
    y = (int)event.getRawY();
    return false;
 }
}

activity_main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sample.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="86dp"
    android:layout_marginTop="171dp"
    android:text="Movable" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM