MBMJ
MBMJ

Reputation: 5431

Create Button on every touch on screen

I want to make an application where whenever I touch the screen, which should be blank at the start, a button will be created and pop up onto the screen. So what I tried here is to make a button in main.xml and call it by id. Then I applied this method.

public class TouchButtonActivity extends Activity {
Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button) findViewById(R.id.b1);
    b.setOnTouchListener(nextListener);
}


public OnTouchListener nextListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            float x = event.getX()*event.getXPrecision();
            float y = event.getY()*event.getYPrecision();
            FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            p.gravity = Gravity.TOP;
            p.leftMargin = (int) x;
            p.topMargin = (int) y;
            b.setLayoutParams(p);
            return true;
        }
        return false;
    }
};

}

This my xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B" />

</FrameLayout>

How could i do this?

Upvotes: 3

Views: 2553

Answers (2)

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

If you want to add a button while you click any where in the screen i think you can use a Relative layout and add touch listener on that layout. While touching on that screen, add button on that layout so it will be like on new touch a new button will be popped up. Thank you

Codes will be like --

Main Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

And the class will be like --

rl = (RelativeLayout) findViewById(R.id.rl);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            float x = event.getX()*event.getXPrecision();
            float y = event.getY()*event.getYPrecision();

            Button btn = new Button(getApplicationContext());
            RelativeLayout.LayoutParams bp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            bp.leftMargin = (int) x;
            bp.topMargin = (int) y;
            btn.setLayoutParams(bp);
            rl.addView(btn);

            return false;
        }
    });

Upvotes: 5

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23962

I'm not sure what you're trying to achieve, but:

1) Make sure that your main.xml layout is FrameLayout

2) And then change your code:

public OnTouchListener nextListener = new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float x = event.getX()*event.getXPrecision();
                    float y = event.getY()*event.getYPrecision();
                    FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    p.gravity = Gravity.TOP;
                    p.leftMargin = (int) x;
                    p.topMargin = (int) y;
                    b.setLayoutParams(p);
                    return true;
                }
                return false;
            }
        };

EDIT

You forgot this:

setOnTouchListener(nextListener);

Upvotes: 0

Related Questions