Android EditText view being interpreted as ImageView? - android

In my app, I am creating a dialog, like so:
dialogview = inflater.inflate(R.layout.registration_dialog, null);
dialog = new AlertDialog.Builder(this);
dialog.setView(dialogview);
And then, assigning several views to variables:
username = (EditText)dialogview.findViewById(R.id.username);
password = (EditText)dialogview.findViewById(R.id.password);
confirm = (EditText)dialogview.findViewById(R.id.confirm);
rememberme = (CheckBox)dialogview.findViewById(R.id.rememberme);
autologin = (CheckBox)dialogview.findViewById(R.id.autologin);
All the variables are appropriately defined, before my onCreate:
private EditText username;
private EditText password;
private CheckBox rememberme;
private CheckBox autologin;
private EditText confirm;
When I try and create the dialog, however, the app crashes. I checked the eclipse debugger, which reports the error:
java.lang.ClassCastException: android.widget.ImageView
on the line
confirm = (EditText)dialogview.findViewById(R.id.confirm);
The XML definition for the confirm field is as follows:
<EditText android:id="#+id/confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword">
</EditText>
I am beyond confused - there isn't even any ImageViews inside my dialoglayout. Is this some strange glitch? Any ideas?

The R.java file needs to be refreshed. Try to Clean your project and compile again. \

Related

Android Dialog show HTML in TextView from String

I'm trying to show HTML code in a dialog but the HTML needs to be stored in a string which is first put into a textview.
I have found many other questions about something similar but most are setting the text in code rather than in strings.xml. None I found seem to use a 'dialog to textview to string with html' process.
I've done so far what I think should work but I'm getting an error now (below). I've tried other ways but cannot get the HTML effects to show.
I'm still new to Android so any help to see what I'm doing wrong would be great. Thanks.
preferencesActivity.java - OnCreate
Preference myPref = (Preference) findPreference("pref_showHelp");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Dialog dialog = new dialog(preferencesActivity.this);
helpFile_textView = (TextView) findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text))); // Line 44
dialog.setContentView(R.layout.helpfile);
dialog.setTitle("Help");
dialog.show();
return false;
}
});
}
helpfile .xml
<TextView
android:id="#+id/helpFile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/helpFile_text" />
</LinearLayout>
Strings .xml
<string name="helpFile_text">Test of <b>bold and <u>underline</u> and bullet •</string>
Logcat
E/AndroidRuntime(1516): java.lang.NullPointerException
E/AndroidRuntime(1516): at com.example.newcalc.preferencesActivity$1.onPreferenceClick(preferencesActivity.java:44)
Change
helpFile_textView = (TextView) findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));
dialog.setContentView(R.layout.helpfile);
to
dialog.setContentView(R.layout.helpfile);
helpFile_textView = (TextView) dialog.findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));
You have to inflate the layout that you want to use in the Dialog before you can find it using findViewById
Also since the id you want to find (i.e. helpFile_textView) is in the layout that you inflated in dialog you have to use dialog.findViewById instead of findViewById to get the TextView
Also
Dialog dialog = new dialog(preferencesActivity.this);
should be
Dialog dialog = new Dialog(preferencesActivity.this);
but i think that is a typo.
try this
dialog.setContentView(R.layout.helpfile);
helpFile_textView = (TextView) dialog.findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(preferencesActivity.this.getResources().getString(R.string.helpFile_text)));
for more correction refer #Aproov's answer.

How to display cursor at the end of text in TextView?

I wanted to display blinking cursor at the end of the text in TextView .
I tried by android:cursorVisible="true" in TextView But no go .
Even i tried text.setCursorVisible(true); Doesn't work .
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="#null" />
Does any one know any solution for it ?
First of all you should use EditText in place of TextView for taking input. If still the cursor doesn't blink, set the android:cursorVisible="true"attribute in xml file, it should make the cursor blink. If your cursor is not visible in edit text, that's also a reason one can't see the cursor blinking. Set android:textCursorDrawable="#null". This should solve your problem
<EditText
android:id="#+id/editext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#null"
android:cursorVisible="true">
</EditText>
In your activity class, add this code as well.
EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());
There is a solution for this.
I had to do this when I was making a terminal app and I used a simple runnable put a cursor at the end and make it blink.
I made 3 class variables:
private boolean displayCursor;
private boolean cursorOn;
private String terminalText;
private TextView terminal; // The TextView Object
terminalText keeps track of the text to be displayed.
Created a class method that runs the runnable the first time
private void runCursorThread() {
Runnable runnable = new Runnable() {
public void run() {
if (displayCursor) {
if (cursorOn) {
terminal.setText(terminalText);
} else {
terminal.setText(terminalText + '_');
}
cursorOn = !cursorOn;
}
terminal.postDelayed(this, 400);
}
};
runnable.run();
}
And initiated the variables and called runCursorThread() in onCreate()
cursorOn = false;
displayCursor = true;
runCursorThread();
I think you should go for EditText. You can set its background and make it appears like TextView with below code.
Step 1
<EditText
android:id="#+id/edtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
Step 2
EditText edt = (EditText) findViewById(R.id.edtText);
edt.setSelection(edt.getText().length());
Output
Finally Fixed this Using the EditText as per #Chintan Rathod advice.
<EditText
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/> //reference to #Chintan Rathod.
Code
EditText text=(EditText) findViewById(R.id.text);
text.setText("hello");
text.setSelection(text.getText().length()); // reference to #Umer Farooq code.

