Cannot open Android Webbrowser using Action_View - android

I am trying to open the standard android webbrowser by clicking on a textview.
I defined the android:autoLink="web" in the textview and then use an onTouchListener to start a browserintent:
// On Touch Listener
chatText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent event) {
// view.performClick();
view.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN){
view.performClick();
openBrowser(chatText.getText().toString());
}
return false;
}
});
// Start Browser function
public void openBrowser(String url) {
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (webIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(webIntent);
}
}
However, everytime I click a link on my textview I get the error:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Although I do have the flags added to my newly started activity, anyone knows what I do wrong here?
I am calling the activity from a class ChatArrayAdapter which extends an ArrayAdapter, however, I pass the appropriate context to the newly started activity

Going over your code, your chat text has some URL in it. To make things easier, Android has Linkify class and the android:autoLink XML attribute. These help to automatically highlight links and perform the standard action when you click on them. All of this is handled out of the box.
Your chatText is probably a TextView. You can use one of the two ways I mentioned like so:
In XML:
In your chatText's XML add the following attribute:
android:autoLink="web"
In code:
Linkify.addLinks(chatText,Linkify.WEB_URLS);

Related

Android button has setOnTouchListener called on it but does not override performClick

When I try to add onTouchListner() to a button, it gets me the
Button has setOnTouchListener called on it but does not override
performClick
warning. Does anyone know how to fix it?
btnleftclick.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
Error:
Custom view has setOnTouchListener called on it but does not override
performClick If a View that overrides onTouchEvent or uses an
OnTouchListener does not also implement performClick and call it when
clicks are detected, the View may not handle accessibility actions
properly. Logic handling the click actions should ideally be placed in
View#performClick as some accessibility services invoke performClick
when a click action should occur.
This warning comes up because Android wants to remind you to think about the blind or visually impaired people who may be using your app. I suggest you watch this video for a quick overview about what that is like.
The standard UI views (like Button, TextView, etc.) are all set up to provide blind users with appropriate feedback through Accessibility services. When you try to handle touch events yourself, you are in danger of forgetting to provide that feedback. This is what the warning is for.
Option 1: Create a custom view
Handling touch events is normally something that is done in a custom view. Don't dismiss this option too quickly. It's not really that difficult. Here is a full example of a TextView that is overridden to handle touch events:
public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_UP:
performClick();
return true;
}
return false;
}
// Because we call this from onTouchEvent, this code will be executed for both
// normal touch events and for when the system calls this using Accessibility
#Override
public boolean performClick() {
super.performClick();
doSomething();
return true;
}
private void doSomething() {
Toast.makeText(getContext(), "did something", Toast.LENGTH_SHORT).show();
}
}
Then you would just use it like this:
<com.example.myapp.CustomTextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Click me to do something"/>
See my other answer for more details about making a custom view.
Option 2: Silencing the warning
Other times it might be better to just silence the warning. For example, I'm not sure what it is you want to do with a Button that you need touch events for. If you were to make a custom button and called performClick() in onTouchEvent like I did above for the custom TextView, then it would get called twice every time because Button already calls performClick().
Here are a couple reasons you might want to just silence the warning:
The work you are performing with your touch event is only visual. It doesn't affect the actual working of your app.
You are cold-hearted and don't care about making the world a better place for blind people.
You are too lazy to copy and paste the code I gave you in Option 1 above.
Add the following line to the beginning of the method to suppress the warning:
#SuppressLint("ClickableViewAccessibility")
For example:
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = findViewById(R.id.my_button);
myButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
}
Solution:
Create a class that extends Button or whatever view you are using and override performClick()
class TouchableButton extends Button {
#Override
public boolean performClick() {
// do what you want
return true;
}
}
Now use this TouchableButton in xml and/or code and the warning will be gone!
Have you tried adding :
view.performClick()
or adding suppresslint annotation :
#SuppressLint("ClickableViewAccessibility")
?
Custom view controls may require non-standard touch event behavior.
For example, a custom control may use the onTouchEvent(MotionEvent)
listener method to detect the ACTION_DOWN and ACTION_UP events and
trigger a special click event. In order to maintain compatibility with
accessibility services, the code that handles this custom click event
must do the following:
Generate an appropriate AccessibilityEvent for the interpreted click
action. Enable accessibility services to perform the custom click
action for users who are not able to use a touch screen. To handle
these requirements in an efficient way, your code should override the
performClick() method, which must call the super implementation of
this method and then execute whatever actions are required by the
click event. When the custom click action is detected, that code
should then call your performClick() method.
https://developer.android.com/guide/topics/ui/accessibility/custom-views#custom-touch-events
At the point in the overridden OnTouchListener, where you interprete the MotionEvent as a click, call view.performClick(); (this will call onClick()).
It is to give the user feedback, e.g. in the form of a click sound.
you can suppress a warning
#SuppressLint("ClickableViewAccessibility")
or call performClick()
[Example]

Drawable's movement with buttons

I've been learning android for almost a month, and I want to make a simple game with a custom class that extends from View, and it's included on main_activity.xml. In Main Activity.class I create an instance of the View, and control the movement of the sprite on the GameView with buttons, so each button has a method that control sprite's movement like this:
public void move_up(View v){
gameview.sprite.move(Dir.UP);}
The problem is that it only works when the button is released, and it's executed one time. I want the method to be executed while the botton is pressed but I can't figure out how to do this.
I'm assuming by the name of your method that you're adding the onClick property to the xml layout file in order to handle the event. While this may seem convenient at first it will not give you what you need.
Instead, you should implement the OnTouchListener which gives you information about what touch event is currently happening, in your case you'd want to handle the ACTION_DOWN action:
findViewById(R.id.btn_up).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}
return false;
}
});
Although you could make your Activity implement the listener and handle multiple buttons there. This is achieved simply by telling your activity to implement OnTouchListener, imeplementing the method onTouch as in the code above but instead as a method of your activity.
And the resulting set, would be simplified to findViewById(R.id.btn_up).setOnTouchListener(this); where R.id.btn_up is the id you've defined in the xml file.
This would make it start moving when they press, instead of when they release, if what you want is to make it move until they release (which would make sense), do something like:
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}else
if(event.getAction() == MotionEvent.ACTION_UP){
gameview.sprite.stop(); //Or whatever you call the stop method
return false;
}

onTouchEvent() will not be triggered if setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) is invoked

I call
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
when my app starts to make my app able to display the full screen.
I want my app's UI to pop up when screen is touched, but Activity.onTouchEvent() is not triggered until the screen is touched a second time. At first touch, only the Virtual Keys are shown.
So, I have to trigger my app's UI to pop up on
public void onSystemUiVisibilityChange(int visibility) {
if (visibility == View.SYSTEM_UI_FLAG_VISIBLE) {
// show my APP UI
}
}
but onSystemUiVisibilityChange with View.SYSTEM_UI_FLAG_VISIBLE will be invoked NOT once per touch (3 times on my Galaxy Nexus) by system, especially if the user touches the screen very fast/often.
project lib 4.0 or 4.03.
Samsung galaxy(9250) with 4.03.
Android 4.4 (API Level 19) introduces a new SYSTEM_UI_FLAG_IMMERSIVE flag for setSystemUiVisibility() that lets your app go truly "full screen." This flag, when combined with the SYSTEM_UI_FLAG_HIDE_NAVIGATION and SYSTEM_UI_FLAG_FULLSCREEN flags, hides the navigation and status bars and lets your app capture all touch events on the screen.
This did work for me:
setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
// show my app UI
}
}
});
What I've done is first imported android.view.GestureDetector so I can use it to detect gestures. Android has a number of default gestures that are automatically detected in the GestureDector class. Most of this info is found here, but below is code in a form that I've used in an actual project that works.
First I've made an anonymous class in my Activity (this can be nested wherever, but I tend to make my anonymous classes at the bottom, right before the closing bracket). NOTE: You can also implement OnGestureListener as part of your class, also.
The code below is for using gesture detection to give a simple hide/show.
I've declared and defined my action bar (my UI, which is initially hidden) as an instance variable, so I can access it here, and wherever else, but you can substitute it for a getActionBar().show() and getActionBar().hide() in the case you don't want to declare it as an instance variable. Substitute your UI in the place of the actionBar here:
public class Example extends ActionBarActivity {
// declared in onCreate() method
private android.support.v7.app.ActionBar actionBar;
private GestureDetectorCompat mDetector;
private YourView view1;
private YourView view2;
private YourView view3;
private YourView view4;
// some other code
class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final String DEBUG_TAG = "Gestures in Example Class";
#Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
// if there is a double tap, show the action bar
actionBar.show();
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
// if the tap is below the action bar, hide the action bar
if (event.getRawY() > getResources().getDimension(R.dimen.abc_action_bar_default_height)) {
actionBar.hide();
return true;
}
return false;
}
#Override
public boolean onDown(MotionEvent event) {
return true;
}
} // end-of-Example Class
Then in my onCreate() I've declared my GestureDetector and also (optionally) set my GestureListeners:
private GestureDetectorCompat mDetector;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code here
mDetector = new GestureDetectorCompat(this, new GestureListener());
// this code is for more advanced view logic not needed for a basic set-up
//setGestureListeners();
} // end-of-method onCreate()
Then in order to actually send gestures to be processed we provide the instructions for doing that, there are two ways I know about, first the simplest:
/**
* This method recognizes a touchEvent and passes it to your custom GestureListener
* class.
*/
#Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
The second way is more complex, but if you want to only recognize touch events on certain Views in your layout as in the case where you have overlapping views and can only access the top View, you can create a custom class to pass the event around or up:
class MyOnTouchListener implements View.OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
if (v.equals(view4)) {
return mDetector.onTouchEvent(event);
} else return false;
}
} // end-of-class MyOnTouchListener
and then use it here:
public void setGestureListeners() {
/* when we return false for any of these onTouch methods
* it means that the the touchEvent is passed onto the next View.
* The order in which touchEvents are sent to are in the order they
* are declared.
*/
view1.setOnTouchListener(new MyOnTouchListener());
view2.setOnTouchListener(new MyOnTouchListener());
view3.setOnTouchListener(new MyOnTouchListener());
view4.setOnTouchListener(new MyOnTouchListener());
} // end-of-method setGestureListeners()
In my setGestureListeners method, I gave them all the same set of commands, that essentially only recognizes touchEvents on view4. Otherwise, it just passes the touchEvent to the next view.
This is code using AppCompat, but if you are not building for older versions, you can use the regular GestureDetector and ActionBar.
Have you tried adding code to only show your UI when the state has changed? You have to maintain the last known visibility and only show your UI when you first come into being visible:
int mLastSystemUiVis;
#Override
public void onSystemUiVisibilityChange(int visibility) {
int diff = mLastSystemUiVis ^ visibility;
mLastSystemUiVis = visibility;
if ((diff&SYSTEM_UI_FLAG_VISIBLE) != 0
&& (visibility&SYSTEM_UI_FLAG_VISIBLE) == 0) {
// DISPLAY YOUR UI
}
}
Code sample adopted from the Android docs
The method Activity.onTouchEvent() gets called at the end of the responder chain (meaning after all other views have had a chance to consume the event). If you tap on a view that is interested in touch (i.e. a Button or EditText) there's a good chance your Activity will never see that event.
If you want to have access to touches before they every get dispatched to your view(s), override Activity.dispatchTouchEvent() instead, which is the method called at the beginning of the event chain:
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
//Check the event and do magic here, such as...
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
//Be careful not to override the return unless necessary
return super.dispatchTouchEvent(event);
}
Beware not to override the return value of this method unless you purposefully want to steal touches from the rest of the views, an unnecessary return true; in this spot will break other touch handling.
I got this problem too, and I found this http://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_HIDE_NAVIGATION
So, no way to help. Even the android system packaged Gallery app used SYSTEM_UI_FLAG_LOW_PROFILE instead of SYSTEM_UI_FLAG_HIDE_NAVIGATION in photo page view. This is at least what we can do.
I had a very similar issue with trying to update the UI from an onTouchEvent() requiring two touches to work, and I tried a bunch of elaborate stuff before finally getting it to work on the first click.
In my case, I was showing a previously hidden item, then getting its height, then moving a button down by that height. The problem I ran into is that the height was showing as 0 until after the first touch event finished. I was able to solve this by calling show() during ACTION_UP for the onTouchEvent() instead of its ACTION_DOWN. Maybe it'd work if you did something similar?
Try to use:
getWindow().getDecorView().setSystemUiVisibility(View.GONE);
instead:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
After that you can use normal activity in fullscreen and if you want nav keys you need to swipe from bottom to up. Working for me at Galaxy Tab 2 with android 4.1.2

