Click on linkified TextView scrolls ScrollView - android

I have a ScrollView containing a TextView. I linkify parts of that TextView with
Pattern pattern = Pattern.compile("MyLink");
Linkify.addLinks(textView1, pattern, "mylink://");
I habe an Intent filter in my Manifest for mylink:// so a new Activity is opened when MyLink is clicked (as described in this question).
This works most of the time, sometimes though a click on the MyLink portion of the TextView doesn't open the Activity but only scrolls the ScrollView in which my TextView resides to the top.
Where does this behaviour come from and how can I fix it?

If you are attempting to open a link that leads to your current activity, it might be recreating the same activity and gives the sensation that it's scrolling up. Probably you want to modify your manifest and set the activity's launchMode attribute to singleTask

Why dont you use TextViewWithLinks?
With that you can have two types of click on the textview,
1. On TextView
2. On Link TextView
String text = "bananas on www.bananas.ba and Google";
TextViewWithLinks textview = new TextViewWithLinks(this);
textview.setText(Html.fromHtml(text));
textview.linkify(new TextViewWithLinks.OnClickLinksListener() {
#Override
public void onTextViewClick() {
// Do whatever you want
}
#Override
public void onLinkClick(String url) {
// Do whatever you want
}
});
//SET Colors
textview.setLinkColors(Color.RED, Color.BLACK);
setContentView(textview);
Let me know if this is not resolving your issue.
Enjoy Coding... :)

Related

How to make spannable text clickable with Accessibility mode on

I have a problem statement where i need to run my application with Accessibility setting on, to have talk back feedback, but the problem here is when i click on a TextView which have Spannable link in it, then it reads the full text but dose not allow me to click on that Spannable text separately while disabling the accessibility allows to make string multi spannable or link clickable.
here is my code to make String clickable :
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
If you are using Android X library you should be able to handle accessibility and clickable spannable strings by:
ViewCompat.enableAccessibleClickableSpanSupport(yourView);
Also make sure you have the latest dependency:
com.android.support:appcompat-v7:28.0.0
It should work back to API 19.
Note: To enable Android X library go to your gradle.properties and add these lines:
android.useAndroidX = true
android.enableJetifier = true
I'm afraid there is no way in android to implement that (I had the same issue for months). the only way is using the local context menu. Looks like Talkback is trying to make the ADA users to get use to the menus using there gestures, which will fix too many issue in our dev side. There might be another way, which is creating a WebView and then add html elements which will handle everything, BUT this will be bad for the app performance :(
As mentioned here: Clickable links (Google support)
you have to access local context menu to activate any clickable span by Swiping up and then right, and then click on Links submenu.
Hope this helps :)
Try this one, worked for me. An alternate working solution.
private void setUpBottomSheetAccessibility() {
ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
#Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_LONG_CLICKED) {
//Put your work to be done on double click;
}
super.onPopulateAccessibilityEvent(host, event);
}
});
}
Above code view is your Textview or any view. And this event work on double click.
Working fine at my end.
Nothing to do.Just you click the Spannable link with two finger on the screen, and one of them must be on the Spannable link. Try some times!!!

Android partially active Activity

I have an activity A. I am creating a kind of tutorial for user for this activity, to teach him how he can use the app on that screen.
For that, my requirement is :
I want to blur all the views of the activity except one view. I want to prompt user to click on that view through a hand image pointing at that view.
Nothing should happen if the user clicks on the blurred/greyed out area, but if he taps on that particular active view, it should react to that touch.
I was thinking of using a full screen fragment for this. The Fragment will take the following input from the activity :
for what coordinates, is should not blur the screen and pass the touch event to the activity
the coordinates on which it should show that pointing hand image.
After from these coordinates, the fragment background would be blur.
I wanted to confirm if that's possible, to make the fragment partially active, i.e. delegate it's touch events to the activity for a particular view of the activity.
Also, please let me know if there is any other better approach of achieving the same thing.
Edit1 :
Thinking of using a fragment here, because I'd want this type of behaviour on different screen in future. In that case, I'd make that fragment generic which takes some inputs (as described above) and use it on different screens.
There's a very good library called SCV which does what you're trying to achieve, you're able to customize the styles for it too. I've used this for first time the app is opened to show the user a tutorial.
According to their Github
The ShowcaseView (SCV) library is designed to highlight and showcase specific parts of apps to the user with a distinctive and attractive overlay. This library is great for pointing out points of interest for users, gestures, or obscure but useful items.
Further Reading:
Android Arsenal - Showcase Views Tutorial
ShowCaseView on Android - Indipendev
I found it much easier to include an 'extra' layout around the UI of my activity, and then to add a highest-z grey mostly-transparent filter to it and put the instructions on that.
Each "step" of the instructions was a different layout that was dynamically loaded into that layout container as they clicked. (Just another approach)
The 'container' layout is a: FrameLayout
then in my Activity I have: (ignore bad naming)
private void addOverlayLayout() {
frameLayout = (FrameLayout) findViewById(R.id.framelayoutInner);
frameLayout3 = (FrameLayout) findViewById(R.id.framelayout3);
frameLayout3.setBackgroundColor(Color.DKGRAY);
frameLayout3.setAlpha(0.3f);
// Dynamically create a relativelayout which will be appended to framelayout
relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_1));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_2));
if (FullscreenReaderActivity.isFirstRun) {
displayNextGuide();
}
}
public void displayNextGuide() {
// clean relative layout if it has views
relativeLayout.removeAllViews();
// clean frame layout if it has child (safe if empty)
frameLayout.removeView(relativeLayout);
if (!isFirstRun) {
return;
}
if (instructionOverlays.size() > 0) {
runOnUiThread(instructionOverlays.get(0));
instructionOverlays.remove(0);
} else {
frameLayout3.setBackgroundColor(Color.TRANSPARENT);
frameLayout3.setAlpha(1.0f);
}
}
public Runnable createSimpleClickInstruction(final int resource) {
return new Runnable() {
#Override
public void run() {
getLayoutInflater().inflate(
resource,
relativeLayout,
true
);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayNextGuide();
}
});
frameLayout.addView(relativeLayout);
}
};
}

