android imagebutton onclick doesnt change resource - android

I want 3 things to change when i click on my imagbutton if boolean is true:
backgroundcolor
set edittext: focusable, clickable
backgroundresource
But only the backgroundcolor is working. The resource doesnt change and the focusable and clickable of the edittext is always false
editfirst.setOnClickListener(new View.OnClickListener() {
boolean check;
#Override
public void onClick(View v) {
if (check == true){
editfirst.setBackgroundResource(R.drawable.edit36px);
editfirst.setBackgroundColor(0xFF99b6b3);
editTextfirst.setFocusable(false);
editTextfirst.setClickable(false);
check = false;
}
else if (check == false){
editfirst.setBackgroundResource(R.drawable.done36px);
editfirst.setBackgroundColor(0xFFb2b2b2);
editTextfirst.setFocusable(true);
editTextfirst.setClickable(true);
check = true;
}
}
});

editfirst.setBackgroundResource(R.drawable.edit36px);
editfirst.setBackgroundColor(0xFF99b6b3);
This will first set the background to edith36px and then replace it again with the color 0xFF99b6b3.
If you want to change the image on the image button you have to use setImageResource(int id).
If you want to disable a view you should use setEnabled(false). I don't think setting clickable to false is working if you have a click listener attached.

Related

Android Studio - Change image when in different state

I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.

how to implement simple like button in listview of each item

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.

How to check TextView Visibility using IF

I have an onCheckChangedListener to show a textView depending on which radio button is selected. I have 1 question and 1 problem that I was wondering if anyone could help me with.
Question: Can you set the radio groups default check value to NO radio button so that none are checked to start with?
Problem: How can I use an IF statement to determine whether a text view is already "visible" and If it is then set it to "gone", I will include my current code.
code:
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch(arg1){
case R.id.rfolk1:
Folk1.start();
TvFolk1.setVisibility(View.VISIBLE);
TvFolk2.setVisibility(View.GONE);
Play.setVisibility(View.VISIBLE);
Pause.setVisibility(View.VISIBLE);
Stop.setVisibility(View.VISIBLE);
Play2.setVisibility(View.GONE);
Pause2.setVisibility(View.GONE);
Stop2.setVisibility(View.GONE);
break;
case R.id.rfolk2:
Folk2.start();
TvFolk2.setVisibility(View.VISIBLE);
TvFolk1.setVisibility(View.GONE);
Play2.setVisibility(View.VISIBLE);
Pause2.setVisibility(View.VISIBLE);
Stop2.setVisibility(View.VISIBLE);
Play.setVisibility(View.GONE);
Pause.setVisibility(View.GONE);
Stop.setVisibility(View.GONE);
break;
}
The View class includes a getVisibility() method. Compare that:
Eg:
if (TvFolk1.getVisibility() == View.VISIBLE)
TvFolk2.setVisibility(View.GONE);
To shorten down code, you can also make a method:
public static void goneIfVisible (View v)
{
if (v.getVisibility() == View.VISIBLE)
v.setVisibility(View.GONE);
}
And keep in mind in Java, variables are lowercased, only use uppercase for class names.
// If TextView is already showing and you want to hide it.
if (TvFolk1.isShown()) {
TvFolk2.setVisibility(View.INVISIBLE);
}
// For uncheck all radio button from radio button groups
RadioGroup rgButton = (RadioGroup)findViewById(R.id.radiobuttongroup);
rgButton.clearCheck();

Make one layout inside the main layout invisible when we click other area on the main layout

I have one custom listview containing imagebuttons and textviews. Right now when I click on the info icon, it opens up the pop up(which is actually a layout that I am making visible on the imagebutton's click) that gives some description and when I click on that icon again it becomes invisible. But I want it to make invisible whenever I click on any other area and not just on that info icon.
//img_Info is the Imagebutton containing i icon
img_Info = (ImageButton)view.findViewById(R.id.img_Info);
img_Info.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//llimg_info is the linearlayout that becomes visible on the click event
if(llimg_info.isShown()) {
llimg_info.setVisibility(llimg_info.INVISIBLE);
}
else {
llimg_info.setVisibility(llimg_info.VISIBLE);
}
}
});
Any suggestions please?
Please try below code for check visibility of ImageView, it will solve your problem.
ImageView llimg_info = (ImageView) findViewById(R.id.img_Info);
if (llimg_info.getVisibility() == 0) {
System.out.println("Visible");
llimg_info.setVisibility(llimg_info.INVISIBLE);
} else{
System.out.println("Invisible");
llimg_info.setVisibility(llimg_info.VISIBLE);
}
Not sure if it will help, but try to register a focus change listener for your pop-up, so that when it will loose focus to make it invisible.
Something like this:
thePopup.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
thePopup.setVisibility(View.GONE);
}
}
});

How to make a view visible when a button is clicked and make that view invisible when the button is again clicked?

I have a layout which is invisible when the activity starts.When I click on a button the layout becomes visible.My requirement is when I click the button for the second time, the layout should be invisible.I know this is a silly question but as I am a new to android, I am unable to figure it out.
Try the following code to toggle the visibility of the view:
v.setVisibility(v.getVisibility() == View.INVISIBLE ? View.VISIBLE
: View.INVISIBLE);
You can also implement by using boolean FLAG.
e.g. Declare
boolean visibility_Flag = false;
button..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(visibility_Flag){
YourView.setVisibility(View.INVISIBLE);
visibility_Flag = false;
} else {
YourView.setVisibility(View.VISIBLE);
visibility_Flag =true;
}
}
});

Categories

Resources