i have this code
package com.tct.soundTouch;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class main extends Activity implements OnTouchListener {
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button zero = (Button) this.findViewById(R.id.button);
zero.setOnTouchListener(this);
mp = MediaPlayer.create(this, R.raw.sound);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
case MotionEvent.ACTION_UP:
mp.pause();
}
return true;
}
}
and it works but not as i expected. The sound plays but only for each time that i press the button. My idea is. While i press the button the sound plays, when i stop the action (finger out of the button) music pause.
Any idea please?
thanks
This should work (there was something wrong with your switch-cases I think):
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
break;
case MotionEvent.ACTION_UP:
{
mediaPlayer.pause();
}
break;
}
return true;
}
public class MainActivity extends AppCompatActivity {
Button button;
MediaPlayer player;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
button = findViewById( R.id.Click );
button.setOnTouchListener( new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()== MotionEvent.ACTION_DOWN) {
player= MediaPlayer.create( MainActivity.this,R.raw.horn );
player.start();
}
else if(event.getAction()==MotionEvent.ACTION_UP){
player.stop();
player.release();
}
return true;
}
} );
}
}
Related
So I'm working on a project and am using a lot of code or atleast a decent amount all in one file and am wondering if there is a way to clean it up into multiple files. I've only every worked with single file. So when It come to adding more files I'm not really sure how to go about this so I'm gonna ask for help on cleaning up the project. the ideal outcome is to have it more understandable via small chunks in other files to clean up the CoreActivity. or if you see a way to clean up some code into a easier and less exhaustive way of writing it then do fix
CoreActivity.java
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import com.github.pwittchen.gesture.library.Gesture;
import com.github.pwittchen.gesture.library.GestureListener;
import com.github.pwittchen.swipe.library.rx2.Swipe;
import com.github.pwittchen.swipe.library.rx2.SwipeListener;
public class CoreActivity extends AppCompatActivity {
//Widgets
ConstraintLayout m_Preference_Toolbar;
ConstraintLayout m_Toolbar;
ConstraintLayout m_Preferences;
EditText m_WebView_Search;
WebView m_WebView;
private Swipe WebSwipe;
private Gesture WebGesture;
//Variables
String Url = "https://www.Google.ca";
int WebS =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_core);
//Find Views
m_Toolbar = findViewById(R.id.m_Toolbar);
m_Preferences = findViewById(R.id.m_Preferences);
m_WebView_Search = findViewById(R.id.m_WebView_Search);
m_WebView = findViewById(R.id.m_WebView);
m_Preference_Toolbar = findViewById(R.id.m_Preferences_Toolbar);
m_Preference_Toolbar.setVisibility(View.GONE);
m_Preferences.setOnTouchListener(new View.OnTouchListener() {
Animation ToolbarGone,ToolbarPrefVisible;
int x = 0;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
m_Preferences.setVisibility(View.GONE);
x =1;
ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
if(m_Preferences.getVisibility() == View.GONE || x == 1) {
ToolbarPrefVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_preference_toolbar_appear);
m_Preference_Toolbar.startAnimation(ToolbarPrefVisible);
}
return false;
}
});
//m_WebView
final WebSettings m_WebViewSetting = m_WebView.getSettings();
m_WebView.setWebViewClient(new WebViewClient());
m_WebViewSetting.setJavaScriptEnabled(true);
m_WebViewSetting.setSupportZoom(true);
m_WebViewSetting.setLoadWithOverviewMode(true);
m_WebViewSetting.setUseWideViewPort(true);
m_WebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
m_WebView.loadUrl(Url); //Default HomePage
//m_WebView_Search TextView and Keyboard
m_WebView_Search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
if (m_WebView_Search.getText().toString().contains(".com")
|| m_WebView_Search.getText().toString().contains(".ca")
|| m_WebView_Search.getText().toString().contains(".net")
|| m_WebView_Search.getText().toString().contains(".org")) {
Url = m_WebView_Search.getText().toString();
m_WebView.loadUrl("https://www." + Url);
m_WebView_Search.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
} else {
if (m_WebView_Search.getText().toString().contains("")) {
Url = m_WebView_Search.getText().toString();
m_WebView.loadUrl("https://www.google.ca/search?q=" + Url);
m_WebView_Search.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
}
}
}
return false;
}
});
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
int Web = 0;
Animation WebShrink, WebEnlarge;
#Override
public void onSwipingLeft(final MotionEvent event) {
if(m_WebView.canGoForward()){
m_WebView.goForward();
}
else{
m_WebView.reload();
}
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
if(m_WebView.canGoBack()){
m_WebView.goBack();
}
else{
m_WebView.reload();
}
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
//WebView Animation (Enlarge)
if(Web == 0){
WebEnlarge = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_webview_enlarge);
m_WebView.startAnimation(WebEnlarge);
Web = 1;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.GONE);
}
}, 350);
}
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
//WebView Animation (Shrink)
if(Web == 1){
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
m_WebView.startAnimation(WebShrink);
m_Preference_Toolbar.setVisibility(View.GONE);
Web = 0;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.VISIBLE);
}
}, 350);
}
}
});
WebGesture = new Gesture();
WebGesture.addListener(new GestureListener() {
#Override public void onPress(MotionEvent motionEvent) {
}
#Override public void onTap(MotionEvent motionEvent) {
}
#Override public void onDrag(MotionEvent motionEvent) {
}
#Override public void onMove(MotionEvent motionEvent) {
}
#Override public void onRelease(MotionEvent motionEvent) {
}
#Override public void onLongPress(MotionEvent motionEvent) {
}
#Override public void onMultiTap(MotionEvent motionEvent, int clicks) {
}
});
}
#Override
public boolean dispatchTouchEvent (MotionEvent event){
WebSwipe.dispatchTouchEvent(event);
WebGesture.dispatchTouchEvent(event);
return super.dispatchTouchEvent(event);
}
}
The simplest way to split code is to think about Single Responsibility (as per the SOLID principles).
As an example in your code, your touch listener can be separated. This would entail making a class that extended the touch listener's class (View.OnTouchListener) instead of extending an activity.
This block in your code:
m_Preferences.setOnTouchListener(new View.OnTouchListener() {
...
});
Would become MyTouchListener.java:
public class MyTouchListener implements View.OnTouchListener {
Animation ToolbarGone,ToolbarPrefVisible;
int x = 0;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
... // keep exactly your same code here
}
}
And then your activity gets changed to:
m_Preferences.setOnTouchListener(new MyTouchListener());
I wrote a simple code in MainActivity with its xml code previously which run successfully. Now, I want to make a fragment and run that MainActivity code in that fragment. I have tried everything, even by combining both codes etc. but all in vain. I'm attaching my MainActivity code below. This is the one I want to use in Fragment. I already know that fragment is like sub-activity of an activity and both have separate xmls too which I also know how to use. Just unable to use my MainActivity code from old app as FragmentActivity in a new app.
1) MainActivity Code which needs to act as a Fragment
package com.ranatalha.userauthority;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity
implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private TextView mytext;
private GestureDetector gestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //this is above two lines cox phly content set hoga tbi agy kam hna na
this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this); }
//******************Upon clicking the button*************************
public void changetextshort(View v) {
mytext = (TextView) findViewById(R.id.mytext);
mytext.setText("Surpriseeee"); }
//******************initializing touch event*************************
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);}
//******************Implements Methods from alt+ins(0) for touch properties*************************
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
mytext.setText("I tapped Once");
return false;}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
mytext.setText("I tapped Twice");
return false;}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
mytext.setText("Double Tap Event Occurred");
return false;}
#Override
public boolean onDown(MotionEvent motionEvent) {
mytext.setText("Down goes");
return false;}
#Override
public void onShowPress(MotionEvent motionEvent) {
mytext.setText("I have pressed");}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
mytext.setText("Single Tap Up");
return false;}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
mytext.setText("I'm Scrolling");
return false;}
#Override
public void onLongPress(MotionEvent motionEvent) {
mytext.setText("I long pressed");}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
mytext.setText("I FLINNGED");
return false; }
}
2) Fragment Code (MainActivity is not having any code now except the basic code of Mainactivity like class name and OnCreate code - although I have added Mainactivity code in 3rd point too which needs to be actually almost like that when we make fragments in android app)
package com.ranatalha.userauthority;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TopSectionFragment extends Fragment {
public class Top
extends AppCompatActivity
implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private TextView mytext;
private GestureDetector gestureDetector;
//******************Override method oncreateview for fragment*************************
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment,container, false);
this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
return view; //designing of top section fragment completed
}
//******************Upon clicking the button*************************
public void changetextshort(View v) {
mytext = (TextView) findViewById(R.id.mytext);
mytext.setText("Surpriseeee"); }
//******************initializing touch event*************************
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);}
//******************Implements Methods from alt+ins(0) for touch properties*************************
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
mytext.setText("I tapped Once");
return false;}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
mytext.setText("I tapped Twice");
return false;}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
mytext.setText("Double Tap Event Occurred");
return false;}
#Override
public boolean onDown(MotionEvent motionEvent) {
mytext.setText("Down goes");
return false;}
#Override
public void onShowPress(MotionEvent motionEvent) {
mytext.setText("I have pressed");}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
mytext.setText("Single Tap Up");
return false;}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
mytext.setText("I'm Scrolling");
return false;}
#Override
public void onLongPress(MotionEvent motionEvent) {
mytext.setText("I long pressed");}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
mytext.setText("I FLINNGED");
return false; }
}
}
3) MainActivity code (which will incorporate Fragment sub-activity)
package com.ranatalha.userauthority;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity
extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
}
One of the main things to keep in mind when converting code from activities and fragments is that you may have to switch things that use context in them.
This line: this.gestureDetector = new GestureDetector(this, this); is likely one of the things that is giving you trouble.
This:
GestureDetector(Context context, GestureDetector.OnGestureListener listener)
is probably the constructor you are trying to use.
The parameters that are context, will need to be changed from this in an activity to getContext() (or to something that extends Context) in a fragment.
For creating the fragment in the first place:
https://developer.android.com/guide/components/fragments.html
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
The container mentioned in the code sample is normally something like a FrameLayout.
It is not clear what you mean by "make a fragment and run that MainActivity code in that fragment"...
If you want to start an activity and make it overlay the Fragment you can try the code from MCeley in Start an activity from a fragment
> Intent intent = new Intent(getActivity(), mFragmentFavorite.class);
> startActivity(intent);
I am trying to create my first android game (in eclipse) and I cannot seem to get OnTouchListner to work, mostly because I don't know how or where to create it. I am trying to figure out where someone taps the screen. Can someone please tell me how and where to create the OnTouchListner!
Activity class:
package com.gregsapps.fallingbird;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class Game extends Activity implements OnTouchListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.){
System.out.println("TOUCH");
}
return false;
}
}
View class:
package com.gregsapps.fallingbird;
import android.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View{
private Bird bird;
private boolean runOnce = false;
private Context context;
public GameView(Context context) {
super(context);
this.context = context;
this.setDrawingCacheEnabled(true);
// TODO add setup code
}
protected void onDraw(Canvas canvas){
update(canvas);
//TODO add drawing code
this.buildDrawingCache();
//bird.canvasImage = this.getDrawingCache(true);
canvas.drawBitmap(bird.image, bird.x, bird.y, null);
System.out.println("drawing");
invalidate();
}
private void update(Canvas canvas){
//TODO add code to update stuff
if(runOnce == false){
bird = new Bird(canvas, com.gregsapps.fallingbird.R.drawable.bird, context);
runOnce = true;
}
bird.move();
}
}
Implement it like this :-
public class Game extends Activity implements OnTouchListener{
GameView gv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gv = new GameView(this);
setContentView(gv);
gv.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
int x = event.getX();
int y = event.getY();
System.out.println("Touched view at X: " + X + " Y: " + Y );
}
return false;
}
}
You should just be able to set it on the view you create:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameView gv = new GameView(this);
setContentView(gv);
// set the touch listener for the view
gv.setOnTouchListener(this);
}
What I mean is, that I want to be able to press two separate buttons at the same time and have them do things on ACTION_DOWN and UP.
I have multiple onTouchListeners, but the app only lets me press one at a time. I need to be able to press at least two at the same time. I assume it has to do with the program getting stuck in a loop when the first button is being held down?
I read something about ACTION_POINTER1_DOWN and ACTION_POINTER2_DOWN, but I have to revise something to make them work right? Or am I even on the right track here?
package the1n07.bt_rc;
import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
public class ControlActivity extends ActionBarActivity {
Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
EditText speedText;
public String speed;
public Integer speedInt;
public boolean forward = false, backward = false, left = false, right = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
Main();
}
public void Main(){
speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
speedText = (EditText) findViewById(R.id.speedText);
forwardButton = (Button) findViewById(R.id.forwardButton);
backwardButton = (Button) findViewById(R.id.backwardButton);
leftButton = (Button) findViewById(R.id.leftButton);
rightButton = (Button) findViewById(R.id.rightButton);
speedPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 5) {
speedInt = speedInt + 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
speedMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 1) {
speedInt = speedInt - 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
forwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("forward", "is held down");
forward = true;
forwardButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
Log.d("forward", "is lifted");
forward = false;
forwardButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
forwardButton.setBackgroundColor(0xff222222);
forward = false;
break;
}
return true;
}
});
backwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
backward = true;
backwardButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
backward = false;
backwardButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
backwardButton.setBackgroundColor(0xffffffff);
backward = false;
break;
}
return true;
}
});
leftButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("left", "is held down");
left = true;
leftButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
Log.d("left", "is lifted");
left = false;
leftButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
leftButton.setBackgroundColor(0xff222222);
left = false;
break;
}
return true;
}
});
rightButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
right = true;
rightButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
right = false;
rightButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
rightButton.setBackgroundColor(0xffffffff);
right = false;
break; //Does last case need break?
}
return true;
}
});
}
}
This question may have already been answered by these two links : 1).Android - How to handle two finger touch.
2).Detect multitouch with Ontouch listener Android
The following links was also posted there :
1).http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
2).http://developer.android.com/training/gestures/multi.html
I've seen lots of people asking about this and usually the answer has been something along the lines of: "Do it before
setContentView(R.layout.test_layout);
or
Do it before super.onCreate(savedInstanceState);
I've tried them both, but putting it before
setContentView(R.layout.test_layout);
gives the exception, where as putting it before:
super.onCreate(savedInstanceState);
doesn't give an exception, but it doesn't work either...
I got the fullscreen working by putting:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);`
before
super.onCreate(savedInstanceState);
I've seen plenty of other ways to force noTitleScreen and fullscreen, but none of them seemed to work. (No errors on most of the other ways, they just didn't seem to do anything) Like changing the layout theme... Did nothing for some reason.
I'm using Android Studio.
Any help would be appreciated.
This is the whole activity. I feel the need to defend myself by saying this is my first time developing with java, so excuse me if I have any obvious mistakes here :3 ->
package the1n07.bt_rc;
import android.content.Intent;
import android.gesture.Gesture;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
public class ControlActivity extends ActionBarActivity {
Button speedPlusButton, speedMinusButton, forwardButton, backwardButton, leftButton, rightButton;
EditText speedText;
public String speed;
public Integer speedInt;
public boolean forward = false, backward = false, left = false, right = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
//have tried putting the line here
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//and here
super.onCreate(savedInstanceState);
//and here
setContentView(R.layout.test_layout);
//and even here just in case, though I'm pretty sure it's not supposed to go here
Main();
}
public void Main(){
speedPlusButton = (Button) findViewById(R.id.speedPlusButton);
speedMinusButton = (Button) findViewById(R.id.speedMinusButton);
speedText = (EditText) findViewById(R.id.speedText);
forwardButton = (Button) findViewById(R.id.forwardButton);
backwardButton = (Button) findViewById(R.id.backwardButton);
leftButton = (Button) findViewById(R.id.leftButton);
rightButton = (Button) findViewById(R.id.rightButton);
speedPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 5) {
speedInt = speedInt + 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
speedMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speed = speedText.getText().toString();
speedInt = Integer.parseInt(speed);
if(speedInt != 1) {
speedInt = speedInt - 1;
}
speed = Integer.toString(speedInt);
speedText.setText(speed);
}
});
forwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
forward = true;
forwardButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
forward = false;
forwardButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
forwardButton.setBackgroundColor(0xff222222);
forward = false;
}
return false;
}
});
backwardButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
backward = true;
backwardButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
backward = false;
backwardButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
backwardButton.setBackgroundColor(0xffffffff);
backward = false;
}
return false;
}
});
leftButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
left = true;
leftButton.setBackgroundColor(0xFF7D7D7D);
break;
case MotionEvent.ACTION_UP:
left = false;
leftButton.setBackgroundColor(0xff222222);
break;
case MotionEvent.ACTION_CANCEL:
leftButton.setBackgroundColor(0xff222222);
left = false;
}
return false;
}
});
rightButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
right = true;
rightButton.setBackgroundColor(0xFFB9B9B9);
break;
case MotionEvent.ACTION_UP:
right = false;
rightButton.setBackgroundColor(0xffffffff);
break;
case MotionEvent.ACTION_CANCEL:
rightButton.setBackgroundColor(0xffffffff);
right = false;
}
return false;
}
});
}
}
Do like this:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}