Skip to content

Instantly share code, notes, and snippets.

@sudhanshu-15
Last active May 24, 2018 18:47
Show Gist options
  • Save sudhanshu-15/48cbe91179013c87f83d199ddecdf637 to your computer and use it in GitHub Desktop.
Save sudhanshu-15/48cbe91179013c87f83d199ddecdf637 to your computer and use it in GitHub Desktop.
Summer REU Android Snippets
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
tools:context=".AnswerActivity">
<TextView
android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="@string/question_hint"
android:textColor="@color/fontColor"
android:textSize="24sp"
android:inputType="textCapSentences"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/answer_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/question_hint"
android:textColor="@color/fontColor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/question_label" />
<Button
android:id="@+id/ask_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="askAgain"
android:text="@string/ask_again"
android:textSize="24sp"
android:theme="@style/FlatButtonClear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/answer_label" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/question_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
tools:context=".QuestionActivity">
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/welcome_text"
android:textColor="@color/fontColor"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.100000024" />
<EditText
android:id="@+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="@string/question_hint"
android:inputType="textMultiLine"
android:textAlignment="center"
android:textColor="@color/fontColor"
android:textColorHint="@color/fontColor"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/welcome_text" />
<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:onClick="clearQuestion"
android:text="@string/clear_button"
android:textSize="24sp"
android:theme="@style/FlatButtonClear"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/question_text" />
<Button
android:id="@+id/shake_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginTop="32dp"
android:onClick="shakeEvent"
android:text="@string/shake_button"
android:textSize="24sp"
android:theme="@style/FlatButtonShake"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/question_text" />
</android.support.constraint.ConstraintLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class AnswerActivity extends AppCompatActivity {
private static final String QUESTION_TEXT = "Question";
private static final String ANSWER_TEXT = "Answer";
private TextView questionText;
private TextView answerText;
private Random random;
private String[] answers = {"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"You can count on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Absolutely",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
"Chances aren't good"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
questionText = (TextView)findViewById(R.id.question_label);
answerText = (TextView)findViewById(R.id.answer_label);
random = new Random();
Bundle myData = getIntent().getExtras();
if (myData != null) {
String question = myData.getString(QUESTION_TEXT);
Log.d("Answer", "onCreate: " + question);
if (question != null && question.length() == 0) {
questionText.setText(getResources().getString(R.string.no_question));
answerText.setText("????");
} else {
questionText.setText(question);
int randomNumber = random.nextInt(answers.length - 1);
answerText.setText(answers[randomNumber]);
Animation animationFadeIn = AnimationUtils.loadAnimation(this,R.anim.fadein);
answerText.startAnimation(animationFadeIn);
}
}
// if (myData != null) {
// String question = myData.getString(QUESTION_TEXT);
// Log.d("Answer", "onCreate: " + question);
// if (question != null && question.length() == 0) {
// questionText.setText(getResources().getString(R.string.no_question));
// answerText.setText("????");
// } else {
// questionText.setText(question);
// if (savedInstanceState != null) {
// answerText.setText(savedInstanceState.getString(ANSWER_TEXT));
// } else {
// int randomNumber = random.nextInt(answers.length - 1);
// answerText.setText(answers[randomNumber]);
// }
// }
// }
}
public void askAgain(View view) {
finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ANSWER_TEXT, answerText.getText().toString());
}
}
private String[] answers = {"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"You can count on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Absolutely",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
"Chances aren't good"};
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#ffd54f</color>
<color name="backgroundColor">#455a64</color>
<color name="fontColor">#f5f5f5</color>
<color name="clearColor">#4caf50</color>
<color name="shakeColor">#42a5f5</color>
</resources>
if (myData != null) {
String question = myData.getString(QUESTION_TEXT);
Log.d("Answer", "onCreate: " + question);
if (question != null && question.length() == 0) {
questionText.setText(getResources().getString(R.string.no_question));
answerText.setText("????");
} else {
questionText.setText(question);
int randomNumber = random.nextInt(answers.length - 1);
answerText.setText(answers[randomNumber]);
}
}
if (myData != null) {
String question = myData.getString(QUESTION_TEXT);
Log.d("Answer", "onCreate: " + question);
if (question != null && question.length() == 0) {
questionText.setText(getResources().getString(R.string.no_question));
answerText.setText("????");
} else {
questionText.setText(question);
if (savedInstanceState != null) {
answerText.setText(savedInstanceState.getString(ANSWER_TEXT));
} else {
int randomNumber = random.nextInt(answers.length - 1);
answerText.setText(answers[randomNumber]);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<set android:interpolator="@android:anim/linear_interpolator" xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:duration="2000" android:fromAlpha="0.1" android:toAlpha="1.0" />
</set>
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class QuestionActivity extends AppCompatActivity {
private static final String QUESTION_TEXT = "Question";
private EditText questionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
questionText = (EditText) findViewById(R.id.question_text);
}
public void clearQuestion(View view) {
if (questionText.getText() != null || questionText.getText().toString().contains(" ")) {
questionText.setText("");
}
}
public void shakeEvent(View view) {
if (questionText.getText() != null && questionText.getText().toString().length() > 0) {
Intent intent = new Intent(this, AnswerActivity.class);
intent.putExtra(QUESTION_TEXT, questionText.getText().toString());
startActivity(intent);
}else {
Snackbar.make(findViewById(R.id.shake_button), R.string.no_question, Snackbar.LENGTH_LONG).show();
}
}
}
<resources>
<string name="app_name">Magic Eight Ball</string>
<string name="welcome_text">Welcome to Eight Ball</string>
<string name="shake_button">Shake</string>
<string name="clear_button">Clear</string>
<string name="question_hint">What is your question?</string>
<string name="no_question">I cannot understand your question</string>
<string name="ask_again">Ask Again</string>
</resources>
<style name="FlatButtonShake" parent="Theme.AppCompat.Light">
<item name="android:buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
<item name="colorControlHighlight">@color/fontColor</item>
<item name="colorAccent">@color/shakeColor</item>
</style>
<style name="FlatButtonClear" parent="Theme.AppCompat.Light">
<item name="android:buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
<item name="colorControlHighlight">@color/fontColor</item>
<item name="colorAccent">@color/clearColor</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment