Trying to Read Edit box inside Custom Dialog... Getting FC - android

I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom 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">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???

You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);

Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)

Related

How to open an EditText View when clicking on an Action Bar Menu Options Item

I am working on an app (in Java) where the Action bar has an Options Menu of items/options (onCreateOptionsMenu resulting in the three vertical dots on the top right of the screen), so that when the user clicks on the three vertical dots, the menu expands showing the list of items/options. I want one of these items, when clicked, to open up an EditText view to enable the user to enter some text. I've spent quite some time researching this but I've not been able to find how it's done. Can somebody please provide the outline of how to achieve this or perhaps point me to an example.
Many thanks
create a dialog_layout.xml file and add below code
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="#+id/editTextDialog"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/edit_text_background"
android:hint="#string/about_routine_dialog_image"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textColor="#3C3B3B"
android:inputType="number"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<Button
android:id="#+id/buttonDialogSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit"/>
</LinearLayout>
then run below function calling showDialog when user click on that particular menu-item:
public void showDialog() {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
final EditText editText = dialog.findViewById(R.id.editTextDialog);
Button buttonDialog = dialog.findViewById(R.id.buttonDialogSubmit);
buttonDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editText.getText().toString(); //your text from editText
dialog.dismiss();
//do your work here
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}

Android: Null Pointer Exception in Dialog

I have a dialog box to be shown with two edittextboxes in it.
When I fill the fields and click on the submit button, I need to save the values in database. I am trying to get the values entered in the fields, but the Null Pointer Exception is raised.
My code is follows
View v = getLayoutInflater().inflate(R.layout.sample, null);
new AlertDialog.Builder(Context)
.setTitle(R.string.Details)
.setView(v)
.setNegativeButton(R.string.Cancel,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(R.string.Submit,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
final EditText txtname=(EditText)findViewById(R.id.editText1);
final EditText txtnumber=(EditText)findViewById(R.id.editText2);
Save(txtname.getText().toString(),txtnumber.getText().toString());
}
when I debug and checked, It shows the txtname and txtnumber values as empty. Where did I went wrong? I was using a layout to show the fields
The layout is,
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Name" />
<EditText
android:id="#+id/editText1"
android:layout_width="195dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:ems="10"
android:hint="FirstName LastName"
android:inputType="textPersonName" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Phone" />
<EditText
android:id="#+id/editText2"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:ems="10"
android:inputType="number"
android:maxLength="10" />
</TableRow>
Please help me out to solve this exception!!
Your txtname and txtnumber are null
i.e
final EditText txtname=(EditText)findViewById(R.id.editText1);
final EditText txtnumber=(EditText)findViewById(R.id.editText2);
when you are trying to access null reference you will get NullPointerException
Try to get them like this
final EditText txtname=(EditText)v.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2);
I think you are looking at the wrong place. Try using:
final View v = getLayoutInflater().inflate(R.layout.sample, null);
and than
final EditText txtname=(EditText)v.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2);
to find the EditText at the "v" view.
Since you haven't provided the full code and a stack trace, I'm taking a wild guess, but I'm assuming it's correct:
You're fetching the edit texts using findViewById from the activity's content view and not from the inflated view.
Change:
findViewById(R.id.editText1);
findViewById(R.id.editText2);
to
v.findViewById(R.id.editText1);
v.findViewById(R.id.editText1);
you need to get the views from the dialod's layout
public void onClick(DialogInterface dialog,
int which) {
final EditText txtname=(EditText)dialog.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)dialog.findViewById(R.id.editText2);
Save(txtname.getText().toString(),txtnumber.getText().toString());
}
please check the resource id it will also make cause for NPE.
findViewById is relative to the Activity. Your views are not in the activity, but rather in the dialog. You need to call
dialog.findViewById
from this scope.

Text cuts off in Dialog

