So I'm trying to get a string from an EditText view and store into a variable which I will then print when the CreateBtn is clicked. Cant quite get this to work. Keeps crashing, I think, because of the CreateBtn and I think it might have something to do with the AlertBox which is where the EditText view is located.
public class MyActivity extends Activity {
// declares vars
final Context context = this;
private Button addClassBtn;
private Button createBtn;
public EditText classNameInput;
private String classTextName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// initializes var
final LayoutInflater classInfo = getLayoutInflater();
addClassBtn = (Button) findViewById(R.id.classBtn);
classNameInput = (EditText) findViewById(R.id.classNameInput);
createBtn = (Button) findViewById(R.id.createBtn);
// creates a alert box with a text view when button is clicked
addClassBtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(classInfo.inflate(R.layout.class_info, null));
builder.setTitle("Add Class");
AlertDialog diaBox = builder.create();
diaBox.show();
}
});
//classNameInput is extracted to a variable
createBtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
String className;
className = classNameInput.getText().toString();
Log.v("EditText", className);
}
});
Here is class_info.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/classNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:paddingTop="25dp"
android:hint=" class name "
android:textSize="25sp"
android:paddingBottom="20dp"
android:fontFamily="monospace"
android:inputType="textAutoComplete|textAutoCorrect"
android:textAlignment="center"
android:paddingLeft="-10dp"
android:paddingRight="-10dp" />
<Button
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/classNameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/createBtn"
android:fontFamily="monospace"
android:textSize="23sp"
android:text="create"
android:onClick="createBtnClick" />
</RelativeLayout>
Here is activity_my.xml
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/classBtn"
android:clickable="false"
android:textSize="100sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClassBtnClick"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:focusable="true" />
</RelativeLayout>