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);
Related
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~
I just want the normal menu which gets displayed on clicking the settings button in android phone instead with Virtual Button Click. I tried openOptionsMenu(); but it didn't help. I hope you understand the problem here: I want for example default "setting" option displayed which we see when I click on button in actionbar but this time I have action bar hidden so I have a button which I want to display the menu on Clicking that button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
You can set its property in xml file to invisible or gone & in the .java file set it to visible see code below:
Note: In my case I'm using Buttons you can use according to your own
.java file:
Button next,btn;
public void newBtnL(View view){
next = (Button) findViewById(R.id.ListenBtn);
btn = (Button) findViewById(R.id.newBtn);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
}
});
}
.xml file:
<Button
android:id="#+id/ListenBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this"
android:layout_below="#+id/btn_stop_service"
android:onClick="newBtnL"/>
<Button
android:id="#+id/newBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ListenBtn"
android:text="that"
android:visibility="invisible"/>
I have a progress bar that I want to click on and move the progress to the point clicked.
i have
pb = (ProgressBar) v.findViewById(R.id.progressBar);
pb.setOnClickListener(new ProgressBar.OnClickListener() {
public void onClick(View v) {
//
}
}
Basically I want it to behave like a SeekBar, but I need to use a progress bar because I am using a circular progress bar, and the code I have visually fits my needs.
cheers,
I went with a custom seekbar
https://github.com/JesusM/HoloCircleSeekBar/blob/master/lib/src/main/java/com/jesusm/holocircleseekbar/lib/HoloCircleSeekBar.java
This is a circular seekbar, as per my requirements
I'd like to add an onclick listener to my launcer icon in the title bar of my app.
Since i'm also supporting API level 8 i do not have an Action bar.
The following code works great, however the menu is set back to default (white background, white text, small icon etc.)
The code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
source: adding click listener to titlebar image
How can I keep my standard layout of my title bar (black background color, white text and large icon), while also implement this onclick listener?
Here are two shots of the different layouts:
good: http://gyazo.com/40d1cdd5302de3cd28b698b68164a556
bad: http://gyazo.com/3cee42524ec4167392baec6cc2369584
(Note that the good one is also has a greater height)
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