When my app starts, the user needs to touch on the screen before the real action starts. I have a textView which gives the hint to touch the screen.
After the screen is touched, I want the text to get invisible. Right now the textView never disappears and always stays in the front.
public class MainActivity extends Activity implements OnGestureListener
{
public boolean touched = false;
TextView mMyView;
public void onTouch()
{
mMyView.setVisibility(View.GONE);
touched = true;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mMyView = (TextView)findViewById(R.id.textView6);
if(touched == true)
{
}
}
}
1.Always use if(something) if you want to see if it's true/false instead of writing if(something == true) [something is a boolian assigned with value true.]
2.If you point your views xml to a method using android:onClick like below,
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="onTouch" />
.
What's the point of implementing OnGestureListener?
If i do this onCreate i initialize my view
View myView = findViewById(R.id.my_view);
3.If i really want a touch i will do this
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events --> tv.setVisibility(View.INVISIBLE);
return true; // if you return false with this then the listener will not be called for the subsequent ACTION_MOVE and ACTION_UP string of events.
}
});
Now you can see in the 3rd ones parameter there is a MotionEvent, you can identify the motion ACTION_DOWN , ACTION_MOVE and ACTION_UP
Now think have you ever used them. You got an idea in your head about a touch so tried to use touch events .. But you don't use them. So it does the same as what onClickListner does in your case. If you want motions use that 3rd one i gave.
But simply you can use
// view is the background layout
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here --> Hide your text tv.setVisibility(View.INVISIBLE);
}
});
Those view onClickListner or setOnTouchListener you can directly use them inside onCreate or keep them inside a method and you can call that method from onCreate. Why to keep a boolean? It's nothing major
Note i considered myView as the background layout not your textView , background is the one you click / touch
So now you changed the questions code several times and I hope it´s the final change. Only than my answer could help.
You have done this in your onCreate():
if(touched == true)
{
tv.setVisibility(View.INVISIBLE);
}
But this is executed directly and has nothing to do with you onTouch() method. Let´s assume your onTouch() works correctly. Make the TextView global:
TextView mMyView;
initialize it in onCreate():
mMyView = (TextView)findViewById(R.id.textView6);
and then hide it in onTouch():
onTouch(View view){
mMyView.setVisibility(View.GONE);
}
But you have to be sure that your method onTouch() works. You can make a Toast or a Log to check. You have to be sure that:
-The TextView is inside your layout xml that you set with setContentView(R.layout.activity_game);
-The onTouch() method is declared in your TextView's xml attribute
android:onClick="onTouch"
and set clickable of your TextView to true:
android:clickable="true";
EDIT
If you implement onGestureListener() I guess the touch event is consumed by the listener and your TextView did not recognize onTouch(). If you don´t do any gesture detection in your activity, then remove this implementation.
You are checking if screen was touched in onCreate() which is called only once at the start of the activity. Initialize TextView globally and set its visibility inside onTouch(View v, MotionEvent event)
Also your onTouch() isn;t correct. You should override public boolean onTouch(View v, MotionEvent event)
public class MainActivity extends Activity
{
public boolean touched = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
TextView tv = (TextView) findViewById(R.id.textView6);
tv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
touched = true;
tv.setVisibility(View.INVISIBLE);
return true;
}
});
}
}
Instead of implementing OnGestureListener add a setOnTouchListener in your root view of your activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlTestView"/>
For example rlTestView is your activity's root layout id, then use below code in your oncreate method
((RelativeLayout)findViewById(R.id.rlTestView)).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
tv.setVisibility(View.GONE);
return true;
}
});`
Use the code below on the onCreate method and yes set the visibility as GONE instead of invisible. Also state the current visibilty of the TextView in the onTouch then set it to
tv.setVisibility(View.GONE);
Related
I've created a method that is called when my EditText is clicked (or touched, to be specific), which simply takes the current time and displays it exactly as it has been passed by System.currentTimeMillis() in a TextView below. The method is this one:
public void captureTime(View view) {
currentTime = System.currentTimeMillis();
String currentTimeStr = currentTime+ "";
TextView textView = findViewById(R.id.textView);
textView.setText(currentTimeStr);
}
To do that, I've had to add the code below in the onCreate method. It uses setOnTouchListener instead of setOnClickListener since otherwise the captureTime method wasn't always called. Now, this method does what I wanted, but now when I click or touch the EditText, although it does this as required, the keyboard doesn't show up anymore. I have looked into other questions in which the keyboard doesn't show up either but they're not related to my case, although I have tried them. So, what is that is making the keyboard not show up and how can I fix this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_time = (EditText)findViewById(R.id.editText);
add_time.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
captureTime(add_time);
return true;
}
return false;
}
});
}
Try removing return true (in the if block). In this case, the "captureTime" method will work and the keyboard will open.
I have a linear layout in a fragment with a bunch of checkboxes and various edittext widgets inside it. Basically like a quiz. A bunch of multiple choice(checkboxes) and a dozen short answer(edittexts) questions.
What I would like is for users to be able to click an edittext, type in an answer, then press DONE or click anywhere else on the layout and have the widget lose focus and the keyboard hide. Currently I am overriding the setOnEditorActionListener and setOnFocusChangeListener methods of each edittext to give focus back to a main layout, and hide the keyboard respectively. Here is the code for an edittext instance called "input_7d":
final EditText input_7d = (EditText) thisview.findViewById(R.id.txtinput_7d);
final LinearLayout parentLayout = (LinearLayout) thisview.findViewById(R.id.main_layout);
input_7d.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
parentLayout.requestFocus();
}
return false;
}
});
input_7d.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(parentLayout.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
This is annoying to do for every edittext I add, and it means editing lots of code if I remove them or add more in the future. What I would like to do is have a custom edittext class that can return focus to it's parent view/layout and hide the keyboard, then use that instead of the built in edittext. I'm very new to this and I haven't been able to find a way for a custom edittext to pass focus back to it's parent layout. Is there a better way to get a bunch of edittexts to all have this behavior and not have it all "hardcoded" into my fragment class?
So I could not find a way to have a edittext pass focus back to it's parent layout from inside the view itself. So instead I have opted to just disable focusable property of it when it
1) It loses focus (user clicked outside the view on something focusable, ie. The parent layout)
2) Finishes it's edit(user presses Done action on soft keyboard)
Surprisingly neither of these actions by default remove focus and the cursor from a default editText. At least not inside my scroll views.
So I added these lines to a custom view(myEditText) that extends the editText view:
this.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
}
return false;
}
});
this.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
myEditText.setFocusable(false);
myEditText.setFocusableInTouchMode(false);
hideKeyboardFrom(context, v);
}
}
});
this.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
myEditText.setFocusableInTouchMode(true);
myEditText.setFocusable(true);
return false;
}
});
I find it really annoying that to get simple functionality like not having the cursor blinking at me always or having the view not take focus when changing fragments and such you have to do such a weird workaround. Making a view unfocusable unless it's focused in which case it is focusable but only until it isn't focused again just seems dumb. Still wondering if there is a better way to do this for a large number of edits in one layout.
you can set a touch listener for the root layout and then remove the focus whenever not needed for the view
findViewById(R.id.rootView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch() //check for y
return false;
}
});
Hey guys i'm new in Android's dev and for a project, I have to implement a listener for a button.
But unfortunately, he can't detect the button I think.
Here is the java code :
public class Touch extends AppCompatActivity implements OnTouchListener,OnClickListener {
private button boutonCompteur = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.Compteur);
boutonCompteur = (button) findViewById(R.id.Compteur);
boutonCompteur.setOnTouchListener(this);
boutonCompteur.setOnClickListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
} }
and here is the XML :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="appuyez ici pour compter votre nombre de touch"
android:id="#+id/Compteur"
android:height="130dp"
android:textColor="#1818e3"
android:textColorHighlight="#cd5555"
android:textSize="22dp"
android:textStyle="italic"
android:layout_marginTop="150dp"
android:layout_alignParentStart="true" />
Thanks for your answers :)
I am going to use some visual help to answer your question and identify why is not working...
take a look at the image below and note:
You need a layout for your activity and this is not the same as the button
You need to find the button in the layout, that is odne by the id you used in the xml file, and is not the same as the Layout!!
you need to set the rigth listener to get the onclick, android has for that the OnClickListener interface
the word button between parenthesis is a casting, (android will try to convert something to a class, therefore the class is the class button, and not the name of your variable)
all the code that you write inside the onClick is what will be executed once the button is pressed.
Conclusion
Take the image as a reference and implement it in your code.
Remove onTouch , use onClick only.
Remove this line
boutonCompteur.setOnTouchListener(this);
and change public boolean onTouch(View v, MotionEvent event) to
public void onClick(View v) {
// Do whatever you want
}
And also change private button boutonCompteur = null; to private Button boutonCompteur; . (I don't think you need to add =null when doing button declaration).
When I click an Android button the Android's Software Keys are shown (hidden before) instead of firing the onClick() method for the actual button that I click.
I have two methods for showing or hiding the system UI:
1.
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
and 2:
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
And I also have another method which a switch to get the onClick() event on buttons
public void onClick(View v) { switch() { } }
Is it possible to fix this behaviour and when I click the actual button will be fired and the System UI will be shown?
There is no workaround to overcome this behaviour until you have both touch listener and also a click listener inside the same view. Try using
View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
instead. Add the below code before setcontentview() method.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView