Skip to content

Instantly share code, notes, and snippets.

@CodeNextPaco
Created October 23, 2019 03:01
Show Gist options
  • Save CodeNextPaco/522c5a3c14897ef4f0270f42f2e1e2d2 to your computer and use it in GitHub Desktop.
Save CodeNextPaco/522c5a3c14897ef4f0270f42f2e1e2d2 to your computer and use it in GitHub Desktop.
Basic Droidgram by Coach Paco. This is a basic implementation, not using classes.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="44dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView4"
android:layout_width="376dp"
android:layout_height="52dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:text="User name"
app:layout_constraintBottom_toTopOf="@+id/scrollView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="349dp"
app:srcCompat="@drawable/android_paco" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/likesTxtView"
android:layout_width="301dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:text="Likes: " />
<Button
android:id="@+id/likeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:onClick="addLike"
android:text="Like" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<ImageButton
android:id="@+id/setMessageBtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:onClick="setMessages"
app:srcCompat="@android:drawable/ic_menu_send" />
</LinearLayout>
<TextView
android:id="@+id/messageTxtView"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.scrollviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button likeBtn;
private ImageButton sendMsgBtn;
private EditText inputTxt;
private TextView outputTxt;
private TextView likesTxt;
int likes = 0;
private ArrayList<String> messages = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
likeBtn = findViewById(R.id.likeBtn);
sendMsgBtn = findViewById(R.id.setMessageBtn);
inputTxt = findViewById(R.id.editText);
outputTxt =findViewById(R.id.messageTxtView);
likesTxt = findViewById(R.id.likesTxtView);
messages.clear();
outputTxt.setTextSize(30);
}
public void setMessages(View v){
if(inputTxt.getText().length() > 1 ) {
String newMessage = inputTxt.getText().toString() + "\n";
messages.add(newMessage);
displayMessages();
Log.d("msg:", "" + messages);
inputTxt.setText("");
}
}
public void addLike(View v){
likes++;
likesTxt.setText("Likes: " + likes);
}
public void displayMessages(){
outputTxt.setText("");
if (messages.size() > 0) {
for (int i = 0; i < messages.size(); i++) {
outputTxt.append(messages.get(i));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment