I am beginner in android but not in Java , and have a problem.
Took hours losses in my application ignores keyboard to enter and you put the code below.
What I want is that when I do a thing to enter and go. Since it seems redundant and have not optimal OK button when you have an OK in the Enter itself.
t_Num = (EditText) findViewById(R.id.eTT);
...
t_Num.setFocusableInTouchMode(true);
t_Num.requestFocus();
t_Num.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("entra en Onkey" + event.toString());
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)){
tV.setText("OK");
System.out.println("Press OK");
return true;
}
return false;
}
});
Whit this code i get spam the keypress , but not spam the keypress OK.
And XML:
<EditText
android:layout_width="75dp"
android:layout_height="wrap_content"
android:inputType="numberSigned|phone"
android:digits="-0123456789"
android:ems="10"
android:id="#+id/eTT"
android:imeActionLabel="#string/OK"
android:maxLength="6"
android:maxLines="1"
android:layout_alignBottom="#+id/tV2_1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/btnOk"
android:layout_toEndOf="#+id/btnOk" />
thanks for help
PD : I search in this page but not get solution , font1 , font2 , etc...
I use search :)
Try this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
//your code
return true;
}
return false;
}
});
This is if the editText has
android:imeOptions="actionGo"
in its xml. Same should apply with Enter. Just change the EditorInfo action.
EditText is a subclass of TextView.
Related
I have a TextView that contains a DrawableRight, what I want to do is detecting when the user presses that icon in drawableRight, is that possible ? and if it is how can I do it ?
PS: I am working inside a fragment
TextView XML
<TextView
android:id="#+id/mTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textSize="22dp"
android:drawableRight="#mipmap/icn" //this is the drawable
/>
mTitle.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (mTitle.getRight() - mTitle.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
//drawable pressed
return true;
}
}
return false;
}
});
i try to jump from a TextView element to the next editable Object in my Android activity. The next Object is a element of ListView, which is editable.
I tried to use the imeOptions but it still performs a newline in the TextEdit Object.
My XML looks like this:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="#string/question_hint"
android:id="#+id/questionField"
android:textColor="#color/text_color"
android:textCursorDrawable="#null"
android:maxLength="#integer/question_length"
android:imeOptions="actionNext"/>
<!--android:minLines="2"-->
I also added a codeline in the oncreate method of the activity:
questionField = (EditText) findViewById(R.id.questionField);
questionField.setImeOptions(EditorInfo.IME_ACTION_NEXT);
But it doesnt work. Anyone has an idea? Thanks
Try something like this:
questionField.setOnEditorActionListener(
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView exampleView,
int actionId, KeyEvent event)
{
if ((actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_GO
|| (event != null
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER)))
{
return nextView.requestFocus() // the next view you want the focus to be on
}
else
{
return false;
}
}
});
I have looked tutorials on how to make my imagebutton capable of drag and drop. With the code I have the imagebutton just disappears when I click it. and when I click anywhere else it does not come back.
Here is my code for the imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getApplicationContext(), "in the movement", Toast.LENGTH_SHORT).show();
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins((int)event.getRawX() - 25, (int) event.getRawY() - 50, 0, 0);
layout = (LinearLayout) findViewById(R.id.gllayout);
layout.removeView(mainHut);
layout.addView(mainHut, params);
}//end if
}//end else
return false;
}//end onTouch function
});
here is the xml for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgm" >
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mainhut" />
</LinearLayout>
Any help is appreciated. Thanks.
To answer my own question...this is how I fixed my issue. It took me a long time to figure this out but here it is. The parameters are a little messy when it comes to my own imagebutton so I will have to figure out what to use to keep my finger in the center of the button, but this moves the button like I was wanting:
button.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (me.getAction() == MotionEvent.ACTION_MOVE )
{
LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()), (int)event.getRawX() - v.getWidth()/2, (int)(event.getRawY() - v.getHeight()));
v.setLayoutParams(params);
}
return false;
}
});
I am currently creating a Hangman android application and I am having trouble registering inputted letters and updating the game. I input a letter with the onscreen keyboard into the EditText created, and nothing happens.
Below is the code I am using:
// Setting up user input
input = (EditText) findViewById(R.id.inputguess);
input.setFocusable(true);
input.setFocusableInTouchMode(true);
//Getting user input
input.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String temp;
String newLetter;
newLetter = input.getText().toString();
temp = (String)enteredText.getText();
if (temp.indexOf(newLetter.toUpperCase(Locale.ENGLISH)) >= 0) {
input.setText("");
return true;
}
input.setText(""); // clearing input
entered += newLetter.toUpperCase(Locale.ENGLISH); // adding inputted letter to the entered string
enteredText.setText(temp + newLetter.toUpperCase(Locale.ENGLISH));
word.setText(hideString(text, newLetter.toUpperCase(Locale.ENGLISH)));
The XML code of my EditText is the following:
<EditText
android:id="#+id/inputguess"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/guessedwords"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="24dp"
android:ems="10"
android:hint="#string/guessletter"
android:inputType="text"
android:maxLength="1"
android:singleLine="true"
android:imeOptions="actionDone">
</EditText>
After researching, the only reason I can think of is the type of listener being used, but I am not entirely sure. Any help would be greatly appreciated (Let me know if I should add more of my code).
Actually, the OnKeyListener only works with hardware keyboards. To make use of the software (onscreen) keyboard, you must add a TextWatcher
editText.addTextChangedListener(new TextWatcher() { ... })
hello i built a appliction that need to rate photos and do the Average on the rating bar now i want to set that if i am not choos nothing on the rating bar so it will not update to the Photos Data Average like lets say that the raiting bar is on 3/5 start (*)
so if i click on it will not update the number '3'
#Override
public void onClick(DialogInterface dialog, int id) {
rate = ratingBar.getRating();
if(ratingBar.?? hes not been clicked ??)
{
}
else{
upDate();
}
textView.setText(String.valueOf(photosRatingInfo.getRate()));
//Logic
}
Sorry for the poor description ):
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:rating="3.0" />
Here i set the default rating 3.0
RatingBar ratingBar;//Initialize Your RatingBar Here
Use setOnRatingBarChangeListener Listener
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar,
float rating, boolean fromUser) {
String answerValue = String
.valueOf((int) (ratingBar.getRating()));
}
});
If you don't want to start the bar from 0 as i suggested in the comment, you can do two things:
derive from RatingBar and override onTouchEvent()
use OnTouchListener, like so:
ratingBar.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// TODO perform your averaging action here
}
return true;
}