FINAL EDIT: Okay well a total hack but at the moment I'm like "meh" it works. All I did to fix the issue was add android:lines="10" to the TextView and it showed everything like in 2.2 and ICS/JB. Total hack because it's not abstract at all but whatever :P..Thanks to everyone who helped!
I'm having trouble displaying text in a custom dialog with Gingerbread (API 10). The only text shown is the first line as shown here. In Froyo, ICS, and JB it displays with every line of text shown. I believe it's an XML thing, but I'm not sure. Where am I going wrong?
Edit: What I have tried:
-Changing the RelativeLayout to LinearLayout
-Adding in ScrollView
-Putting my string on one line
-using requestLayout() and forceLayout()
-putting the dialog functions in a separate class
-taking out the margins in my button
-using \n instead of HTML
-AlertDialog
-inputType and singleLine XML attributes on my TextView
-I think there's one or two more that I forget..
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:padding="5dp"/>
<View
android:id="#+id/bar"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#CCCCD0"
android:layout_below="#+id/dia_text"/>
<Button
android:id="#+id/dialogbuttongotit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dialog_confirm"
android:textSize="20dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/bar"/>
</RelativeLayout>
Here is the Code:
final Context context = this;
public void addListenerOnRectHelpButton() {
img = (ImageButton) findViewById(R.id.rect_img);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//create a new dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialogs);
dialog.setTitle("Rectangular Area");
// set the custom dialog text
TextView text = (TextView) dialog.findViewById(R.id.dia_text);
String dialog_rect_txt = "<u>Area of a Rectangular Channel</u><br />" +
"Height x Width (H x W)<br />--Example:<br />" +
"Height: 3ft, Width 5ft<br />" +
"H x W = 3ft x 5ft = 15ft<sup>2</sup><br />";
text.setText(Html.fromHtml(dialog_rect_txt));
Button dialogButton = (Button) dialog.findViewById(R.id.dialogbuttongotit);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
Having you tried calling requestLayout() on the TextView?
...
text.setText(Html.fromHtml(dialog_rect_txt));
text.requestLayout();
...
Why not just use an AlertDialog instead of building your own?
Dialog dlg = new AlertDialog.Builder(this).setTitle(
"Rectangular Area").setMessage(dialogRectText).setPositiveButton(
"Got It", clickListener).create();
dlg.show();

Add dynamically input text and buttons in activity android

I have an application with an input text where the users have to insert an information and a button "+" beside to input text.
I would like to make my form dynamic in a way that when a user pushes on "+" button appears dynamically another text input and another "+" button beside this one, the process is repeated in the same way.
I created and xml file, sample_content:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/attempt"
android:layout_alignParentBottom="true"
android:text="TextView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="36dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:text="+" />
<EditText
android:layout_width="229dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/addKey"
android:background="#drawable/inputtext_corner"
android:ems="10"
android:textSize="18sp" >
<requestFocus />
</EditText>
</RelativeLayout>
and in my Activity, AddDeviceActivity I put:
inflater = LayoutInflater.from(AddDeviceActivity.this);
Button addKey = (Button) findViewById(R.id.addKey);
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});
But this solution doesn't work because when I add the first input text and the first button, I don't know how to make the second button work in my AddDeviceActivity dynamicly
Just wondering whether you can do this:
Have your activity implement OnClickListener and add this method to your activity:
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
canvas.addView(childView);
((Button)childView.findViewById(R.id.addKey)).setOnClickListener(AddDeviceActivity.this);
}
And then change your initial code to use
addKey.setOnClickListener(this);
instead of an anonymous inner class.
I haven't tested this, but don't see why it wouldn't work.
check out this, pass null instead of canvas object in inflate() method
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, null, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});

Display an edit text box on click of a button

I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?
If you just want to "display an edit text box on click of a button" why don't you just..
Keep the EditText in your XML layout file for that activity below the Button where you want it..
XML set it's
android:visibility = "gone"
and making instance of that
EditText et=(EditText)findViewById(R.id.thatEditText);
in activity...in your button click event set
et.setVisibility(View.VISIBLE);
Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).
//xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<EditText
android:id="#+id/edtbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Activity
//oncreate
editText.setVisibility(View.GONE);
btn..setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setVisibility(View.VISIBLE);
}
});
If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.
e.g.
<RelativeLayout android:id="#+id/templayout">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
and you want to add edit text control after text View then on button click :
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.title);
EditText yourEditText = new EditText(this);
relativeLayout.addView(yourEditText, params);
Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.
YourEditText.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
YourEditText.setVisibility(View.VISIBLE);
}
});

Categories

Resources