Created
February 3, 2016 04:57
-
-
Save mgarnerdev/ac3c879aeffe2f8889ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LGTM