Custom button form (House/Tree etc.)

I'm a bit new to Android, therefore question may sound ridiculous, but:
Is there any way to make a button in form of smthg? There ate tons of apps, where you press on a tree or some house and some action starts. How is that made?
Not action, but form. Or there is no way to make button NOT rectangular?
You are talking about imagebutton.Image button documentation
For example creating a button with tree background.Example:
<ImageButton android:src="#drawable/tree.png"></ImageButton>
Yes you can do that. Considering your trees and house as an image.
ImageView im;
im = (ImageView) findViewById(R.id.image_of_tree);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//perform some action
}
});
Off-course When you get inflated your layout and your view(can be any view) is available by id. your button can be an ImageView, ImageButton etc. just find the id for your view and set whatever event you want to listen by this view.

How could I create a custom textview which has specifed distance to its left when meet some characters,for instance ,like '#'

I don't know how to solve this issue,there are some strings needed to fill in the textview,"#Tony#Tom#James#Brown...",what I want to do is like following "#Tony #Tom #James #Brown..." this will be displayed in the textview,the spacing is also specified,for example,"20dp" is the distance between '#Tony' and '#Tom'.I don't know how to create a textview likes above,I have a another question,when the content is out of range,the content should start from next line,so do I deal with this problem?Thank you anyone who gives an answer. If I didn't discribe this question in detail,forgive me.
You can use https://android-arsenal.com/details/1/2242 .
<com.apradanas.simplelinkabletext.LinkableEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
// find username
Link linkUsername = new Link(Pattern.compile("(#\\w+)"))
.setUnderlined(false)
.setTextColor(Color.parseColor("#D00000"))
.setTextStyle(TextStyle.BOLD)
.setClickListener(new Link.OnClickListener() {
#Override
public void onClick(String text) {
// do something
}
});
more information is available on the android arsenal page

how to add an action to part of a string android

I have the fallowing text: "By clicking OK you will disable the service. Learn more".
i want to make "Learn more" clickable, however i want a popup menu to appear instead of directing to a website
i have used the fallowing stack question :
How to set the part of the text view is clickable
which worked great. i found the index of learn more by ". ". this solution crashes the application in Chinese and Hindi languages (in Hindi a point is written -> |).
How can i make the "Learn more" clickable in a generic way to show a popup menu?
Is there maybe a way to define the click action in strings.xml, like calling a link? (instead of calling a link -> launch popup menu?)
You can use WebView and anchor. Create new WebViewClient (especially you need this method: shouldOverrideUrlLoading()) and do everything you want when user will click your anchor.
You can create a click event based on the text as you defined.. Check this library.. it may help you.. https://github.com/klinker24/Android-TextView-LinkBuilder
solved it, might be a hack but it works fine.
in strings.xml i have added
//<a></a> tags to be removed later on
<string name="learn_more">By clicking OK you will disable the service. <a>Learn more</a></string>
in the code :
TextView textView= (TextView) findViewById(R.id.textViewInLayout);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(R.string.learn_more);
//indexes of the clickable text
int start = textView.getText().toString().indexOf("<a>");
int end = textView.getText().toString().indexOf("</a>");
//set the text as html to make the tags disappear
textView.setText(Html.fromHtml(getString(R.string.learn_more)));
//make the text clickable
Spannable spannable = (Spannable) textView.getText();
ClickableSpan myClickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
yourActionHere();
}
};
// end - 3 beacuse of </a>
spannable.setSpan(myClickableSpan, start, end - 3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);`

Categories

Resources