I have sort of a file manager. It displays files in a ListView. Each of those ListViews has a custom footer - a button, which is defined in a xml file. Pressing a button allows user to chose a file from the "downloads" directory and copy it to the folder where the button was pressed. This is implemented through the AlertDialog. So by clicking the option from the dialog I get a path of a file that should be copied.
Now, how do I get the location from which my button was pressed? Can I somehow pack some sort of data in my button on its creation so that later when it is pressed I could identify its initial location?
there are many ways to achieve that. you can create a separate OnClickListener for each button as JaLoveAst1k proposes. you could also add some information to the buttons's tag (setTag()) and get it back in the onClick() function. Yet another way would be to have a Hashtable<Button, String> where you store your string informaton relative to the Button.
Yes, like this:
protected class CustomListener implements View.OnClickListener {
private String text;
public CustomListener(String text) {
this.text = text;
}
...
}
And set this listener to button, in constructor give data.
Related
I have html string:
htmlString = "This is a link and another link link2"
I set this String to my TextView. Here is my code:
tvText.setText(Html.fromHtml(htmlString));
And also I set LinkMovementMethod, links to be clickable.
tvText.setMovementMethod(LinkMovementMethod.getInstance());
Everything is fine, but links open in the browser by default, and I need to open them inside the application in web view.
Can I somehow get the link from the clicked String ? So that I can load the link in web view.
Or do I need to manually search for all links in the String and put clicks on them (ClickableSpan)?
Please, help me.
Here is how you can do it:
Extend LinkMovementMethod to accept custom click listener
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)
Base on: Intercept link LinkMovementMethod with a Yes/No dialog
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 have some edittexts and listview for data and buttons like add,update and delete. What is the best practice for controlling the buttons to be enabled or not, using a public method like..
public void CheckButtonStates(int Condition, List<ImageButton> tmlist)
{
switch (Condition)
{
case 1://Start
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(false);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(0.4f);
break;
case 2://Add new
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(true);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(1f);
break;
}
or there is another way more efficient?
I think the best way to control this situation is to control the visibility or the enabled of the buttons from buttons click.
When the app starts you have your edittexts empty and save button enable in order to save new data in sqlite database.
When your user click something from the listview take the data from selected list item fill the edittexts and set disable the save button and enable update and delete button.
When user click delete or update button save or delete data in sqlite and set your edittext fields empty and enable save button and disable update and delete buttons.
Example:
btnUpdate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//Save data to sqlite
//Set your edittexts empty
btnUpdate.setVisibility(View.GONE);
btnDelete.setVisibility(View.GONE);
btnSave.setVisibility(View.VISIBLE);
}
});
Set a listener for each button and decide which functions should not be able due to the last function,it all depends on your needs. like this:
UpdateButton.setonclickListener(new.....)
{
//onclick:{
updateButton.setEnabled(true);
}
}
Hope you understand what I tried to do,if not tell me and I will update a full code.
I have a Button 'Remove' on a View, lets call it UserInputView. UserInputView is added to a Layout by user input. Now, the user may have typed something wrong, and wants to press the Remove-button I've created, to remove this particular UserInputView.
I've added an EventHandler for the Click-event of the Remove-button. Now I need to find what View-object this particular Button belongs to so I can call _myLayout.RemoveView(theViewICantFind). How do I find this View-object? I can't seem find it by any properties of the Button in question.
Or perhaps, is it another clever way of removing the UserInputView from the Layout that I don't know about?
Thanks folks!
In your EventHandler, there is a sender object. That is the actual View object.
I suppose you could remove it like this in the Click handler:
public void MyButtonHandler(object sender, EventArgs args)
{
var button = (Button)sender;
_myLayout.RemoveView(button);
}
If you want to remove the view the Button belongs to, you can just get it by calling button.Parent.
i work on an app and i have this problem.
Example:
i have 4 buttons in the main page:
APPLE,
STRAWBERRY,
CHERRY,
FAVORITES
if i click on APPLE STRAWBERRY OR CHERRY, it will open another activity with 3 more buttons
APPLE HISTORY,
MADE APPLE JUICE,
HOW TO GROW APPLE
Each of this buttons when clicked, open another activity with some data and text
I would like when someone made a long press on APPLE, STRAWBERRY or CHERRY to open a windows that ask if i want to save the selected item to FAVORITES, so it will copy the button inside the activity FAVORITES with the option to delete this choice in the future.
Can someone help me?
Thanks
You can set a listener for a long click to handle it
yourButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Inside this listener you can open a Dialog, where the user can choose if he wants to add it to Favorites or not. Furthermore save the users choice in SharedPreferences with a boolean or something which indicates that he made his choice. In your Favorite Activity read the SharedPreferences file and handle accordingly.
This could be done by adding Views programatically to have a dynamic setup. Or if the repertory is limited, like your example, set the Visibility of some Views / Buttons in your XML to gone and change it to Visible by reference to the users input.
You could save all your favorites items in a csv file, in a preference, in a xml file.. There are millions of way to achieve it.
I would personally use a sharedPreference to let the user edit it easily.
Is it possible to add an array or object to SharedPreferences on Android