OnItemClickListner is not working android

i am using listview in android with ratingbar as well as check box, here is my problem,if i am setting onitemclick listner to the listview, it is not working?
Please any one can help me..
Thanks in advance.
In your xml file.............
In checkbox..........
android:focusable="false"
Yes, I see the exact same thing. If you remove the checkbox and rating bar, then OnItemClick works, but with these widgets in your view, Android thinks the user wants to interact with them.
So instead you have to handle the user click in the view itself (rather than the listview).
OnTouchListener pressItemListener =new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
HomeActivity ha = (HomeActivity) getContext();
ha.handleLocationTouchEvent(position, arg1);
return false;
}
}
newView.setOnTouchListener(pressItemListener);
In the above example, HomeActivity is my parent activity. So I handle the user touch event in the custom view and then pass it off to the parent activity where you can do with it what you want. You might also want to handle onLongTouch. Hope this helps.

How to set animation when I changes content in same activity

I one activity I am seting different layouts depends on user choice.
For example I have setContentView(R.layout.main),
after that when user choose something I am setting new like setContentView(R.layout.first) when next time clicks I am setting setContentView(R.layout.second).
I need to change content in same activity. How to set animation ( something like when I really changing between activities ) when I changes content from main to first and from first to second ?
Let us take this with example.
Suppose we are changing view on press of button,
private OnTouchListener touch = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
loadOutViewAnimation();//
loadInViewAnimation();
}
}
}
public void loadOutViewAnimation(){
//considering layout is your root layout
layout.setAnimation(animation);
}
public void loadInViewAnimation(){
setContentView(R.layout.first);
//by using findview by id here you will get root layout.
layout.setAnimation(animation);
}
You can do this by using ViewFlipper.....
See this example http://www.androidpeople.com/android-viewflipper-example
I hope this will help you to solve your problem.
You can used methods from AnimationUtils class makeInAnimation(context, boolean) and makeOutAnimation(context, boolean) to create Animation object. Config it with setStartTime and setDuration methods. Now you can call setAnimation on your view and it will be appearing or/and disappearing with your animation.

Categories

Resources