Skip to content

Instantly share code, notes, and snippets.

@YitziG
Created March 12, 2018 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YitziG/d7f92d20f0decd44578c2d4100b78d11 to your computer and use it in GitHub Desktop.
Save YitziG/d7f92d20f0decd44578c2d4100b78d11 to your computer and use it in GitHub Desktop.
the xml layout and Java code for our app
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yitzi.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/group"/>
<TextView
android:id="@+id/topTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="top text"
android:textColor="@android:color/black"
android:textSize="36sp"
android:visibility="gone"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="createMeme"
android:text="CREATE MEME"/>
<TextView
android:id="@+id/bottomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="bottom text"
android:textColor="@android:color/white"
android:textSize="36sp"
android:visibility="gone"/>
<EditText
android:id="@+id/topEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:hint="Write top text here!"
android:inputType="text"
android:paddingLeft="5dp"
android:textColorHint="@android:color/black"/>
<EditText
android:id="@+id/bottomEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:hint="Write bottom text here!"
android:inputType="text"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white"/>
</RelativeLayout>
package com.example.yitzi.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView topTextView;
TextView bottomTextView;
EditText topEditText;
TextView bottomEditText;
Button button;
boolean memeShown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topTextView = findViewById(R.id.topTextView);
bottomTextView = findViewById(R.id.bottomTextView);
topEditText = findViewById(R.id.topEditText);
bottomEditText = findViewById(R.id.bottomEditText);
button = findViewById(R.id.button);
}
public void createMeme(View view) {
hideKeyboard(view);
hideTheEditTextViewsAndTheButton(view);
getTheTextFromTheEditTextViewsAndSetItOnTheTextViews();
showTheTextViews();
memeShown = true;
}
private void showTheTextViews() {
toggleTextViews(View.VISIBLE);
}
private void getTheTextFromTheEditTextViewsAndSetItOnTheTextViews() {
String topTextContent = topEditText.getText().toString();
topTextView.setText(topTextContent);
String bottomTextContent = bottomEditText.getText().toString();
bottomTextView.setText(bottomTextContent);
}
private void hideTheEditTextViewsAndTheButton(View view) {
view.setVisibility(View.GONE);
topEditText.setVisibility(View.GONE);
bottomEditText.setVisibility(View.GONE);
}
private void hideKeyboard(View view) {
View currentFocus = this.getCurrentFocus();
if (currentFocus != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
@Override
public void onBackPressed() {
if (memeShown) {
clearAllTextFromAllViews();
toggleTextViews(View.GONE);
showMemeCreationView();
memeShown = false;
}
else {
super.onBackPressed();
}
}
private void showMemeCreationView() {
topEditText.setVisibility(View.VISIBLE);
bottomEditText.setVisibility(View.VISIBLE);
button.setVisibility(View.VISIBLE);
}
private void toggleTextViews(int visibility) {
topTextView.setVisibility(visibility);
bottomTextView.setVisibility(visibility);
}
private void clearAllTextFromAllViews() {
topTextView.setText("");
bottomTextView.setText("");
topEditText.setText("");
bottomEditText.setText("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment