Here is activity_main code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center"
android:textSize="36sp"
android:textColor="@color/colorPrimary"
android:text="To Do"
android:layout_marginTop="45dp"
android:fontFamily="@font/montserratlight"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_marginTop="126dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="50dp"
android:src="@drawable/plussymbol" />
</RelativeLayout>
Here is my MainActivity Java Code:
package com.geoff.productivitywatch;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
int clickCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView);
final ArrayList<String> todo = new ArrayList<>();
todo.add("Swipe Up");
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.tododesign, todo);
list.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here is where the pop-up box code goes
final EditText todoEditText = new EditText(getApplicationContext());
AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("New Task")
.setMessage("What next?")
.setView(todoEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Add Task to listview
String task = String.valueOf(todoEditText.getText());
todo.add("" + task);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
}
}
I tried to write a method in which clicking the floating action button brought up a dialog alert box where you could edit text and then add it to a listview. Android Studio does not see an error in my code, however when I test the app on my android device, as soon as the Floating Action Button is clicked, the app crashes. However, I know it is not an issue with my device since I have tested it successfully numerous times before, it only began crashing when I added the AlertDialog method. I think I may have gone wrong with the contexts, however I have changed them to all the variants I can think of and it has not helped.