android.widget.Button cannot be cast to android.widget.EditText

Developing my first Android calculator application, I succeeded in updating a TextView in a new activity by passing the answer via an intent, but this requires the user to hit Back to perform another calculation. I'm trying to make the doCalculation button update a simple TextView in the MainActivity and getting the error:
06-22 11:08:17.318: E/AndroidRuntime(31328): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
Here's my code:
/** Called when the user clicks the Calculate! button */
public void doCalculation(View view) {
// Do something in response to button
int answerInt;
String answer;
EditText numberOne = (EditText) findViewById(R.id.number1);
EditText numberTwo = (EditText) findViewById(R.id.number2);
int numberOnee = Integer.parseInt(numberOne.getText().toString());
int numberTwoo = Integer.parseInt(numberTwo.getText().toString());
answerInt = numberOnee * numberTwoo;
answer = Integer.toString(answerInt);
TextView homeAnswerView = (TextView) findViewById(R.id.homeAnswerView);
homeAnswerView.setTextSize(40);
homeAnswerView.setText(answer);
}
For reference, here's the code that worked successfully launching a new activity:
// Called when the user clicks the Calculate! button
public void doCalculation(View view) {
// Do something in response to button
int answerInt;
String answer;
Intent intent = new Intent(this, DisplayCalculationActivity.class);
EditText numberOne = (EditText) findViewById(R.id.number1);
EditText numberTwo = (EditText) findViewById(R.id.number2);
int numberOnee = Integer.parseInt(numberOne.getText().toString());
int numberTwoo = Integer.parseInt(numberTwo.getText().toString());
answerInt = numberOnee * numberTwoo;
answer = Integer.toString(answerInt);
intent.putExtra(EXTRA_MESSAGE, answer);
startActivity(intent);
}
UPDATE, the XML for reference:
<EditText
android:id="#+id/number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:singleLine="true" />
<Button
android:id="#+id/calculateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/number2"
android:layout_alignRight="#+id/number2"
android:layout_below="#+id/number2"
android:layout_marginTop="14dp"
android:onClick="doCalculation"
android:text="Calculate!" />
Thank you for your help,
-Michael
It seems like either R.id.number1 or R.id.number2 is a Button. Check your XML and make sure it's an EditText.
Edit: Original answer didn't work, but cleaning the project solved the problem.
I've just had this problem. It seems that the xml layout file is not compiled properly. Or rather it is not included in the list of changed files to be compiled.
i was having the same situation but i found out that there are two textviews with same ids in different activities so i changed one of them and the program ran clearly so check the ids of all your edittext and buttons and change the samilier even if they were in other activities and i think it will run with out any problems
I followed the steps in the answer (cleaned and then made sure it's the id) and noticed that going to the source of my EditText R.id brings me to the EditText. Thought this is definitely not a IDE cache problem.
What I did do is to change the LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
to
android:layout_width="match_parent"
android:layout_height="match_parent"
For some reason it fixed the issue (I had this whole layout wrapped in something else that I just recently removed).

First Android App - Can't convert EditText to integer value

I'm trying to write my first Android app. It will take a number input by a user in an EditText field, convert it to an integer, then find the factors. I want to port this from a Java program that I wrote before. I have stubs working to the point that I have a UI, but I haven't yet ported the code that will find the factors. I'm stuck trying to convert the EditText to an integer. If I insert either of the following lines, the program crashes in the emulator. Log.Cat says, "Caused by NumberFormatExcepion: Unable to parse '' as an integer."
Any suggestions are appreciated.
userNumber is the name of the value taken from the EditText field, and the EditText field is also named userNumber. I don't know if that's bad form or not. I want to assign the value of userNumber to the integer value userInt. userInt will then be factored.
Either of these approaces will cause the problem:
userNumber = (EditText) findViewById(R.id.userNumber);
userInt = Integer.parseInt(userNumber.getText().toString());
Integer userInt = new Integer(userNumber.getText().toString());
The EditText block of XML looks like this:
<EditText
android:id="#+id/userNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
Here is the relevant code from the class:
public class AndroidFactoringActivity extends Activity {
// Instance Variables
EditText userNumber;
Button factorButton;
TextView resultsField;
int factorResults = 1;
int userInt = 0; // This comes out if using Integer userInt
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultsField = (TextView) findViewById(R.id.resultsField);
factorButton = (Button) findViewById(R.id.factorButton);
userNumber = (EditText) findViewById(R.id.userNumber);
// userNumber is also the name of the EditText field.
// userInt = Integer.parseInt(userNumber.getText().toString());
// Integer userInt = new Integer(userNumber.getText().toString());
resultsField.append("\n" + String.valueOf(userInt));
//Later, this will be factorResults, not userInt.
// Right now, I just want it to put something on the screen.
}
}
You're trying to parse the int in your onCreate method, which occurs before the user has a chance to enter anything into the EditText. Hence the exception from trying to parse an empty string.
You'll have to either make a button to press, which will then parse the int out of the EditText, or attach a listener to the EditText that will parse it when something is typed into it.

How do I get edittext value from a custom AlertDialog where retrieval of values is not in the onClick?

What I am trying to do is to create a custom dialog that overrides an AlertDialog.
What it is supposed to do is get some text (at least 2 strings) and then for each of those strings it is supposed to be able to get more information, but I want to do this in custom dialogs.
So what is supposed to happen is a user can enter 2 people in an activity screen, and then for the first person, you get a custom dialog and that person can enter three words, and then it jumps to the next custom dialog (exact same layout I am inflating) and the second person can enter some words.
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinLay_Enter_Words"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/TextView_AddPlayerWord_Instruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/help_text_size"
android:textStyle="bold"></TextView>
<EditText
android:id="#+id/EditText_Word1"
android:layout_height="wrap_content"
android:maxLength="20"
android:layout_width="match_parent"
android:maxLines="1"></EditText>
<EditText
android:id="#+id/EditText_Word2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"></EditText>
<EditText
android:id="#+id/EditText_Word3"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1"></EditText>
</LinearLayout>
And this is part of the code:
protected Dialog onCreateDialog(int id) {
switch (id) {
case NOUN_INPUT_DIALOG_ID:
Dialog returnedDialog = initWordDialog();
return(returnedDialog);
}
return null;
}
It calls initWordDialog():
private Dialog initWordDialog() {
LayoutInflater inflater = LayoutInflater.from(this); //(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogLayout = inflater.inflate(R.layout.word_entry_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
...
TextView v1 = (TextView) dialogLayout.findViewById(R.id.TextView_AddPlayerWord_Instruction);
...
v1.setText("SomeText");
builder.setView(dialogLayout);
builder.setTitle(R.string.enter_word_title);
builder.setPositiveButton("Next", onNextSubmit);
AlertDialog wordBuilderDialog = builder.create();
return wordBuilderDialog;
}
I think what I am trying to find has been discussed to some degree here:
Value of EditText in Custom Dialog
Android - Custom Dialog - Can't get text from EditText
How to add two edit text fields in an alert dialog
The problem, I believe, lies here, where all of the examples everyone has their onClick in the same function as their onCreate. My stuff was a bit more complicated and I wanted to separate out the functions; however, as a result, I am now unable to access any of the EditText variables.
Here is my onClick implementation:
private DialogInterface.OnClickListener onNextSubmit = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (setPlayerWords()) {
...
}
};
The part that matters is I don't even get to the part where I'm accessing the edittexts until setPlayerWords is called, and this is where it is failing:
public boolean setPlayerWords() {
PMGamePlay pmObj = (PMGamePlay) getApplicationContext();
String[] playerWords = new String[pmObj.numberOfWordsPlayersGetToInput()];
//LayoutInflater inflater = LayoutInflater.from(this);
//View dialogLayout2 = inflater.inflate(R.layout.word_entry_dialog, null);
//setContentView(R.layout.word_entry_dialog);
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.LinLay_Enter_Words);
final EditText w0 = (EditText) myLayout.findViewById(R.id.EditText_Word1);
final EditText w1 = (EditText) myLayout.findViewById(R.id.EditText_Word2);
final EditText w2 = (EditText) myLayout.findViewById(R.id.EditText_Word3);
String test = w0.getText().toString();
playerWords[0] = w0.getText().toString();
playerWords[1] = w1.getText().toString();
playerWords[2] = w2.getText().toString();
...
return true;
}
I initially tried re-inflating, but that seemed to reset and while the edittexts would not be null, they were reset to have "" in their values.
Then I tried to setContentView on my xml file, but that still gave me a null value.
Now, I just try and simply access the linearlayout, and that also returns a null value. If I just try to access the edittexts by their id directly without first going through its parent linearlayout, it also returns a null value.
At this point, I'm not sure what to do other than to cram everything that I have in these separate functions into the same single onclick, but I really don't want to do that. Is there nothing else I can do to access these edittexts?
Any help would be greatly appreciated!
Have you tried using the long version of inflate inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) ? I know that if you don't use this method there can be some issues with it grabbing layout characteristics, so might be causing the issue. For the viewgroup you should pick the parrent view for the alert and usually want attachToRoof = false;

Categories

Resources