So, let me explain the default situation at first: A user can choose a picture - and this picture is saved by a string (e.g. "picture1") in a Properties-file. Now I wan't to display the picture by loading the String off the Properties-file and getting the Image out of my Drawables-Resources by R.drawable.MYPICTURE.
this is what I worked out:
String iconsString[] = {"default", "icon"};
int iconsResource[] = {R.drawable.default, R.drawable.icon};
int iconResourcePosition;
int iconsStringLength = iconsString.length;
for (int i = 0; i < iconsStringLength; i++) {
if (iconsString[i] == mProperties.getProperty("icon")) {
iconResourcePosition = i;
} else {
iconResourcePosition = 0;
}
}
btn_profileIcon.setBackgroundDrawable(iconsResource[iconResourcePosition]);
But it doesn't work, since the ".setBackgroundDrawable" does not accept int-values. Well, and that's where I'm stuck. I could make the "iconsResource[]" beeing "Drawable" instead of "int", but that would cause an other problem :|
Thanks for the help!
mmmm don't you want to do something like this:
btn_profileIcon.setBackgroundResource(iconsResource[iconResourcePosition]);
Use BitmapFactory.decodeResource(…) to store a reusable Bitmap and then use setImageBitmap(…) on your ImageView (or derived).
Related
I am new to Java and building an Android app using it, I want to be able to determine how many switches are on but not sure how the best way to sum an array of switches. I currently have:
SS = new Switch[8];
SS[0] = getView().findViewById(R.id.switch1);
SS[1] = getView().findViewById(R.id.switch2);
SS[2] = getView().findViewById(R.id.switch3);
SS[3] = getView().findViewById(R.id.switch4);
SS[4] = getView().findViewById(R.id.switch5);
SS[5] = getView().findViewById(R.id.switch6);
SS[6] = getView().findViewById(R.id.switch7);
SS[7] = getView().findViewById(R.id.switch8);
submit = getView().findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
totalactive= IntStream.of(SS).sum();
with the array "SS" declared as a class global.
Thank you
Inside onClick use a for each loop:
totalactive = 0;
for (Switch s : SS) {
if (s.isChecked()) totalactive++;
}
All the answers so far are great! But then, you could also do it the old school way:
int totalactive = 0;
for(int i = 0; i < SS.length; i++){
if(SS[i].isChecked()){
totalactive++;
}
}
//Here's your result:
Log.d("sumTotal", Integer.toString(totalactive));
I bet this is very easy to understand and relate to. I hope this helps. Merry coding!
I have multiple buttons containing some information.
Now I want that the information written on the buttons are appended when i press them i.e when i press the first button
- the information gets printed into text field
When I press second button
- the information written on button gets appended or added into the text field with the older information (data in button 1).
code for what I am trying is:
private void EActionPerformed(java.awt.event.ActionEventevt) {
String x = ans.getText();
for(int i = 0; i < x.length(); i++) {
ans.setText("H");
}
}
private void FActionPerformed(java.awt.event.ActionEvent evt) {
String x = ans.getText();
for(int i = 0; i > x.length(); i++) {
ans.setText("A");
System.out.println("completed");
}
}
Simply:
ans.setText(ans.getText() + newString);
There are a few ways to this, setText is one, but it's not particularly efficient (it's easier to type though), as you are creating additional temporary objects though the process
If you need to update the field often, you might consider using something more like...
Document doc = userNameField.getDocument();
doc.insertString(doc.getLength(), newString, null);
It may seem a silly question but I've tried a variety of ways and have no idea how to solve. I am new to android programming and am completing a spinner with a list of objects. When you select the object in need spinner pick up the object ID and the number of animals of the same.
My last attempt was is, but I know he does not enter the IF because the equals can not compare the list with a string, but do not know how to make this comparison.
String posicaoSpinner = String.valueOf(sLote.getSelectedItemPosition());
int idLote =0;
int qtdAnimais;
for (int i = 0; i < loteList.size(); i++) {
for (Lote lotes : loteList) {
if (posicaoSpinner.equals(loteList.get(i))) {
idLote = Integer.valueOf(String.valueOf(lotes.get_id()));
qtdAnimais = lotes.getQtd_animais() - itemPovoamento.getAnimais();
lotes.setQtd_animais(qtdAnimais);
}
}
}
Any help is welcome. Thank you so much
If you need the value of the selected item you can simply use .getSelectedItem() of the spinner. So:
String posicaoSpinner = posicaoSpinner.getSelectedItem().toString();
should do the trick?
I posted this question up earlier and it was pretty much a lazy post as I didn't provide the code I had and as a result got negged pretty badly. Thought I'd create a new one of these ....
I have an array dogArray which takes ( name, secondname, dogname )
I want to be able to change the dogname:
here's my attempt :
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
String namgeChange = rp.next(); {
for (int i = 0; i < dogArray.length; i++){
for (int j = 0; j < dogArray[i].length; j++){
if (dogArray[i][j] == name){
dogArray[i][j] = nameChange;
}
}
}
}
For a start it doesn't like the fact I've used ' name ' although it is defined in the dogArray. I was hoping this would read input so that users are able to change 'name' to whatever they input. This will change the value of name in the array.
Do you guys think I'm getting anywhere with my method or is it a pretty stupid way of doing it?
Only one loop is necessary, also move your call to next.
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
for (int i = 0; i < dogArray.length; i++){
String nameChange = rp.next(); {
if(!dogArray[i].name.equals(nameChange)){
dogArray[i].name = nameChange;
}
}
}
You might want to make this function static as well and use accessors and mutators to change and get the name. Might want to tell the user what you want them to type as well. Also there is no point in testing if the name changed, if it changed then set it, if it didnt change then set it (it doesnt hurt). Just get rid of if and keep the code inside.
Similar question to Android: Set a random image using setImageResource, but I think I'm looking for a different answer.
As mentioned in the linked post, this:
int p = R.drawable.photo;
image.setImageResource(p);
displays photo1, but something like this:
String a = "R.drawable.";
String b = "photo";
String c = a+b;
int p = Integer.parseInt(c);
image.setImageResource(p);
will not. I have a lot of images, and an array contining all of the image names, so I want to fill an array something like:
int imageArray[] = new int[number_of_images];
for (int i = 0; i < numImages; i++)
imageArray[i] = ("R.drawable." + image_names[i]);
but I get a runtime error. Is there any way to make this loop work? Or better ways to cue up image ID's into an array?
Thanks!
You can use this method and it will retrun the source id based off a string. The second and third paramaters are optional but if you can put them in I would.
public int getIdentifier (String name, String defType, String defPackage)
Example:
int p = getIdentifier("photo","drawable")
Or if you are getting ids all the time you could create a function like:
public int getDrawableId(Context context, String name){
return context.getResources().getIdentifier(name,"drawable", context.getPackageName());
}
You just have to check to see if it returns 0 because that means it did not find it.
EDIT
For your example up there just impliment the function above and change it to this:
int imageArray[] = new int[number_of_images];
for (int i = 0; i < numImages; i++)
imageArray[i] = getDrawableId(getApplicationContext(),"R.drawable." + image_names[i]);