I have a dynamic list of subscriber buttons, each which have several dynamically generated card type buttons that are associated with the Subscriber. In my subscriber button onClick, I would like to display (or hide) the Card Type buttons, but I'm having an issue figuring out how to associate the group of cards with the particular subscriber. Here is what I have in my Onclick so far:
View.OnClickListener getSubscriberOnClick(final IdCardSubscriberButton subscriberButton) {
return new View.OnClickListener() {
public void onClick(View v) {
Drawable icon;
if(subscriberButton.isExpanded() == false) {
icon = getResources().getDrawable(R.drawable.ic_id_card_close);
subscriberButton.setExpanded(true);
//Here's where I need to display the card Type buttons. How do I associate the
//card type buttons with this particular subscriber button?
} else {
icon = getResources().getDrawable(R.drawable.ic_id_card_dropdown);
subscriberButton.setExpanded(false);
}
subscriberButton.setCompoundDrawablesWithIntrinsicBounds(null,null,icon,null);
}
};
}
Any help would be appreciated.
Use setTag to set the same tag for buttons in same group. And use getTag to set the visibility of tag with same group.
Related
In particular class, from many places call back is coming, i just want to whether it's coming text or button for example, so that I can set the data accordingly.
NOTE: I'm not talking about parent layout, I want to know exact name where the event click is happened!
If I'm doing this: Log.d("Hello", "Clicked finally: "+ view?.id)
This is coming:
D: Clicked finally: 2131296625
If you are using Kotlin, You can simply check if the View is Button or Image using is operator like:
when(view) {
is Button -> {
// a Button is clicked.
}
is AppCompatImageView -> {
// an Image is clicked.
}
else -> {
// any other view is clicked.
}
}
You can do something like this:
if (view.javaClass.simpleName == Button::class.java.simpleName) {
// it is a button
} else if (view.javaClass.simpleName == TextView::class.java.simpleName) {
// it is a text view
}
Please note that this won't work if you are using any subclass of Button or TextView. You will need to explicitly specify the class you want to check for.
I'm trying to play around with some Kotlin and Anko (more familiar with iOS) and taking from their example, there is this code:
internal open class TextListWithCheckboxItem(val text: String = "") : ListItem {
protected inline fun createTextView(ui: AnkoContext<ListItemAdapter>, init: TextView.() -> Unit) = ui.apply {
textView {
id = android.R.id.text1
text = "Text list item" // default text (for the preview)
isClickable = true
setOnClickListener {
Log.d("test", "message")
}
init()
}
checkBox {
id = View.generateViewId()
setOnClickListener {
Log.d("hi", "bye")
}
init()
}
}.view
My row appears how I want with a checkbox and textview. But I want to bind an action to the row selection not the checkbox selection. Putting a log message in both, I see that I get a log message when the row is selected which flips the checkbox. It does not, however, log my "test:message" from the textView click handler. Is there a way to get around this?
Apparently your issue has been addressed here. As the checkbox is consuming all the focus of ListItem you should set the CheckBox's focusable flag to false:
checkBox {
focusable = View.NOT_FOCUSABLE
}
Unfortunately setFocusable call requires at least API 26, but you could define view .xml and inflate the view manually as described here:
<CheckBox
...
android:focusable="false" />
Alternatively you could try setting a onTouchListener returning false which means the touch event will be passed to underlying views.
Let me know if it works ;)
I am using Gabrielemariotti's Cardslib library to implement card layout in my android application. I am using a custom layout for my cards. Below is the code for creating custom cards:
Card card = new Card(getActivity().getApplicationContext(), R.layout.status_card);
card.setTitle("sample title");
I have three buttons at the bottom of my card (like buttons in Facebook android app). I want to set onClickListener for these buttons. But I am not sure how to do that.
Please help me here.
Thanks,
You have to define your layout.
Then create a Card with this layout, and override the setupInnerViewElements method.
Inside this method you can define your OnClickListener on your buttons, and you can access to all card's values.
public class CustomCard extends Card {
/**
* Constructor with a custom inner layout
*
* #param context
*/
public CustomCard(Context context) {
super(context, R.layout.carddemo_mycard_inner_content);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve button
Button myButton = (Button) view.findViewById(R.id.myButton);
if (myButton != null) {
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Click Listener card=" + getId(),
Toast.LENGTH_LONG).show();
}
});
}
}
}
I have an easy solution for this.
So another way to add onClick listeners, which is a bit easier, is through the XML.
In the xml for the button, you add this line:
android:onClick="methodName"
Where 'methodName' is obviously the name of a method. This will call the method whenever the button is clicked. The next step is obvious - just go into your java activity and create the method that you want called, making sure to take the View as a parameter. So you'd have something like this in your activity class:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
//whatever you want to do here
}
This is a shortcut to creating a whole onClickListener.
Hope that helps. Good luck :)
EDIT:
Remember, you're passing in a view here, so you can get whatever you want off of that view. Since you commented that you need to get the text off of your card, I'll show you how to do that.
Here is your method for this case:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
TextView textFromCard = view.findViewById(R.id.THE_ID_YOU_GAVE_YOUR_TEXTVIEW_IN_THE_XML);
String textFromTextView = textFromCard.getText().toString();
//do whatever you want with the string here
}
I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference.
The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.
Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.
When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.
How can I do this...?
1. How to get value of click button.
2. How to bring back the changed color to default; once the button has been re-clicked.
Plz suggest me...
this is button code
holder.b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clicked) {
holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
} else {
holder.b1.setBackgroundResource(R.drawable.like_icon);
}
clicked = true;
}
});
You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.
Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..
// set a default background color to the button
placeHolder.likeButton.setBackgroundColor(Color.RED);
placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null; // to hold the button animator
#Override
public void onClick(View v) {
// first time this will be null
if(buttonColorAnim != null){
// reverse the color
buttonColorAnim.reverse();
// reset for next time click
buttonColorAnim = null;
// add your code here to remove from database
}
else {
final Button button = (Button) v;
// create a color value animator
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
// add a update listener for the animator.
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
});
// you can also set a delay before start
//buttonColorAnim.setStartDelay(2000); // 2 seconds
// start the animator..
buttonColorAnim.start();
// add your code here to add to database
}
}
});
This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.
Note: You have to set the default button color based on your logic.
#Override
public void onClick(View view) {
if(!check)
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
check = true;
}
else
{
personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
check = false;
}
}
you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.
You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.
Try following:
use setOnClickListener() on the button.
eg.
viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// TODO :
// 1. make webservice call to update like status (Assuming a web service call)
// 2. Implement a callback for webservice call, to get the status of request.
if(success)
a) change the colour of like btn. and insert the data in Db.
b) Also maintain a column in db for likestatus(by default set it false).
}
}
);
Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.
Im trying to implement a feature of my small android app.
There is a image button, and a popup window will appear when I click it. According to which button in that popup window user click, that image button should change its image accordingly.
Like when i click 1 in the popup window, I should inform the button to update image to 1.
Can anybody tell me how to do this?
You can make use of the following method
public void setTabFor(Button btn) {
for (Button button : btnArray) {
if (button == btn) {
if (button == button_one) {
Utils.setTabButton(R.drawable.left_selected, button_one);
} else if (button == button_two) {
Utils.setTabButton(R.drawable.middle_selected, button_two);
} else if (button == button_three) {
Utils.setTabButton(R.drawable.middle_selected, button_three);
} else {
Utils.setTabButton(R.drawable.right_selected, button_four);
}
}
Where setTabButton in Utils class is used to set backgrounddrawable:
public static void setTabButton(int drawable, Button... btn) {
for (Button button : btn) {
button.setBackgroundResource(drawable);
}
}
You should assign an ID to each of the buttons in popup window. Then you have to implement a listener that will notify (and pass the ID to) your object (responsible for the image button) that a button was clicked and your object will update the image button according to the received ID.
Try this,
First of all,you have to assign unique Id for all image button that you used in your application.then which button you clicked with help of Ids u can display buttons as per your choice.
I hope it will help you and solve your problem very soon.