Android - NumberPicker doesn't show + and - buttons - android

I wanna take an hour as input from user using a Number Picker, but it doesn't show the + and - buttons, it's just a values wheel

There are different ways that you can do this. I would take that string and then parse it down to see what you find then handle the string accordingly. Say you have an edit text field and it returns the string HourText. The way I would handle this, would be to use a number picker, set an upper and lower limit. Then you are returned a type int. From that you can use >=20 or >=50. In my mind using a numberPicker, and then handling the value accordingly sounds easier.

Related

Calculator app approach

as getting into android i decided to replace the default calculator with mine. A simple calculator with the 4 operational signs. I've been giving to all buttons the right behaviour, storing every number in a 'num' ArrayList(String) and signs in a 'sign' ArrayList(String).
What i wanted to do, was to then combine numbers and signs into a string, parse it into a float and getting a result. I thought this was one of the easy/simple ways to deal with it, since when you set a float like this:
float f = 6*4-5/2+3
it gives you the right result. but it clearly does not when starting from a String, like this:
String s = "6*4-5/2+3"
Float f = Float.valueOf(s)
Is there a way to getting a result from my 2 ArrayList(String)? In the negative case, what would be a doable approach (in the sense im not an experienced programmer)I?
I Think this approach is incorrect.
I would do the following:
You would have a Textview or Edittext as the calculator "screen" on top.
then you would have all your number and operation signs buttons.
Now, every number you press, it will append to the last one on the screen, using .append()
once you tap on an operator sign - two things will happen:
1) the number in the textView will be stored as a Float (using Float.valueOf(yourTextView); in a varibale, say firstNum.
2) you will save the operator you clicked in a second variable, say String calcOper.
Now, you enter your second number, and then you would press the Equals sign.
What will happen then is simply use a Switch of If expression.
If calcOper is "-" - then do firstNum- Current number shown on screen.
If calcOper is "+" - then do firstNum+ Current number shown on screen.
At last don't forget to set the text on the TextView the result.
Good luck!

Formatting fileds using android views

Is there any good simple way to format fields as I want.
I'd like to separate field value from field text. For example, I'd like to group number by 4, so when I set field's value to 123456789 it would show as 1 2345 6789, but when I get value I'll get number without spaces (123456789).
I think, this is the way widgets works on other tools, i.e. .Net VS. I can set there some mapping, formats and so on, so what is shown depends on value and format/mapping.
In android this must be done for passwords fields. It has text as value, but it shows some dots (value is mypassword and field shows **********).
I guess you just want to display the number in nice format, please use the APIs provided by PhoneNumberUtils.
static String formatNumber(String source)
static void formatNumber(Editable text, int defaultFormattingType)
You could set the formatted string to the text view for displaying nicely. But the saved content is still the raw content without any format.
Please refer to the SDK document at http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html

getting value 9.777123455E9 instead of 9777123455 from excel sheet

I am working on a project in which i am getting value from excel sheet(in assets android) and reflecting data in list view.
problem is:: phone no is not in proper format.
9.777123455E9 instead of 9777123455
When it's a phone number, you should always store the cell data as text, even if it consists only of digits, since a phone number is no mathematical number and when doing operations on it, you want to treat it as a string of characters (i.e. text).
If you input a phone number that looks to Excel like a mathematical number, it will interpret it as a number and in consequence will do things to it that make sense for numbers, but not necessarily for phone numbers, such as displaying it in scientific format.
To force Excel to treat your number as text, precede it with a single quote (apostrophe) when entering it. That is, enter into the cell:
'9777123455
It will be displayed without the single quote, just as you expect a phone number to be displayed and can be processed as text.
double d=9.777123455E9;
NumberFormat formatter = new DecimalFormat("#");
System.out.println(d);
System.out.println(formatter.format(d));
output
9.777123455E9
9777123455
E9 simply means multiply by 10^9
Update:
As #blubberdiblub mentioned, for phone numbers, it makes sense to change it to text. But for other cases, If you need to do mathematical operations leaving it in the scientific format works. You can right click on the column name and select formatting option to set the type of data the column will handle (number , text etc). If you want don't want to change the phone number to text and still see the number, simply increase the width of the column. The number will be shown full (without the "E").

Scan string for characters and return bounded text

I am writing a dictionary-type app. I have a list of hash-mapped terms and definitions. The basic premise is that there is a list of words that you tap on to see the definitions.
I have this functionality up and running - I am now trying to put dynamic links between the definitions.
Example: say the user taps on an item in the list, "dog". The definition might pop up, saying "A small furry [animal], commonly kept as a pet. See also [cat].". The intention is that the user can click on the word [animal] or [cat] and go to the appropriate definition. I've already gone to the trouble of making sure that any links in definitions are bounded by square brackets, so it's just a case of scanning the pop-up string for text [surrounded by brackets] and providing a link to that definition.
Note that definitions can contain multiple links, whilst some don't contain any links.
I have access to the string before it is displayed, so I guess the best way to do this is to do the scanning and ready the links before the dialog box is displayed.
The question is, how would I go about scanning for text surrounded by square brackets, and returning the text contained within those brackets?
Ideally the actual dialog box that is displayed would be devoid of the square brackets, and I need to also figure out a way of putting hyperlinks into a dialog box's text, but I'll cross that bridge when I come to it.
I'm new to Java - I've come from MATLAB and am just about staying afloat, but this is a less common task than I've had to deal with so far!
You could probably do this with a regular expression; something like this:
([^[]*)(\[[^]]+\])
which describes two "match groups"; the first of which means any string of zero or more characters that aren't "[" and the second of which means any string starting with "[", containing one or more characters that aren't "]", and ending with "]".
Then you could scan through your input for matches to this pattern. The first match group is passed through unchanged, and the second match group gets converted to a link. When the pattern stops matching your input, take whatever's left over and transmit that unchanged as well.
You'll have to experiment a little; regular expressions typically take some debugging. If your link text can only contain alphanumerics and spaces, your pattern would look more like this:
([^[]*)(\[[\s\w]+\])
Also, you may find that regular expression matching under Android is too slow to be practical, in which case you'll have to use wasyl's suggestion.
Quite simple, I think... As the text is in brackets, you need to scan every letter. So the basic recipe would be :
in a while loop scan every character (let's say, while i < len(text))
If scanned character is [:
i++;
Add letter at index i to some temporary variable
while (character # i) != ']' append it to the temporary variable
store this temporary variable in a list of results.
Some tips:
If you use solution above, use StringBuilder to append text (as regular string is immutable)
You might also want (and it's better, I think) to store starting and ending positions of all square brackets first, and then use string.substring() on each pair to get the text inside. This way you'd first iterate definition to find brackets (maybe catch unmatched ones, for early error handling), then iterate pairs of indices...
As for links, maybe this will be of use: How can I get clickable hyperlinks in AlertDialog from a string resource?

Can we added changing image/text on android app

Is there a way in android app that we can add some text or image and we can change that dynamically without adding new version. Like we have this text: Today's deal is: 1
then in a week if i want that text to show as: Today's deal is: 2.
Is there a way to do this, for an app which does not have a server and is stand alone app.
A text or image can be altered during run time; so how you choose to change that is up to you. You would have a simply switch statement depending on the day of the week for your specific example.
TextView tv = (TextView) findViewById(R.yourxml.name);
tv.setText("Monday!");
...
One way I can think of to achieve this is by writing a long value in the shared preferences for a Date object. Each time you start the application we will check if it's been 1 day since the value was saved. Also maintain the different string you want to display as string array in the resources.
I hope it helps...

Categories

Resources