Set a String value from a Image in an ImageView Button - android

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/

Related

How to Get current selection From Smiley Rating bar?

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.

Is there a simpler way to code the following requirement and compress the code?

I have made a custom bottom bar. It has got five images. On selecting a particular image a fragment will open and the selected image will turn blue. The rest will remain black.
So what I am doing now is
img1,img2,img3,img4,img5
So I set click listener to it and doing it like
#Override
public void onClick(View view) {
try {
switch (view.getId()) {
case R.id.img1:
// Set background image as blue to img1 and set //black for others
break;
case R.id.img2:
// Set background image as blue to img2 and set //black for others including the previous one
break;
case R.id.img3:
break;
case R.id.img4:
break;
case R.id.img5:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
So I am doing the repetitive steps again and again . can I compress this code to something shorter , like storing the previous image in a view and coloring the current one ?? In nutshell what is the best way to compress this code ?
Thank you :)
You can set the background color of all the images to black BEFORE the try sentence, and then set only the selcted image's background to blue:
#Override
public void onClick(View view) {
//Set black background for all the imags
try {
switch (view.getId()) {
case R.id.img1:
// Set background image as blue to img1 for others
break;

How to realise multiple text editing from one button?

Here is the goal: 1 textview, 1 button. after pressing button text in textview is editing to new string. I tryed to realise it like this, but it
doesn`t work. Please help. There is 1 activity, 1 button, 1 tv and multiple pressings to edit text.
public class Second extends AppCompatActivity {
TextView tv;
Button btn;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
i = 1;
tv.setText(R.string.s1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (i=1;i<28;i++);{
switch (i){
case 1:tv.setText(R.string.s1);
break;
case 2:tv.setText(R.string.s2);
break;
case 3:tv.setText(R.string.s3);
break;
case 4:tv.setText(R.string.s4);
break;
case 5:tv.setText(R.string.s5);
break;
case 6:tv.setText(R.string.s6);
break;
case 7:tv.setBackgroundResource(R.drawable.tigs);
tv.setHeight(430);
tv.setWidth(350);
tv.setText(R.string.s6_1);
break;
default:break;
What i`m doing wrong?
At the end you will only see the value of R.string.s6 in your edit text and the background will be set as R.drawable.tigs. The for loop executes in a fraction of second and set value one by one. But due to its fast execution you will only see the last value. But if you want to see all the text one by one you can use a java thread and call its sleep method by passing the time in milliseconds.

Switch case, getText of the case android

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.

onclick not working in resume method

i have a problem in my application, i have a formulaire whish a user should fill information and save it to database,i have both Edit Text and Radio Button :
rm_1 = (EditText) findViewById(R.id.rm_1);
rm_2 = (EditText) findViewById(R.id.rm_2);
rm_3 = (EditText) findViewById(R.id.rm_3);
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_3);
i have a method Onclick whish associate each radio buton selected with a value :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((Checkable) view).isChecked();
switch (view.getId()) {
case R.id.rm_13_1:
if (checked)
a = 0;
break;
case R.id.rm_13_2:
if (checked)
a = 1;
break;
case R.id.rm_14_1:
if (checked)
b = 0;
break;
case R.id.rm_14_2:
if (checked)
b = 1;
break;
case R.id.rm_14_3:
if (checked)
b = 2;
break;
case R.id.rm_14_4:
if (checked)
b = 3;
break;
}
until now everything works fine, the user writes in the edit text and select the radio button , and in database i find the same information.
in order to save the data entered by the user i did used shared preferences, so the text writing by the user and radio button selected appear again when the user returns to the activity.
That's when the issue occurs when a user change the activity , so if he returns to the activity he finds the radio button already selected but when he click on save button the value he gets in database is zero, it is like the methode :
public void onRadioButtonClicked(View view)
is not working, i don't know why ?? the user need to click again on radio button to have the values assigned in the method onRadioButtonClicked(View view), so how to solve that ?
this is where i save data :
Button bton = (Button) findViewById(R.id.ajoutUn);
bton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ajouter(v);
}
public void ajouter(View v) {
db.open();
db.insertMENAGE(rm_1ts, rm_2ts, rm_3ts, rm_4ts, rm_5ts,
rm_6ts, rm_7ts, rm_8ts, rm_9ts, rm_10ts, rm_11ts,
a, b, rm_14_4_autrets, rm_15ts);}}
And the method in database is :
public long insertMENAGE(String Region, String Provence_prefecture , String Commune_Arrondissement ,String N_district, String N_M_district , String N_menage_logement, String Adresse_menage , String Nom_Enqueteur, String code_enquêteur , String Date_realisation_enquête, String Nom_controleur , String Date_controle, int echantillon_principal, int Statut_enquêté , String autre, String Observations ) {
ContentValues initialValues = new ContentValues();
initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement);
initialValues.put(col_N_district,N_district);
initialValues.put(col_N_M_district,N_M_district);
initialValues.put(col_N_menage_logement,N_menage_logement);
initialValues.put(col_Adresse_menage,Adresse_menage);
initialValues.put(col_Nom_Enqueteur,Nom_Enqueteur);
initialValues.put(col_code_enquêteur ,code_enquêteur);
initialValues.put(col_Date_realisation_enquête,Date_realisation_enquête);
initialValues.put(col_Nom_controleur,Nom_controleur);
initialValues.put(col_Date_controle,Date_controle);
initialValues.put(col_echantillon_principal,echantillon_principal);
initialValues.put(col_Statut_enquêté,Statut_enquêté);
initialValues.put(col_Observations,Observations);
return db.insertOrThrow(MENAGE,null, initialValues);
}
Check all your radio button values at your onResume() method like following, and assigning values to your variable.
if(rm_13_1.isChecked())
{
}
else if(rm_13_2.isChecked())
{
}
else if(rm_14_1.isChecked())
{
}
else if(rm_14_2.isChecked())
{
}
else if(rm_14_3.isChecked())
{
}
Hope it will work
First of all I'm posting this an answer because the text is too large.
Either there's some meaningful code that we're not seeing, or there's a problem understanding a few things.
The method onRadioButtonClicked(View view) is only called when the user clicks the radioButton. When the user comes back from another Activity the button is already selected. This is correct, and this is how it should behave. As far as I can see, this code only affects variables 'a' and 'b'.
On the other hand you say that you have a separate 'save' button. When the user comes back from another Acivity, variables 'a' and 'b' should preserve their values, and the results of your 'save' code should work.
I know this is not an answer, just a few insights that hopefully help you in some way.

Categories

Resources