To avoid multiple button click at same time

Bottleneck

Sometimes user can made bottleneck in your app when user click multiple button click at same time. you can avoid, it’s very easy.

To avoid multiple button clik at same time

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second(or any time span).

public class MainActivity extends Activity implements View.OnClickListener{

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonRight = (Button) findViewById(R.id.btnRight_activity_main);
        buttonLeft = (Button) findViewById(R.id.btnLeft_activity_main);

        buttonLeft.setOnClickListener(this);
        buttonRight.setOnClickListener(this);
    }

    // variable to track event time
    private long mLastClickTime = 0;

    @Override
    public void onClick(View v) {
        // Preventing multiple clicks, using threshold of 1 second
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
           return;
        }
        mLastClickTime = SystemClock.elapsedRealtime();
        pressedOnClick(v);
    }

    public void pressedOnClick(View v){
        switch (v.getId()){
            case R.id.btnLeft_activity_main:
                showToast("Left_activity_main");
                break;

            case R.id.btnRight_activity_main:
                showToast("Right_activity_main");
                break;
        }

    }
}

example in github

Add a textview and a button to linear layout programmatically

It’s very simple to add textviews and buttons to linear layout programmatically, just follow this steps. The fist step. Add a LinearLayout in .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_linear_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/thewordis"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

</LinearLayout>

Now in .class add:
1) add LinearLayout
2) add LayoutParams
3) add TextView
4) add Button
5) add the TextView and the Button to View

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ClassTheDeveloperWorldIsYours extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.the_developer_world_is_yours);

        //added LInearLayout
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_linear_layout);

        //added LayoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        //add textView
        TextView textView = new TextView(this);
        textView.setText("The developer world is yours");
        textView.setId(1);
        textView.setLayoutParams(params);

        // added Button
        Button button = new Button(this);
        button.setText("thedeveloperworldisyours");
        button.setId(2);

        //added the textView and the Button to LinearLayout
        linearLayout.addView(textView);
        linearLayout.addView(button);

    }
}

You can download this code:

Download code