Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 618
MBMJ Avatar asked Jan 01 '26 03:01

MBMJ


1 Answers

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;
        }
    });
like image 144
Chinmoy Debnath Avatar answered Jan 02 '26 17:01

Chinmoy Debnath