after clicking the button mouse scroll will be activated and scroll up, button value should be increment and when scroll down value decrement. but button outside code working,then button inside code not work.my sample code here.
public class MainActivity extends Activity {
Button button;
int x,f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
// i need implement code here ,how can i do?
return false;
}
});
}
#SuppressLint("NewApi") #Override
public boolean onGenericMotionEvent(MotionEvent event) {
//if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
//selectNext();
{
x=Integer.parseInt(button.getText().toString());
f=x+5;
button.setText(""+f);
}
else
{
x=Integer.parseInt(button.getText().toString());
f=x-5;
button.setText(""+f);
return true;
}
}
return super.onGenericMotionEvent(event);
} }
i need those function inside the touch listener i don't know how can i do,please help me!
Related
I want to write some code that performs the action to go back to the previous activity. Of course I can write a code for each back button that I have, but I would prefer to have only a piece of code working for all the back buttons (and then, for example, to assign the function to the "onclick" event). How can I manage that?
Please use onclick method and that method redirect to previous activity.
#Override
public void onBackPressed()
{
// your code.
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN)
{
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Use inheritance
YourActivity.java
public class YouActivity extends BaseActivity {
Button back_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
back_button = (Button) findViewById(R.id.back_button);
back_button.setOnClickListener(base_listener);
}
}
BaseActivity.java
public class BaseActivity extends Activity {
View.OnClickListener base_listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//you back action here;
}
};
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
I need to use Androids Switch button control but I need to be able to detect changes when the user is dragging the control not on clicking. So if the user drags from off to on and holds it in the on position I need to detect that. I thought I could just set the onTouchListener and then look at when the switches text changes state (on/off off/on) and then trigger my changes then but I can't. The
mySwitch.getText()
Does not work for me. It return blank text even though I have the textOn and textOff attributes set. I need to be able to detect the state change when the user is dragging, not on click. The onCheckedChanged listener will not work for me since that triggers only when the user toggles or slides and lifts there finger off of the button. Any help would be appreciated. Thank you.
Basic Code:
public class MainActivity extends Activity {
private String TAG = MainActivity.class.getSimpleName();
private TextView switchStatus;
private Switch mySwitch;
private TextView mySwitchText;
private String oldState;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchStatus = (TextView) findViewById(R.id.switchStatus);
mySwitch = (Switch) findViewById(R.id.mySwitch);
//set the switch to ON
mySwitch.setChecked(false);
oldState = mySwitch.getText().toString();
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
//switchStatus.setText("Switch is currently ON");
}else{
// switchStatus.setText("Switch is currently OFF");
}
}
});
mySwitch.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_MOVE) {
if(oldState.equals("OFF") && mySwitch.getText().equals("ON")) {
switchStatus.setText("Off to On");
oldState = "ON";
}
if(oldState.equals("ON") && mySwitch.getText().equals("OFF")) {
switchStatus.setText("On to Off");
oldState = "OFF";
}
}
Log.v(TAG, "Old State: " + oldState + " my Switch: " + mySwitch.getText().toString());
return false;
}
});
mySwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mySwitch.isChecked()) {
mySwitch.setChecked(false);
}
}
});
//check the current state before we display the screen
if(mySwitch.isChecked()){
switchStatus.setText("Switch is currently ON");
}
else {
switchStatus.setText("Switch is currently OFF");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Switch is a distant descendant of textview. Therefore you'd have to do this to make it work.
oldState = (TextView)mySwitch.getText().toString();
However, Switch has a textOn and textOff that works for this purpose.
I have a onClickListener on a textview and the textview has the flag that it's selectable.
But the onclick event I specified is only called when the textview is clicked a second time.
After the second time it calles the onclick right, but if a other textview that also is selectable with a onclicklistener it also is only called the second time, then it works fine, but then the other one works only the second time again. I can't find the source of these weird events.
telefoonTXT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {startTelIntent();}}
);
urlTXT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {startWebIntent();}
});
I faced this issue as well. Whenever text view is touched firstly onTouch, then OnSelection and at last OnClick is called.
If i understand your problem clearly you want to select text in text view when user double taps or long presses like the usual text selection but when user simply clicks it once u want the onClick to function. I think the following might help you.
Add a gestureDetector to your text View.
GestureDetectorCompat mDetector;
mDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener());
mDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// This is where u add your OnClick event
startTelIntent();
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("dtttt", "double tap");
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
});
telefoonTXT.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return false;
}
});
In my case focus change listener works, but be aware for cases when you use it in androidTv that you move with D-pad
textView.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus && textView.measuredHeight > textView.lineHeight * textView.lineCount) {
//your click event
}
}
I check if text can be scrolled then don't take the click event
The accepted answer is good. But I had one additional issue with it: When I have some selection text in textview, I want to tap on textview to only deselecting the text - without perfoming on click listener. And perform on click only with no text selection.
So below is my solution:
#SuppressLint("ClickableViewAccessibility")
public static void setOnClickGestureDetectorListener(TextView textView, Context context, View.OnClickListener onClickListener){
if (textView == null || context == null || onClickListener == null)){
return;
}
final GestureDetectorCompat detector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener());
detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onClickListener.onClick(textView);
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
});
textView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if ( v instanceof TextView ) {
TextView textView = (TextView)v;
if (textView.getSelectionStart() >= 0 && textView.getSelectionEnd() > textView.getSelectionStart()){
return false;
}
detector.onTouchEvent(event);
}
return false;
}
});
}
And calling example is below:
setOnClickGestureDetectorListener(textView, getContext(), new View.OnClickListener() {
#Override
public void onClick(View v) {
// This is where u add your OnClick event
}
});
set this ...
TextisSelectable = "false"
i think it will be work correctly
If I implement the OnClickListener, the sound effect will work on clicking the button. While on implementing the OnTouchListener the sound effect doesn't work on touching it. So how to enable the sound effect on implementing the OnTouchListener?
EDIT:
Here is the code if I use clicking approach:
public class CalculatorActivity extends Activity implements OnClickListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnClickListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public void onClick(View v)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
}
And here is the code if I use touching approach:
public class CalculatorActivity extends Activity implements OnTouchListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnTouchListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}
}
Try...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.playSoundEffect(SoundEffectConstants.CLICK);
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}
I have defined a standard onCreateOptionsMenu. The menu button works fine when my EditText box is empty. But when the EditText box has data in it, the menu button doesnt work. Any clue? Please help, i have no clue how to solve this. Thanks!
public class TipCalc extends Activity {
private EditText total;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
total = (EditText)findViewById(R.id.EditText01);
total.setOnKeyListener(mKeyListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private OnKeyListener mKeyListener = new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if((v.getId() == R.id.EditText01
&& (total.getText().toString().length() > 0) {
calculate();
return true;
}
}
return false;
}
}
I dont know if that would be correct but create setOnClickListener rather than OnKeyListener
something like this
TextView total = (TextView) findViewById(R.id.EditText01);
total.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//do some stuff
}
});