Skip to content

Instantly share code, notes, and snippets.

@mgarnerdev
Created February 3, 2016 04:57
Show Gist options
  • Save mgarnerdev/ac3c879aeffe2f8889ad to your computer and use it in GitHub Desktop.
Save mgarnerdev/ac3c879aeffe2f8889ad to your computer and use it in GitHub Desktop.
package com.example.mgarner.blankslate;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtInput;
private Button mBtnSubmit;
private TextView mTvLabel;
private Handler mTextHandler;
private ArrayList<String> mTextArray;
private Runnable mTextDisplayRunnable;
private boolean mShowingText = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtInput = (EditText) findViewById(R.id.main_activity_et_input);
mBtnSubmit = (Button) findViewById(R.id.main_activity_btn_submit);
mTvLabel = (TextView) findViewById(R.id.main_activity_tv_label);
mTextHandler = new Handler();
mTextDisplayRunnable = new Runnable() {
@Override
public void run() {
if (mTextArray.size() > 0) {
mShowingText = true;
mTvLabel.setText(mTextArray.get(0));
mTextArray.remove(0);
mTextHandler.postDelayed(this, 1000);
} else {
mTvLabel.setText("");
mShowingText = false;
}
}
};
mTextArray = new ArrayList<>();
mBtnSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.main_activity_btn_submit) {
//handle submit
String textInput = mEtInput.getText() != null ? mEtInput.getText().toString() : "";
mEtInput.setText("");
showText(textInput);
}
}
private void showText(String textInput) {
mTextArray.add(textInput);
if (!mShowingText) {
mTextHandler.post(mTextDisplayRunnable);
}
}
}
@seanwalmart
Copy link

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment