Smiley Rating bar
Hello,I just getting started with android ,and I am trying to use smiley rating bar,i add it to my project,
my problems is :
1-Get current selection (the choice of the user) i read the instructions on the git,but i didn't understand how can i get the user choice.
2-send that choice or the selection of the user to an email address without opening the email app ( directly send ).
this the git https://github.com/sujithkanna/SmileyRating.
Bind the view from xml in your java class by smileRatingBar = findViewById(R.id.smile_rating_bar_id);
then you need to add listener for it:
smileRatingBar.setOnSmileySelectionListener(new SmileRating.OnSmileySelectionListener() {
#Override
public void onSmileySelected(#BaseRating.Smiley int smiley, boolean reselected) {
// reselected is false when user selects different smiley that previously selected one
// true when the same smiley is selected.
// Except if it first time, then the value will be false.
switch (smiley) {
case SmileRating.BAD:
Log.i(TAG, "Bad");
break;
case SmileRating.GOOD:
Log.i(TAG, "Good");
break;
case SmileRating.GREAT:
Log.i(TAG, "Great");
break;
case SmileRating.OKAY:
Log.i(TAG, "Okay");
break;
case SmileRating.TERRIBLE:
Log.i(TAG, "Terrible");
break;
}
}
});
Your first query i.e. how to know the current selection (the choice of the user) can be known by the following way :
SmileRating smileRating = findViewById(R.id.feedback_rating);
int level = smileRating.getRating();
Where Level will return 0 if NONE selected else will return 1 to 5 ranging where 1 is Terrible while 5 is Great.
Related
I have a simple switch case:
switch (view.getId()){
case R.id.one:
break;
case R.id.two:
break;
}
What I would like to do is write something smart and self sufficient to obtain the text value of the mentioned textviews. For example r.id.one holds text of 1, while r.id.two holds text of 2.
Whenever I press 1 I want to get it's text value.
I know it can be done by the following way:
TextView one = (TextView)findviewbyid(r.id.one);
one.getText();
But with the increase of textviews it will be hard to maintain, as I want to use the obtained value later on.
Thanks advance to all the downvotes, really helpful.
Solution:
switch (view.getId()){
case R.id.one:
String number = (String) ((TextView)view).getText();
break;
case R.id.two:
String number = (String) ((TextView)view).getText();
break;
}
Now number receives the value from the textview. Thanks all.
Instead of a switch, you can try to get the text if the view is any TextView. For instance:
if (view instanceof TextView) {
((TextView) view).getText();
}
Try Butterknife library.
Use something like :
#OnClick({R.id.textviewID1, R.id.textviewID1)
protected void onTextViewClick(TextView textView) {
textView.getText();
}
This allows you to use the same callback function for each textview using just a simple annotation.
And makes the code more readable as well.
I am doing a programme for database. I have created three edit text for adding employee id, name and phone no. The issue is when I press add button without inserting any value the app crashes. How do i handle this? The following is the code which I have written.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.btn_add:
Model india = new Model();
india.id=Integer.parseInt(edit_empid.getText().toString()); // convet editable object from get text to string and then into integer
india.name = edit_name.getText().toString();
india.phone = edit_phone.getText().toString();
db.addcontact(india);
list = db.getAllContacts();
print(list);
edit_empid.setText(" ");
edit_name.setText(" ");
edit_phone.setText(" ");
break;
case R.id.btn_delete:
long id = Long.parseLong(edit_empid.getText().toString());
db.deletecontact(id);
list = db.getAllContacts();
print(list);
break;
case R.id.btn_update:
Model Japan = new Model();
Japan.id=Integer.parseInt(edit_empid.getText().toString().trim());
Japan.name = edit_name.getText().toString();
Japan.phone = edit_phone.getText().toString();
int result = db.updatecontact(Japan);
System.out.println("Print the update result " + result);
list = db.getAllContacts();
print(list);
break;
case R.id.btn_clear:
edit_empid.setText(" ");
edit_name.setText(" ");
edit_phone.setText(" ");
break;
}
}
}
The best way is to use TextInputLayout. Google introduced it in new design library. In order to use the TextInputLayout you have to add the following to your build.gradle dependencies:
compile 'com.android.support:design:22.2.0'
Then use it in your xml files:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
</android.support.design.widget.TextInputLayout>
Before doing your database works check EditText value with if statement and if it is incorrect try:
TextInputLayout til = (TextInputLayout) findViewById(R.id.textInputLayout);
editText.setErrorEnabled(true);
editText.setError("You need to enter a name");
Just add an if statment of your input argument to check - isempty
Thats my guess
User input is always evil! make that clear in your mind. you can never trust users to put only the right stuff into your inputs.
so just bounds check every input. If you want to get no empty strings check for it via
String input1 = edittext1.getText();
if(1 > input1.length()) {
// tell user to check edittext1
} else {
// do your update stuff
}
same stuff need to happen for every input. if you e. g. try to get int values check em if they fit in your expected range and so on....
by this you don't need to put a space value into your editTexts while clearing em ... (which need to be deleted by user if he wants to set the edittext correctly (-> evil user just ignores the space and enters its name and you will have to check for leading or ending sign of space before putting it in your database )
hint: regex strings to ensure input is correct ;)
There is an excellent Library that handles Android Validation:
Android Saripaar v2
I am using badoo Star Bar, and I have it all set up and working expect for when the method public void onFinalRating(int rating, boolean swipe) { is called, the number of stars that I have selected doesn't stay highlighted, it goes back to the default state. Here is the repo on git hub https://github.com/badoo/StarBar
And my set up is exactly the same, haven't changed anything but here it is anyways,
This is my layout
<com.badoo.mobile.views.starbar.StarBar
android:id="#+id/starBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
And then here I implement it
mStarBar = (StarBar)findViewById(R.id.starBar);
mStarBar.setOnRatingSliderChangeListener(new StarBar.OnRatingSliderChangeListener() {
#Override
public boolean onStartRating() {
// The user has initiated a rating by touching the StarBar. This call will
// immediately followed by a call to onPendingRating with the initial rating
// value.
Toast.makeText(DiningDetailActivity.this, "Started rating", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public void onPendingRating(int rating) {
// This method will first be called when the user initiates a rating and then
// subsequently as the rating is updated (by the user swiping his finger along
// the bar).
Log.i(TAG, Integer.toString(rating) + "");
}
#Override
public void onFinalRating(int rating, boolean swipe) {
// If the rating is completed successfully onFinalRating is called with the
// final result. The swipe parameter specifies if the rating was done using
// a tap (false) or a swipe (true).
Toast.makeText(DiningDetailActivity.this, "Final rating " + rating, Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelRating() {
// Called if the user cancels the rating by swiping away from the StarBar and releasing.
}
});
So my question is how when I select lets say 4 stars to get them to say highlighted, instead of going back to the gray state?
I have looked at the readME file and gone over his code and can't seem to find it.
Thanks so much for the help :)
You need to save the ratings. You only show toasts. I dont know the methods of the library but there is probably a method for changing the active stars.
I have a ListView and each row contains a different User object.
A User has these different fields:
List of things I like
List of things I hate
List of games I play
Description of myself
Now, when I populate the ListView, I want to show different things that each user has filled out. So for example, one row might show 'things I like' filled out, while another might show the 'description of myself'.
I don't want to adjacent rows to be showing the same type of row. All this code will be inserted into my BaseAdapter's getView methood.
What would be the best logic in terms of pseudocode to achieve this?
Thanks in advance!!
Updated: Solution
// Creates arraylist of elements that are complete in profile
List<String> profileElements = new ArrayList<String>();
if (user.getGameOwned() != null && user.getGameOwned().size() > 2) {
profileElements.add(KEY_GAMES_PLAYED);
}
if (user.getAboutMe() != null && !user.getAboutMe().isEmpty()) {
profileElements.add(KEY_ABOUT_ME);
}
if (user.getIAm() != null && user.getIAm().length > 2) {
profileElements.add(KEY_I_AM);
}
if (profileElements.size() > 0) {
String randomElement = profileElements.get(new Random().nextInt(profileElements.size() - 1));
if (randomElement.equals(KEY_GAMES_PLAYED)) {
// do whatever here
}
else if (randomElement.equals(KEY_ABOUT_ME)) {
// do whatever
}
}
You could try this approach, in your BaseAdapter put a verification if what list do you want to show.
For example:
int ctr = 1; // your counter - try to declare it on the global level
if(ctr<4){//your 4 types of list -
display(ctr); //print the current ctr
ctr++;
}else{
display(ctr);
ctr = 1; //make the value to 1 for the next batch of loop
}
code:
display(int index){
switch (index){
case 1:
//List of things I like
break;
case 2:
//List of things I hate
break;
case 3:
//List of games I play
break;
case 4:
//Description of myself
break;
default:
break;
}
}
You could also do random.nextInt(4) but it might have the same result each row.
I think what you're trying to do could be achieved using an ArrayAdapter and a custom row layout. It is well explained in this article. I'd recommend reading the whole thing but section 6 explains what you want to do.
I have an ImageView Button which loads 3 different images ("tick", "cross" and "N/A"-as an image) every time it's clicked on. What I want to do is when the user presses a button:
If "tick" is selected save it as String "true" in the local database.
If "cross" is selected save it as a String "false" in the local database.
If "N/A" is selected save it as a String "null" in the local database.
Code where images change after every click:
private void configureImageButton1() {
imgBtnOne = (ImageButton)findViewById(R.id.imgBtn1);
imgBtnOne.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
//increase counter to move the next image.
currentImage++;
currentImage = currentImage % numImages;
//Set the image depending on the counter.
switch(currentImage)
{
case 0: imgBtnOne.setImageResource(R.drawable.tick);
break;
case 1: imgBtnOne.setImageResource(R.drawable.redcross);
break;
case 2: imgBtnOne.setImageResource(R.drawable.not_applicable);
break;
default: imgBtnOne.setImageResource(R.drawable.tick);
}
}
});
I have seen everywhere however unable to find exactly what I need. Any pointer or guidance will be much appreciated :).
You can set the string value in the switch statement. for example:
...
case 1:
imgBtnOne.setImageResource(R.drawable.redcross);
currentValue = "false";
break;
case 2:
...
For the database access... take a look at this tutorial:
http://hmkcode.com/android-simple-sqlite-database-tutorial/