How to have an Activity that uses a ScrollView AND keep onTouch method working properly? - android-activity

I have an Activity that populates two views on a ViewFlipper. I added an onTouch(View v, MotionEvent event) public boolean method to the Activity. The method is implemented so that when the person taps on the screen the ViewFlipper goes to the next view. It works great but some of the text is too long so I went into my XML file and surrounded the textfield in one of my ViewFlipper views with a linearlayout and then a ScrollView. But now when I'm viewing the fields that are too long and show a scroll bar, I can't display the previous view. The onTouch method in my main activity isn't being executed. I have not been able to figure this out. I've read some posts about implementing or overriding the methods in ScrollView but I don't know where to do this in my activity. Does anyone know how I can program ScrollView to not intercept but keep its ability to scroll the view?

ScrollView myScroll;
myScroll.setOnTouchListener(new OnTouchListener){
#Override
public boolean onTouch(View v, MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (oldTouchValue < currentX) {
//left swipe
return true;
}
if (oldTouchValue > currentX) {
//right swipe
return true;
}
return false;
}
}

Related

How to make activity start at the bottom of the ScrollView?

I want to make a quiz application with levels. It has a long background image and as you scroll down, at the bottom I have the levels 1, 2, 3,...going up. How can I make the activity scroll at the bottom automatically when I enter the app?!
ScrollView scrollview = findViewById(R.id.scrollView);
scrollview.post(new Runnable() {
#Override
public void run() {
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
}
});

Listview has Arrayadpater with edittext

I have single view layout with edit text and image. To display data in listview i am using arrayadapter . While entering value in Edit text in first row. The data gets repeated to another row in listview . How can i resolve this ?
View rowView = null;
try {
if (convertView == null) {
//inflating view.
rowView =
inflater.inflate(R.layout.list_single_view,null,false);
}
else
{
//Setting view if layout is already implemented.
rowView = convertView;
}
imageView = (ImageView)
rowView.findViewById(R.id.iv_activity_image_single);
//Facing issue here in editText . The entered text gets repeated
et_image_name = (EditText)
rowView.findViewById(R.id.txt_user_notes);
et_image_description = (EditText)
rowView.findViewById(R.id.txt_user_description);
//Loading image using universal image loader to imageView
if (equipmentPicturesList.get(position) != null) {
Constant.imageLoader.displayImage("file:///" +
equipmentPicturesList.get(position).toString(), imageView, Constant.options,
new SimpleImageLoadingListener());
}
else {
imageView.setBackgroundResource(R.drawable.ic_launcher);
}
}
catch (Exception e) {
Logger.loadStackTrace(e);
}
return rowView;
The EditText messes with the focus of whatever layout it is in. That can be a little tricky by itself, but in a ListView it causes all sorts of problems. But there is a way around that, it will work if you put the EditText inside the footer or header of the ListView with addHeaderView or addFooterView. The reason that it works in the header or footer is that the header and footer views are not recycled by the ListView. Alternatively you can call setHasTransientState() on the EditText to prevent it from being recycled even as a normal view inside the ListView.
But beware that turning off the view recycling - especially if you do it for multiple views - can impact performance and scrolling speed of the ListView.

Have webview selection and swipe at the same time

I have a webview containing some text. I need to be able to swipe on the webview and make text selection at the same time. The problem is after I have implemented the swipe overriding the onTouchEvent method, the other features of the webview seem to have freezed, links in the webview aren't clickable anymore, and the text selection (after longClick) doesn't work anymore, I need to make to implement the swipe and preserve the other features of the webview.
I tried to override the longClick click method as well and use the following method to implement text selection:
public void selectAndCopyText() {
try {
Method m = WebView.class.getMethod("emulateShiftHeld", null);
m.invoke(MyWebView.this, null);
}
catch (Exception e1) {
e1.printStackTrace();
KeyEvent shiftPressEvent = new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(this);
}
}
but it didn't work for when this method is called I only get the upper bar for copy/ paste/ search/ websearch... but no actual selection on the webview occur.

Inconsistent text field.TouchDown firing when using a ScrollView

I'm using a UIScrollView for the first time, and I'm going crazy. I have several text boxes and labels on the scrollview. My text box event handled for TouchedDown, HandleTbLocationTouchDown (tb = Textbox), isn't firing half the time. My guess would be that the scrollview is swallowing the event because I have the exact same functionality on another View and it works perfectly fine.
Also, I set the size of the ScrollView in IB but that doesn't let it be scrollable. Setting it programmatically in my ViewDidLoad works though.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Set the scrollableview
scrollView.Frame = new RectangleF(0,20, 320, 460);
// Set the initial scrollable view area size
scrollView.ContentSize = new SizeF(320, 550);
// For some reason this isn't always firing to do the offset,
// but the keyboard ALWAYS comes up.
tbLocation.TouchDown += HandleTbLocationTouchDown;
}
void HandleTbLocationTouchDown (object sender, EventArgs e)
{
scrollView.SetContentOffset(new PointF(0, 130), true);
}
Help?

How to detect keyboard events on hardware keyboard on iPhone (iOS)

I have an app with a login page that has three fields for three different random characters. When the user leaves the last field, the soft keyboard disappears and the user can touch a "login" button on screen.
When there is a hardware keyboard (bluetooth or physical) attached, I'd like to be able to hit "enter" on it. However because the user is not in a field, I can't see how to detect this key being pressed.
Would anyone have advice on which class handles key press events? Presumably there is a delegate that I can use to receive these but my searches through the SDK haven't found anything.
Thanks
For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:
Objective-C
// In a view or view controller subclass:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (NSArray *)keyCommands
{
return #[ [UIKeyCommand keyCommandWithInput:#"\r" modifierFlags:0 action:#selector(enterPressed)] ];
}
- (void)enterPressed
{
NSLog(#"Enter pressed");
}
Swift
// In a UIView/UIViewController subclass:
override var canBecomeFirstResponder: Bool {
true
}
override var keyCommands: [UIKeyCommand]? {
return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ]
}
#objc func enterPressed() {
print("Enter pressed")
}
One way to accomplish this is to have a hidden extra (4th in your case) text field. Make it 1x1 px in size and transparent. Then make it the first responder when any of your other 3 text fields are not, and look for text change events in that hidden field to trigger your key input event.
You might also want to check the notification for a software keyboard appearing if you don't want it to stay visible as well.
As a followup to the response by #Patrick, here is how you do it in Xamarin.iOS:
public override bool CanBecomeFirstResponder
{
get { return true; }
}
public override UIKeyCommand[] KeyCommands
{
get
{
return new[]{ UIKeyCommand.Create((NSString)"\r", (UIKeyModifierFlags)0, new ObjCRuntime.Selector("enterPressed")) };
}
}
[Export("enterPressed")]
private void OnEnterPressed()
{
// Handle Enter Key
}