I am using rating bar to mark a field as favorite. The user should be able to unmark it some time in future. But once i set it, the on-click listener is not working on that item.
XML code
<RatingBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numStars="1"
android:stepSize="1"
android:id="#+id/fav"/>
JAVA code
fav.setOnClickListener(new View.OnClickListener(){ //fav is a ratingbar
public void onClick(View view){
favRest = !favRest; //A boolean variable which is set/reset each time it is clicked
if(favRest)
{ fav.setRating(1.0f);
addFav(uname,hotelName);
}
else{
fav.setRating(0.0f);
removeFav(uname,hotelName);
}
}
});
Once you rate, its not possible to clear rate using touch on RatingBar. You can only change rate value from 1.0 to 5.0.
To clear/reset rate value, you have to use other action like adding a Clear/Reset Button.
In Button click listener, you can reset rating value by using setRating(0.0)
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Reset
ratingBar.setRating(0.0);
}
});
UPDATED:
In your RatingBar, you are using android:numStars="1" and android:stepSize="1". So once you rate 1 its not possible to rate 0 by using touch on RatingBar
From my point of view:
You can use ImageView instead of RatingBar
Add two different icons for favorite and unfavorite in res/drawable folder
Set desired icon to ImageView as per checking favRest value.
Try This:
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:id="#+id/fav" />
imageview = (ImageView) findViewById(R.id.fav);
fav.setOnClickListener(new View.OnClickListener(){ //fav is a ratingbar
public void onClick(View view){
favRest = !favRest; //A boolean variable which is set/reset each time it is clicked
if(favRest)
{
imageview.setImageResource(R.drawable.icon_favorite);
addFav(uname,hotelName);
}
else
{
imageview.setImageResource(R.drawable.icon_unfavorite);
removeFav(uname,hotelName);
}
}
});
Hope this will help~
Related
I am making a music player app in which there is an image of the play button and once clicked it switch to pause.png and song start playing. Clicking again on the button will change the image to play.png and pause the sound. This pattern continues.
This question has been previously answered first click change to new image and second click change to old image, android
But the checked answer doesn't work because boolean variable used to switch need to be declared final. And once declared final I cannot change the value of the variable.
boolean showingFirst = true; //Declare globally
image1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(showingFirst){
image1.setImageResource(R.drawable.img1);
showingFirst = false;
}else{
image1.setImageResource(R.drawable.img2);
showingFirst = true;
}
}
});
You can do this in this way using tag attribute of ImageView:
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(iv.getTag().equals("playing")){
iv.setImageResource(R.mipmap.play);
iv.setTag("paused");
} else {
iv.setImageResource(R.mipmap.pause);
iv.setTag("playing");
}
}
});
And your ImageView declaration in xml file will be like below:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/pause"
android:tag="playing"
android:id="#+id/iv"/>
I have an indicator rating bar:
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:rating="2.5"
android:numStars="5"/>
You can see that the xml tells the rating bar to reflect 2.5 stars currently.
At a click of a button, I want it to reflect 5 stars instead, so I wrote this code:
indicatorRatingBar = (RatingBar) findViewById(R.id.ratingBar);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indicatorRatingBar.setIsIndicator(false);
indicatorRatingBar.setRating(5);
indicatorRatingBar.setIsIndicator(true);
}
});
The rating bar does not update at all. I don't know if it is because it is an indicator rating bar?
How can I get this to work properly with the rating bar set at the indicator style?
I actually found the answer through trial and error. I just needed to call invalidate on the view as it has changed and it doesn't invalidate itself automatically.
indicatorRatingBar = (RatingBar) findViewById(R.id.ratingBar);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indicatorRatingBar.setRating(5);
indicatorRatingBar.invalidate();
indicatorRatingBar.setIsIndicator(true);
}
});
Change it to
indicatorRatingBar.setRating(5.0f);
I've searched the forums, but not have found any specific or understandable answers for my problem.
I'd like to change my Imagebutton image to a picture, selected from the gallery. Prefferrably the image should stay changed after closing the application.
My XML for the button is here:
<ImageButton
android:id="#+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:background="#drawable/eat"
android:clickable="true"
android:longClickable="true"
android:scaleType="fitCenter" />
The java code for playing the sound is here with the OnClick method.
ImageButton eat = (ImageButton) findViewById (R.id.eat);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. Can You please guide me a little bit?
You need to return true from image's onLongClickListener.
Like this:
eat.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//do something
return true;
}
});
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener.
I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.
I have a TextView with the android:onClick attribute. When clicked, the TextView disappears. I don't want the TextView to disappear when clicked. Any ideas?
Edit:
<TextView android:id="#+id/textView1"android:text="Click Me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="processClick"
android:clickable="true"/>
http://i1179.photobucket.com/albums/x386/jenningsr2006/unclicked.png
http://i1179.photobucket.com/albums/x386/jenningsr2006/clicked.png
Edit
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
TextView t = (TextView)findViewById(R.id.textView1111);
t.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
Clicking it does the operation correctly, that's not the problem. When I "mousedown" on the TextView, it disappears, then reappears on "mouseup".
I thought I had the same problem but it turned out the textview was not dissapearing, rather the color was changing so that it was the same as the background color. Thus it appeared hidden but it really was there. You can set the clicked color of the text view by setting it's color state list resource
http://developer.android.com/guide/topics/resources/color-list-resource.html
Have you registered a method processClick? There is no need to do it this way. Remove the clickable property and also onClick property. More simple approach is to set onClick listener from the code, for example in onCreate method:
TextView text = (TextView) findViewById(textView1);
text.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
}
});
The view becomes clickable automatically when you set an on click listener. Good luck