I am following This Tutorial of implementing splash screen in my android project and i want splash screen is closed whenever user tap two time on screen. If this can be done using touch events then how i used them ?
You'll need to take a look at 'onInterceptTouch' from there you can 'finish();' as usual
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29
You could also set the LinearLayouts android:clickable="true"
and then use an onclicklistner
final LinearLayout MYLAYOUT = (LinearLayout) findViewById(R.id.MYLAYOUT);
MYLAYOUT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
You might also have to add android:duplicateParentState="true" to all the views INSIDE your linear Layout
The easiest way will be to use a gesturedetector.
Add an id to your splash LinearLayout so that you can get a reference to it in code
Then in onCreate of MainActivity you'll need to do:
#Override
public void onCreate() {
super.onCreate();
...
LinearLayout splashBg = (LinearLayout)findViewById(R.id.splash_bg);
GestureListener mGestureListener = new GestureListener();
GestureDetector mGestureDetector = new GestureDetector(this, mGestureListener);
splashBg.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
}//end onCreate
You'll need to create this custom GestureListener to listen for the double tap and then close the splash and continue to the next activity
private class GestureListener implements GestureDetector.OnDoubleTapListener {
#Override
public boolean onDoubleTap(MotionEvent e) {
MainActivity.this.finish();
if (!mIsBackButtonPressed) {
// start the home activity
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
MainActivity.this.startActivity(intent);
}
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
}
use this code to set splashscreen
int touchcount=0;
LinearLayout layout;
layout=(LinearLayout) findViewById(R.id.main);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
touchevent++;
if(touchcount==2)
{
Intent intent=new Intent(youractivity,this,nextactivity.class);
startActivity(intent);
finish();
}
return false;
}
});
Related
I currently have a command that will allow the user to proceed to the next page if a button is clicked, this button has a feature android:onClick="advancenext" which I have defined in the .java file to be the following
public void advancenext (View view) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
However, I only want the user to call advancenext if a variable i is greater than 5. This is what I tried
if (i>5) {
public void advancenext (View view) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
However, when I run the app, I can still advance even though i is not greater than 5. Does anyone know how to fix this, or better restrict advancenext in a different way?
EDIT Thanks for the great answers, but I encountered a different error when I changed my statement. When I try:
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
I get the errors Syntax error on token "(", ; expected and Syntax error on token ")", ; expected around the ( and ) of (View view).
EDIT dos Here's my full code
public class Prompt1 extends Activity {
int i; //Variable i stores the touch number
float[] X, Y; //Array that will store the touchpoints
int ultscore1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
i=1;
touchLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
}
return true;
}
});
}
I would do this
public class Prompt1 extends Activity implements OnTouchListener{
int i; //Variable i stores the touch number
float[] X, Y; //Array that will store the touchpoints
int ultscore1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prompt1);
i=1;
touchLayout.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
advancenext(v);
return true;
}
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
}
This allows your class to directly handle the touch event (and any other touch events for views contained in the classes set content view) by just adding the on touch listener to the view. You can handle the incrementing of your variable i in onTouch like you were and call your advancenext method with the view passed to onTouch. The method, advancenext, could very easily have its functionality moved to onTouch, that is
#Override
public boolean onTouch(View v, MotionEvent event){
i++; //increasing i value
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
return true;
}
But moving the functionality out of onTouch into it's own method will allow you to use advancenext with other io events (like onClick).
You have to evaluate your if statement inside your function. Be sure you have access to i variable.
public void advancenext(View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
You've got your statement backwards:
public void advancenext (View view) {
if (i>5) {
Intent intent = new Intent(Prompt1.this, Prompt2.class);
Prompt1.this.startActivity(intent);
}
}
You should call your onClick() function no matter what, THEN check the condition. The way you have it now, you are ignoring the if statement completely.
I am trying to add both OnClickListener and OnTouchListener to my image view. Following is how the image view is created
dialogImage = (ImageView)findViewById(R.id.dialogImage);
Following is how the listeners are set
dialogImage.setOnClickListener(dialogBoxClicked);
dialogImage.setOnTouchListener(imageViewSwiped);
Following is the listener method implementation
public OnClickListener dialogBoxClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
//To do has been removed because the code is too big
}
};
OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
{
public void onSwipeRight()
{
currentlyActiveQuestion++;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
public void onSwipeLeft()
{
currentlyActiveQuestion--;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
};
Here the OnTouchListener is implemented by a class called OnSwipeTouchListener to monitor left and right swipes. This class can be found here - https://stackoverflow.com/a/12938787/1379286
But the problem now is, when I set the OnTouchListener to the image view, the OnClickListener is not responding / do not do what it should do. ImageView is only responding to OnTouchListener. If I remove OnTouchListener then the OnClickListener works. I tested this in a virtual device WVGA5.1 and Galaxy Nexus in eclipse and not in a real phone because I do not have one.
How can I solve this?
EDIT
Any code example will be greatly appreciated
You may call View.performClick() when action_up. This results in the click event being called when an actual click happens. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
The OnTouchListener hooks the click-event. Handle the click event in it instead. Check out the answer on this question
From my experience, If you can't have both onTouchListener and onClickListener for the same view. If you want your onClickListener to work, set clickable="true" in the XML.
I wrote an app with 1 activity and 1 view, so it gives me the coordinates of my fingers i put on the screen. Worked perfectly.
The Problem is now: I Created a new App with more than 1 Activity so i can change between them with intents. also worked fine. But one Activity should be the one wich give me my finger positions. So i Copyed the class and activity put them into the manifest. And made a Button and a intend for to run it.
So when i try to run it it creates the class but doesnt react on my onTouchEvents anymore...
and i have no clue why. i hope i explained my problem well enough for u guys to understand.
So this is my main activity. starts the menu with the option to go to the not working class
public class V1_2 extends Activity implements OnClickListener{
Button btn_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_v1_2);
btn_1 = (Button) findViewById(R.id.button1);
btn_1.setOnClickListener(this);
}
public void onClick(View v) {
if( btn_1.getId() == ((Button)v).getId() ){
startActivity(new Intent(this,Obj_recog.class));
}
}
This is now the activity wich creates the touchpiont class for the touchevents
public class Obj_recog extends Activity implements OnClickListener{
touchtest TP;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blank);
TP = new touchtest(this);
}
public void onClick(View v) {
}
And now an example of what doesnt work here but worked at the last project the same way
public class touchtest extends View{
public touchtest(Context context) {
super(context);
Log.d("worked", "worked");
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d("Touch", "TOUCH!!!!");
return true;
}
}
So i get the message that it "worked" but it doesnt react on touchevents like it used to do...
It will be worked if you add touchtest view as your main view by using setcontentview and then
add touchlistener on that view and call your ontouchevent from your touchtest class. code will be like --
TP = new touchtest(this);
setContentView(TP);
TP.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TP.onTouchEvent(event);
return false;
}
});
i think should change this approach find an alternative way to implement for which you are going in this way.
In this way you can keep your blank layout and also TP.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
TP = new touchtest(this);
rl.addView(TP);
TP.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TP.onTouchEvent(event);
return false;
}
});
i am creating a call/dial button, when i click on that call/dial button, a call will be made based on the input that is displayed in the edittext. I managed to do that part. can you guys advise me whether i can do a longer click on that same call/dial button, so that a toast can come out to ask user to choose something else??
I did some research on "setOnLongClickListener" but i am not sure if i can combine it in the same call/dial button? I have attached on the working dial function which i managed to do, wondering if the "setOnLongClickListener" can be combined together somehere in the code?
private void dialANumber() {
try {
buttonCall = (ImageButton) findViewById(R.id.imageButton2);
buttonCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("DialANumber", "Dialing the number failed", anfe);
}
this code is working. i hope a longer click can be made on the same call/dial button so the button can have a normal click to make a call, and longer click to pop out a toast. Thanks in advance.
Note that returning "false" on the long click listener will have the UI responding to the long click as a short click too. Return "true" if you want to kill that off. "True" means "yes, I used this event" and "false" means "whether I used it or not, the environment is free to respond as well." (I know this because I just used AkashG's answer in my own app.)
A GestureDetector with a SimpleOnGestureListener would help you differentiate between the different types of presses. A GestureDectector is a class which can read different types of touch events (for example, single taps and long presses), and sends them to a listener which handles each type differently. Here's the documentation on the Detector and Listener.
http://developer.android.com/reference/android/view/GestureDetector.html
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
First, set up your SimpleOnGestureListener, the important methods for you to override will be onSingleTapUp and onLongPress. In your onCreate, create an instance of GestureDetector that references your listener. Then, attach an OnTouchListener to your button that sends the event to your detector. You'll want it to look something like this:
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
#Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
#Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}
There a a bunch of other types of events this class can interpret in case you want to get even more fancy. GestureDetector is a very good class to do a little research on, it can be very useful and isn't too complex. Hope this helps.
Yes you can do this:
XML file:
<Button
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL"/>
<ImageButton
android:id="#+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
For button click event:
Button button=(Button) findViewById(R.id.call);
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
return false;
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
For imageButton:
ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(check==false){
Toast.makeText(getBaseContext(), "CLick", Toast.LENGTH_SHORT).show();
}
imageButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
check=true;
if(check){
Log.d("bool", check+"");
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
check=false;
}
return false;
}
});
Declare this at the top(golbally):
boolean check=false;
I have a Gridview displaying buttons that get their content from an array, I would like to change the background color of these buttons when clicking/pressed on it. I tried onClick and onTouch, it does go in to the method, but color is not background is not set. What is wrong with my code? Please help.
private class ImageAdapter extends BaseAdapter {
private Context mContext;
btnView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("onClick","go");
btnView.setBackgroundColor(Color.rgb(12,11,12));
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
Intent data = new Intent();
data.setData(Uri.parse("PictureStyle" + position));
setResult(RESULT_OK, data);
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("OnTouch","go2");
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
return false;
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("Touch listener",String.valueOf(position));
btnView.setBackgroundColor(0xff00ff00);
Log.d("Touch listener","set");
if ( event.equals(MotionEvent.ACTION_DOWN) ) {
btnView.setBackgroundColor(0xffff0000);
}
return false;
}
});
try it with v.setBackgroundColor(Color.rgb(12,11,12)); in your onClick
You better use GridView.setOnItemClickListener instead of register a clickListener on each Button