I want to decode text before showing it to AlertDialog. So I used Html.fromHtml(String), but It is not working my case. I have also tried escapeHtml() but still no result.
My String is :
<div> The pampiniform plexus is a network of many small veins found in the human male spermatic cord. It is formed by the union of multiple spermatic veins from the back of the testis and tributaries from the epididymis.<br /> <br /> </div> <div> The veins of the plexus ascend along the cord in front of the ductus deferens. Below the superficial inguinal ring they unite to form three or four veins, which pass along the inguinal canal, and, entering the abdomen through the deep inguinal ring, coalesce to form two veins. These again unite to form a single vein, the testicular vein, which opens on the right side into the inferior vena cava, at an acute angle, and on the left side into the left renal vein, at a right angle.The pampiniform plexus forms the chief mass of the cord.<br /> <br /> </div> <div> In addition to its function in venous return from the testes, the pampiniform plexus also plays a role in the temperature regulation of the testes. It acts as a heat exchanger, cooling blood in adjacent arteries. An abnormal enlargement of the pampiniform plexus is a medical condition called varicocele.</div> <br>
My Code :
TextView tv = new TextView(getApplicationContext());
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(lstDescription.get(position)).toString());
final AlertDialog alertDialog = new AlertDialog.Builder(RandomQuestion.this).create();
alertDialog.setView(tv);
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
alertDialog.show();
How to show it as Html Text ???
to decode Html String you can use Html.fromHtml()
like
Html.fromHtml((String) htmlCode).toString();
ok then try this way :
String str = lstDescription.get(position)).toString().replace("<","<").replace(">",">").replace("nbsp;", " ");
tv.setText(Html.fromHtml(str );
Related
I need to add a country code to phone numbers to dial it from Android, but I would like to display the phone numbers without the country code. I can get the dialler working using setText() for the phone number (with country code) in Java and adding Autolink in the XML.
However I can't seem to find a way of displaying the phone number without the country code i.e. when using <a href= you can have different text to the actual hyperlink.
So I want to display 01234 567890, but dial +441234567890.
I've tried using this HTML in a string but it doesn't work:
01234 567890
You can remove first three characters of your string line:
numberString.substring(3);
and it will return you:
01234 567890
Hope it will help you :)
Use a ClickableSpan:
final SpannableString span = new SpannableString("01234 567890");
final ClickableSpan onClick = new ClickableSpan() {
#Override
public void onClick(View textView) {
//Launch your Intent to dial the number, with whatever number you like
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
span.setSpan(onClick, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
final TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(span);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.GREEN);
You may need to add clickable:true in your XML as well.
Or, if your TextView just contains the number, you can set a click listener on the entire TextView and launch your dial from there
I am trying to build a small game app for a school project. It should consist of two edit text fields and a button. In the first field, there should be two randomly generated numbers by the app. In the second field, a user should enter the sum of those two numbers. If the sum is correct, a toast will be displayed when the user clicks the button, saying "You guessed it". If it's not correct, then the toast will say "You did something wrong".
The part with the random numbers is working just fine. However, I simply cannot make it check what the user has entered in the second field. And because of that I can't write any onClick method for the button that would check whether the user has entered the correct sum of those two random numbers or not.
Any help or suggestion would be much appreciated, thanks!
But you don't really need the TextWatcher for what you want to do.
Here's a simple guess-try of what you're trying to do.
theButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int number1, number2;
//figure out the numbers from the first edittext, i'm pressuming there's a space between the numbers
String[] parts = firstEditText.getText().toString().split(' ');
number1 = Integer.valueOf(parts[0]);
number2 = Integer.valueOf(parts[1]);
//now we know the numbers, lets Toast whether the user was right or wrong
int enteredNumber = Integer.valueOf(secondEditText.getText().toString());
if(enteredNumber == (number1 + number2) ) {
Toast.makeText(getActivity(), "You guessed it right!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Something's not right...", Toast.LENGTH_SHORT).show();
}
}
});
I am currently trying to work with AlertDialogs.
Currently, I have an EditText in my AlertDialog, and want to restrict the input to only Integers. I have used a basic try and catch block to avoid the app crashing form a NumberFormatException.
However, I want to set it up so that when the user tries to press the button with the incorrect input, the input does not register and the Dialog is not cancelled.
UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
int RawInput = settings.getInt("UpperLimit", 12);
try {
RawInput = Integer.parseInt(input.getText().toString());
}catch (NumberFormatException se) {
Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
//Here I want the app to not register the click and prevent the dialog box from closing.
}
editor.putInt("UpperLimit", RawInput);
editor.apply();
Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
}
});
What method can I use to achieve this?
The basic trick here is to use the dialog's setCancelable to false and call dismiss() only when the input has been validated.
More info here: AlertDialog with positive button and validating custom EditText
Use can acheive input to only Integers this by both Java or XML.
Java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
XML
android:inputType = "numberSigned"
this may helps you in both way.
If you restrict user to allow only number then you can simply try with-
android:inputType="number"
and if you want to allow with floating number then try with-
android:inputType="numberDecimal"
and in any case if you want to disable dialog button then try this-
Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setEnabled(false);
will work for you :)
Simply use in EditText's xml code:
android:inputType="numberSigned"
This will let user input only integers. Hope this helps!
I have a button and I want it to change the text that I positioned above the button to something random everytime the user press it. How can I do it?
I want to look something like this:
"Hello"
**press**
"Why did you press?"
**press**
"Don't do that again, or..."
**press**
"You just did!"
Here is the code of the button and the text.
dontPressButton.setOnClickListener(
//Sets the button to wait for the press
new Button.OnClickListener(){
public void onClick(View V) {
//Selects the text field to be changed
TextView textChange = (TextView) findViewById(R.id.textChange);
//Changes the text
textChange.setText(string.textChange2);
}
}
);
There is no magical way to have it randomly change the text, you have to program it to do that. You are going to have to create an array of responses and then in the button's code, tell it to cycle through those responses. So for example:
private String[] responseArray = {"Hello", "Why did you press?", "Don't do that Again", "You Just did!" }
private int numTimesPressed = 0
dontPressButton.setOnClickListener(
//Sets the button to wait for the press
new Button.OnClickListener(){
public void onClick(View V) {
//Selects the text field to be changed
TextView textChange = (TextView) findViewById(R.id.textChange);
//Changes the text
//note this line will cause an error if there is not enough values in the array. You would have to write a catch for this
textChange.setText(responseArray[numTimesPressed++]);
//if you want random, you'll have to change the array index you are accessing to random value between the array's bounds
}
}
);
You'll definitely need an array of string responses. Afterwards, the first approach I would use is to use a random number generator, and then just link it back to your array. Unfortunately I'm not able to write the code, since I don't know the exact syntax, but in pseudocode:
string array[x]={"Hello","Why did you press?",...};//Number of string responses (in this case it's 4)
int random_number;
random_number=RandomNumberGenerator(1,x);//1 and x are the lower and upper bounds
switch (random_number)://If you don't know, switch is basically a simplified if-else system
case 1:print "Hello";
.........
There are many random number generators online that you can use, depending on the language. Hope this helped!
P.S: Maybe you'll want to fine-tune your responses to make them more natural. For example, you probably want to re-roll your response if you get two of the same one in a row.
I'm a starter on Android, mocking a contact list of a phone. Now I have a contact list, like the pic below, when I press the one item of the contact list, it pops up a dialog with two choices. And if I choose "Add to Black", another AlertDialog allows me to put this number to the blacklist. What I want to realize here is to automatically read the number of the item I picked, show it in the "number" blank, which doesn't require users to input again. But it turned out it didn't work, still nothing in the blank. The screenshots and codes of showing the add-black-dialog are below.
final View view = getLayoutInflater().inflate(R.layout.add_person, null);
AlertDialog alertDialog = new AlertDialog.Builder(MyTabHost.this).setTitle("Add a Black Number")
.setView(view).setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
inputNumber.setText(dataList.get(arg2).get("number"));
String number = inputNumber.getText().toString();
String remark = inputRemark.getText().toString();
Map<String, String> map = new HashMap<String, String>();
map.put("remark", remark);
map.put("number", number);
map.put("display", Utils.formatPhoneNumber(number));
if (MyTabHost.blackList.get(0).containsValue("You don't have any black number")) {
MyTabHost.blackList.removeAll(MyTabHost.blackList);
}
MyTabHost.blackList.add(map);
int i = mySharedPreference.getBlackSize() + 1;
mySharedPreference.saveBlack(remark, number, i);
mySharedPreference.saveBlackSize(i);
dialog.dismiss();
new AlertDialog.Builder(MyTabHost.this)
.setMessage("Black number succesfully added")
.setPositiveButton("OK", null).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
alertDialog.show();
Actually I'm thinking whether I am using wrong view. As the codes state above, I use final View view = getLayoutInflater().inflate(R.layout.add_person, null);to get the new view of the dialog, which add_person.xml here is my layout.
The reason why I wondering about the wrong view is that something weirder happened: when I manually inputed the number and remark(say I pressed the contact "Jack"'s number:8871203459 and "Jack" as a remark) and pressed "Add", meanwhile the things in the blanks suddenly change to some numbers else(some numbers I got in other activities), like below, and the black data stored was also the odd wrong number.
That's odd because I did write the codes of getText(), and saved it:
EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
inputNumber.setText(dataList.get(arg2).get("number"));
String number = inputNumber.getText().toString();
String remark = inputRemark.getText().toString();
Map<String, String> map = new HashMap<String, String>();
map.put("remark", remark);
map.put("number", number);
map.put("display", Utils.formatPhoneNumber(number));
This is a long and boring problem. Thanks for your reading and help...
Just moved relevant comment down into an answer.
That setText is only being run once you click the add button on your dialog. Move both find views and the setText out of the dialogs on click
use toString() Method for converting text into string and then place it in appropriate place. I faced a same problem and solved by this method.
I solved my second problem in this post. It was a careless mistaken by querying wrong ArrayList. This time the odd numbers don't exist. But the EditText is still not able to show the characters with what I want. Pls help if possible.
Update: I've found that though the EditView is not able to set the values visible to the users, it does get the value - I've tested that. What's odd is, whatever I input in the two blanks(should have shown the values) and press "Add" button, the app will always save the original value I try to let them show.
Specifically:
EditText inputNumber = (EditText) view.findViewById(R.id.inputNumber);
EditText inputRemark = (EditText) view.findViewById(R.id.inputRemark);
inputNumber.setText(contactList.get(arg2).get("number").toString());
inputRemark.setText(contactList.get(arg2).get("name").toString());
String number = inputNumber.getText().toString();
String remark = inputRemark.getText().toString();
Map<String, String> map = new HashMap<String, String>();
map.put("remark", remark);
map.put("number", number);
My EditTexts are able to get the contactList.get(arg2).get("number").toString() and contactList.get(arg2).get("name").toString(), but just don't show them. And whatever else I input on the EditTexts, they will pass the two values above, instead of what I newly input, though I've specified String number = inputNumber.getText().toString() and String remark = inputRemark.getText().toString().
Finally update: Problem solved, it's not a hard tech one but a programming logical mistake. Please refer to #ElefantPhace's answer on the upper side if any one encounters same problem. Thanks all!