Reputation: 1
I'm an android newbie and as a test I want to make a program that insert text in EditText and display it. I want my program to show a dialog box that states "Please insert comments first" when there is no value in the EditText. But
comments.getText().toString()==null
would produce error. Here is my code:
package android.insertcomments;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class InsertCommentsActivity extends Activity {
/** Called when the activity is first created. */
public Button insertcom;
public Button displaycom;
public EditText comments;
public Button savecom;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Insert Comments App");
setContentView(R.layout.home);
insertcom = (Button) findViewById(R.id.insertcom);
displaycom = (Button) findViewById(R.id.displaycom);
insertcom.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
addListenerOnButton();
}
});
displaycom.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if (comments.getText().toString()==null)
noCommentsErrormessage();
else
// Display comments
}
});
}
public void addListenerOnButton() {
setContentView(R.layout.comments_adder);
savecom = (Button) findViewById(R.id.savecom);
savecom.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
comments = (EditText) findViewById(R.id.commentsEditText);
Toast.makeText(InsertCommentsActivity.this, comments.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
}
public void noCommentsErrormessage() {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error!")
.setMessage("Please insert comments first")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
My code is supposed to work as follows: 1. When you click "Insert comments" button, a TextEdit field shows up and you can enter your comments 2. When you click "Display comments" button, the program check if there is some value stored in EditText field -if no it displays a dialog box saying "Please insert comments first" -if yes it displays the contents of the TextEdit field.
Any help would be greatly appreciated. Thanking you in advance.
Upvotes: 0
Views: 1347
Reputation: 11889
So you want to show EditText when Insert comments is pressed. Do it as follows: As Agrwal said initialize comments in oncreate and make it invisible(4)/gone(8) immediately by following code:
comments = (EditText) findViewById(R.id.commentsEditText);
comments.setVisibility(8);//to make it invisible
comments.setEnabled(false);//to disable it
Now in onclick() method of insertcom make the EditText visible/enabled by:
comments.setVisibility(0);//to make it visible
comments.setEnabled(true);//to enaable it
Upvotes: 0
Reputation: 34765
comments = (EditText) findViewById(R.id.commentsEditText);
must be in oncreate method and also in clicke event of one button you are using another button event so it is crashing.
Upvotes: 1