How to set focus to the text after the hint in EditText? - java

I'm interested in you can set the focus to the text after the prompt to EditText? If no such attribute for xml layout? At the moent I still looked like this.
need
EDIT :
The fastest and working answer given Asok
I also found a similar way:
EditText.append("60"); // 60 or your text in EditText

A hint is no real text, it disappears after the user types something. Assuming the cursor would be at the end of the hint what behaviour would you expect when the user presses a button and the hint disappears?
What you can do is set a default text in the XML via
android:text="60"
or in code via
editText.setText("60");
and on focus jump to the end of the EditText via
editText.setSelection(editText.getText().length());

If I understand correctly you are looking to place the cursor behind the hint text, if this is correct then it is not possible, you'll have to remove hint and use setText instead, like so:
EditText et = (EditText)findViewById(R.id.sixtyseconds);
et.setText("60");
et.setSelection(et.getText().length());

Related

how can I assign the value of keyword when I click the button

For example, in a calculator, there are 9 integers on the phone screen and else just like + - / * .... maybe something else. But I want to ask you one question. How can I append the value of keyword of the button to the display. Our developer created the button as text "5". When the user clicks that button, I want to display the 5 value in the edittext without clicking the number 5 value in the built-in keyboard in Android. Can anyone explain how to do this please?
"Keyword" means number 1 - 9 on the keyboard. Normally, when we create the EditText, we can easily input the keyword in EditText by using built-in keyboard. But I don't like it .
I recommend you to use a TextView instead of EditText to display the numbers. But if you insist, it's also fine. If you look at the docs, you will see there is a setText method in TextView class. And because EditText extends TextView, it will have that as well.
You can use the setText method to set the text of the text view and use getText to get it. That is very simple. In a calculator app, you want to append (which means add something at the end of a string) the number to the existing text, right? So let me show you how to do this.
You can use this code to append "5" to the text view.
String originalText = yourTextView.getText ().toString();
yourTextView.setText (originalText + "5");
Just in case you don't know how to get the TextView:
yourTextView = (TextView)findViewById(R.id.youtTextViewsId);

How to add a permanent text which is tappable and editable along with hint, which user can change in EditText of android?

I looked on google but didn't find what exactly i am looking for.
I want a edit text to have text or image which i can change by tapping it and selecting new text or image. Also i want hint followed by that text or image.
I tried multiple experiment and was able to add text(by changing selection location and adding text) and image(using drawable addition from android code)
For text :
edt.setText("Fixed Text");
Selection.setSelection(edt.getText(), edt.getText().length());
Image through XML:
android:drawableLeft="#drawable/ic_launcher"
But when i add text, i won't be able to add hint. In case of image hints get added. Also i want my permanent text and image to be tappable and updatable.
Any help would be really helpful. I want $ to be fixed and tappable, and want to change it to different currency. but want 52.63 to be hint and can be editable by user tap.
EDIT to give more clarity:
Example: i want to add part of the text as permanent and part as hint for example $ 52, so $ is permanent and not editable through user, he can select it through list view by tapping on $, but 52 he can change using android keyboard.
I am not quite sure about the whole part about the image being placed into the EditText as a hint, but if you wanted to create the $52.63 as a hint in the EditText, all you would have to do is:
edt.setHint("$52.63");
in your onCreate() method. I am not sure if this is what you are looking for, but this is a simple way to add a hint to a textEdit. If you wanted the hint to change when the user tapped the screen or something like that, I would look into adding a tap listener of the android component that you desire to be tapped to change the hint, and then modify the hint using the method above.

Android: Adjusting EditText font size to fit text to one line

I am making an app that has two EditText views, txtOne which is used for input, and txtTwo which is used for output. txtOne is editable, and txtTwo is not.
I am looking for a simple way to scale the font size of the text in these two views so that it always fits within the view without wrapping to a new line.
I have found a few implementations of this using custom views extending TextView, but they aren't fit for an EditText view, or use android.graphics.Paint, which is not what I am looking for.
Is there any way to check to see when text in an EditText view is wrapping? If so, then it would be easy enough to do something like:
if(txtOne.isWrappingText()) {
txtOne.setTextSize(txtOne.getTextSize() - 2);
}
Does anyone know of any way to detect this or an alternative solution?
I deleted my post
Basically the suggested code contained a TextWatcher for the EditText, this helped to answer the question. But the code itself was just wrong (I tested it meanwhile).
I suggest to read this question and answers because they adress the very same issue...

Java StyledDocument: How can I ensure that what the user types never appears already styled?

The user types some text. When they press a button, what they have typed is split up and colour coded:
colors.setCharacterAttributes(characters, tokens[x].length(), formatBlue, true);
Using a set of rules.
When they make an edit between the position as defined by characters and the position characters + tokens[x].length() it comes up in my formatBlue style.
However, I would like it to be in black until the user next presses the 'colour code' button I have.
In short: the desired effect is that everything that is typed should always in black, until it has been phrased and coloured differently by the program.
So far, the best solution I have is to detect when the caret changes position, and then do:
setLogicalStyle(textArea.getCaretPosition(), formatBlack)
Any better suggestions would be appreciated.
the desired effect is that everything that is typed should always in black
You could try using a DocumentFilter. If the text about to be inserted does not contain an attribute then you assign the default black attribute.
Read the section from the Swing tutorial on Implementing a Document Filter for more information.
You can get EditorKit from your JEditorPane. It's StyledEditorKit instance. So you can get InputAtributes from the kit and remove all the attributes. Thus all the typing will use the empty AttributeSet.

How can I create an AutoComplete popup in a JTextPane in Java?

I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse.
I think the appropriate class for displaying info on top of another component is JPopupMenu, which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user to choose from, a menu seems appropriate.
To check for text changes, you'd add a DocumentListener to the document that's wrapped by the JTextPane; you can access it using getDocument().
To find out where the cursor (actually, the caret) is, you can use getCaretPosition(). That returns the caret's position within the text stream as an int. You can use modelToView() to translate that position to actual (x,y) coordinates. That in turn will tell you where to show your menu.
You can use addKeyListener() to catch keyboard events on your JTextPane, like hitting Ctrl-Space.
The combination of all that should allow you to do what you're looking to do.
You can also use http://fifesoft.com/autocomplete/. You can install it on any JTextComponent.
For things like this you probably should consider layered panes so your auto-complete suggestions appear in the correct place and z-order.
Furthermore you will have to look for changes in the JTextPane to know when the user is typing and you will need a parser that understands what is typed so you can offer the feature only at appropriate points.
It's not quite clear what exactly your problem is and what you got so far.
I achieved this by adding a key listener to the JTextPane and checking for CTRL + Space keystrokes. When the appropriate key combo was detected the listener went off and looked up the list of possible matches based on the characters directly to the left of the cursor at the time of the key press and found the best matches and displayed them to the user in a JPopup. If there was an exact match then it simply replaced the partial text with the match. If no matches were found an option was given to the user to add the text that they had already typed, edit it and record it into the list of acceptable data.
We use jide. They have a lot of components that help you do this kind of thing really easily

Categories

Resources