I'm kind of new to this whole thing so I need some help. I have an application that creates an ImageView OnCreate. I want to make this image clickable and have it open a browser to a specific link.
How would I do this? I'm having trouble specifically with the setOnClickListener because the parameters are not accepting an OnClickListener.
I'm developing for Android 1.6
You shoud set ImageView property clickable to true. Then set listener:
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do stuff
}
});
You can subclass ImageView, if you like. Then you can override onClick in your own class. Rabas' method is probably more common though, and seen throughout Google's examples.
Related
Basically, my question is similar to this one:IBM Watson Assistant in Flutter: How to show options?
There is only one answer, telling me to decode the response and show the options as clickable UI elements. I already know how to decode it, but how to make them clickable as a button? Like this:
Example
I am not familiar with RecyclerView enough, maybe some method could do it?
you can add onClickListener to views so if you are using textviews for showing options you can add click listener like this in your RecyclerView Adapter class
textViews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//YOUR CODE
}
});
What I want to achieve:
I want to get the button text as parameter for the function
What I want:
// Operators - setOnClickListener
btnDivide.setOnClickListener { viewModel.mountOperator(it.text) }
btnMultiply.setOnClickListener { viewModel.mountOperator(it.text) }
btnMinus.setOnClickListener { viewModel.mountOperator(it.text) }
btnPlus.setOnClickListener { viewModel.mountOperator(it.text) }
What is working:
// Operators - setOnClickListener
btnDivide.setOnClickListener { viewModel.mountOperator(btnDivide.text) }
btnMultiply.setOnClickListener { viewModel.mountOperator(btnMultiply.text) }
btnMinus.setOnClickListener { viewModel.mountOperator(btnMinus.text) }
btnPlus.setOnClickListener { viewModel.mountOperator(btnPlus.text) }
The problem that is appearing:
Error: Unresolved reference: text, when i am doing it.text
But inside setOnClickListener{ it: View! }, I saw this preview as it is generally shown. So I thought that, here it is refering to the button to which I am setting the onClickListener.
if there is any confusion about the question, please write in the comment. If my approach is wrong, share in the comments
So I thought that, here it is referring to the button to which I am setting the onClickListener.
Here, it is of type View and not of type Button, or whatever type of specific view type you want it to be, so because not all views have a text property (which means that View doesn't have something called text, but Button might), you can use something like this:
btnDivide.setOnClickListener { viewModel.mountOperator((it as Button).text) }
where you can replace Button with whatever type of view it is.
it as Button is casting the View we get into our specific type, in your case I'm assuming this will be a button, but you can cast it to whatever type of view it actually is.
it: View! here does refer to the button you're setting the onClickListener to, but it's returned to you as a View and not of a more specific type
Also to follow up on #a_local_nobody's answer, the reason it's just passing in a generic View and not the type you're setting the click listener on, is because you can create a single listener object/function and reuse it for all the things that need to handle clicks (which could be different subclasses of View)
So if you like, you could make one of those and use it instead of repeating the code for each button:
fun handleButtonClick(view: View) {
if (view is Button) viewModel.mountOperator(view.text)
}
btnDivide.setOnClickListener { handleButtonClick(it) }
// or as a function reference
btnMultiply.setOnClickListener(::handleButtonClick)
And another thing you can do when you're assigning multiple identical click listeners (or doing the same anything to a bunch of things) is to just do a loop
listOf(btnDivide, btnMultiply, btnMinus, btnPlus).forEach {
it.setOnClickListener { //bla bla }
}
that works for reusing a function reference or just creating a new one for each listener (like you're doing in your question) - it just saves you repeating yourself, it can be easier to read, and if you need to make changes you only have to do it in one place
I have a standard LinkMovementMethod established in my TextView to push a web Activity of some sort when the user touches a link. However, I want to establish a "do you want to see the link" dialog rather than taking the user straight to the webpage. I've tried overriding the touch methods but it all gets a little convoluted. A little help?
You can accomplish it in two ways:
Create custom Spans: more complicated, but you can accomplish more customised text consisting of clickable parts (or bold, differently coloured etc). To know more, check out ClickableSpan and SpannableStringBuilder
Extend LinkMovementMethod to accept custom click listener
In my opinion second solution is better in basic cases like yours. Here is how you can do it:
Copy this java class: InternalLinkMovementMethod to your project
Add set the link movement method of your TextView to this custom one, providing a click listener:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
#Override
public boolean onLinkClicked(String linkText) {
// here you can handle your click, eg show the dialog
// `linkText` is the text being clicked (the link)
// return true if handled, false otherwise
}
}
yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener));
I want to create a book in android. It's text come from database and every section of this text has a reference.I need to put clickable image inside text to show reference and when I click on image something like Toast which contain reference list will appear.
I didn't see something like that in typical app. any body have any suggestion to implement it ?
If I understand the question correctly, you will have a clickable image inside the activity. Next add a onClickListener to the image, inside of this display the toast with the reference.
ImageView img = (ImageView) findViewById(R.id.img);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "https://www.google.com/", Toast.LENGTH_SHORT).show();
});
}
If you have multiple clickable images then you might be better off implementing the interface View.OnClickListener and adding the onClick method.
I think the best way is to use Linkify class provided by Android Framework - see http://developer.android.com/reference/android/text/util/Linkify.html for details.
Also you can play with google sample - https://github.com/googlesamples/android-TextLinkify
I have been experiencing a problem with my custom adapter and have located the problem solution. However I do not know how to go about it. I have researched and seen a few examples of a listview custom adapter at which buttons are given tags like viewHolder.button.setTag(Tag) and I understand what the tag does but I am unsure as to how to use it. My questions are: When I set the tag on a button, how does the application differentiate my buttons from another if all the tags are set the same? Also, say I have an onClick method in my custom adapter, how do I use the tag that I set to the button to identify the button that was clicked? I've kind of seen similar adapters on the internet but not exactly , a link to an example would be greatly appreciated also.
I am unsure as to how to use it
tag is a mechanism to make your views remember something, that could be an object an integer a string or anything you like.
My questions are: When I set the tag on a button, how does the
application differentiate my buttons from another if all the tags are
set the same?
i do not understand this question but i think if you notice that your button has a memory and it calls tag you can use it in a better way. if all of your buttons memory(tags) are the same so you can not use tags to distinguish the buttons you must use ids.
say I have an onClick method in my custom adapter, how do I use the
tag that I set to the button to identify the button that was clicked?
you must set different tags for your buttons or grouped them logically and set different tags for each group then in your onClick method use the tags to identify your buttons group:
OnClickListener myButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Object obj = arg0.getTag();
if(obj instanceOf groupOneTagObject){
// do action for group 1
}else if(obj instanceOf groupTwoTagObject){
// do action for group 2
}
}
});