Skip to content

Instantly share code, notes, and snippets.

@bratawisnu
Created August 18, 2018 14:40
Show Gist options
  • Save bratawisnu/997b34e2795ff4916dd619bf1c1b2a25 to your computer and use it in GitHub Desktop.
Save bratawisnu/997b34e2795ff4916dd619bf1c1b2a25 to your computer and use it in GitHub Desktop.
Berikut adalah code untuk melakukan input data ke Firebase
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb013d60"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Firebase"
android:padding="10dp"
android:textSize="34sp"
android:textColor="#fff"
android:textStyle="bold|italic"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/textTitle"
android:layout_below="@id/title"
android:background="#fff"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/textInput"
android:layout_below="@id/textTitle"
android:layout_marginTop="20dp"
android:background="#fff"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="@id/textInput"
android:id="@+id/btnSubmit"
android:background="#dc304a9f"
android:textColor="#fff"
android:layout_marginRight="70dp"
android:layout_marginLeft="70dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Submit"/>
</RelativeLayout>
public class WriteFirebaseActivity extends AppCompatActivity {
Activity activity;
EditText textTitle, textInput;
Button submit;
DatabaseReference rootRef, demoRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_data_firebase);
activity = this;
textTitle = (EditText) findViewById(R.id.textTitle);
textInput = (EditText) findViewById(R.id.textInput);
submit = (Button) findViewById(R.id.btnSubmit);
rootRef = FirebaseDatabase.getInstance().getReference();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strTextTitle = textTitle.getText().toString();
String strtextInput = textInput.getText().toString();
demoRef = rootRef.child("demo").push();
demoRef.child("textTitle").setValue(strTextTitle);
demoRef.child("textInput").setValue(strtextInput);
demoRef.child("date").setValue(getDateTime());
startActivity(new Intent(activity, GetFirebaseActivity.class));
}
});
}
@Override
protected void onResume() {
super.onResume();
ResetField();
}
public void ResetField() {
textTitle.setText("");
textInput.setText("");
}
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment