While writing Robotium testcase, I want to differentiate between two images(drawable) placed on same imageView. Drawables are placed dynamically. I have tried to get drawable with the help of getDrawable() but every time different drawable object is coming.
Is there any way to get the drawable id? Any help or guidance will be well appreciated.
You can't get the drawable id from the drawable itself, but you can store and retrieve the drawable id of the imageView using the setTag() and getTag() methods.
TestActivity.java
public class TestActivity extends Activity {
private static String TAG = "TestActivity";
private Activity mActivity;
private static int ID_TAG = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.d(TAG, "onCreate");
mActivity = this;
// set drawable
ImageView imgView = new ImageView(mActivity);
imageView.setImageResource(R.drawable.photo);
String value = String.format("%d", R.drawable.photo);
imageView.setTag(ID_TAG, value);
// get drawable
value = imageView.getTag(ID_TAG);
drawableId = Integer.parseInt(value);
if(drawableId == R.drawable.photo){
Log.d(TAG, "You found the photo");
}
}
}
Related
I wanted to make it with TransitionDrawable class, but it needs a separate file transition.xml. There I define which image I change to.
I need to define them in Java code because I don't know which images I will change too. I have many images and I accidentally get only two images which will change between each other. What can I do? Perhaps I need another class.
Code with transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
#Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
You can programmatically create a TransitionDrawable using the class' constructor. You don't need to acquire it from XML. This gives you the flexibility of dynamically assigning the Drawables it transitions between.
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);
I have created in my layout two ImageViews, let's call them imageviewTop and imageviewBottom.
I saved two images into the drawable (green_image.png and red_image.png).
I also added a button and want I would like to do is, when the button is clicked, one of the ImageViews will get selected randomly and from the green_image it will change to the red_image.
I already tried with creating a switch/case statement and generating a random number, like 1 or 2.
Based on this number the case statement would update either the top or bottom image.
This is working fine for 2 ImageViews, but in case I would have 100, I would need to create 100 cases in code.
I am searching for a more dynamic option.
I know how to update the image for the ImageView, I am struggling with the part, on how to select one ImageView randomly, if it is possible.
Here is the code:
public class MainActivity extends Activity {
ImageView imagevieTop, imageviewBottom;
Button randomButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagevieTop = (ImageView) findViewById(R.id.imageViewTop);
imageviewBottom = (ImageView) findViewById(R.id.imageViewBottom);
randomButton = (Button) findViewById(R.id.buttonRandom);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// randomly select one of the two imageviews
// for example: randomly selected imageviewTop
// set imageresource red_image to imageviewTop
//at next start up it would select either top or bottom, 50%-50% and then assign the image to it
}
});
}
}
You could just use one ImageView, and randomize the picture you draw.
Alternatively, you could adjust this to use an array of ImageViews. Your choice.
The line you want, though, is int index = random.nextInt(imgs.length); to get a random index from the list.
public class MainActivity extends Activity {
int[] imgs = new int[] { R.drawable.green_image, R.drawable.red_image };
Button randomButton;
private final Random random = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imgView = (ImageView) findViewById(R.id.imageView);
randomButton = (Button) findViewById(R.id.buttonRandom);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int index = random.nextInt(imgs.length);
// randomly select one of the two drawables
int resId = imgs[index];
// set imageresource imgView
Drawable d = getResources().getDrawable(resId);
imgView.setImageDrawable(d);
}
});
}
}
To handle the "100 ImageViews" problem, I'd recommend not copying 100 lines of code, and instead looping over reasonable ID values.
List<ImageView> imgViews = new ArrayList<ImageView>();
for (int i = 0; i < 100; i++) {
int resId = getResources().getIdentifier("imgView" + i, "id", getPackageName());
ImageView nextImg = (ImageView) findViewById(resId);
imgViews.add(nextImg);
}
In case you want to do a dynamic selection of "n" ImageView elements, then you'll need to store them in a data structure (e.g. an array, a list, etc.). For example this code will select a random ImageView from a list:
public ImageView getRandomImageView(final List<ImageView> imageViewList) {
final Random random = new Random();
//The "nextInt" method works in the half-open range [0, n), so it'll never be equal to the list size.
final int randomElement = random.nextInt(imageViewList.size());
return imageViewList.get(randomElement);
}
In your case, with two ImageView's ("imagevieTop" and "imageviewBottom") declared in fixed variables then you would need to pass them to a list or something similar in order to select one of them dynamically.
I created an ImageView and in my activity every few seconds i change the picture to a different one. I also have a button than when press, is supposed to get me the resource id of the picture currently being displayed, the problem is that everytime i do:
ImageView view = (ImageView) findViewById(R.id.imageView1);
It always gives me the resourceId defined in the xml, not the one i changed dynamically, how can i achieve this? here's the code in my activity class:
public void startImages(View v) {
...
delayedImage(0);
}
private void delayedImage(final int index) {
//stop at the 5th picture
if (index > 5) return;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
String drawableId = "picture" +index;
int resourceId = getResources().getIdentifier(drawableId, "drawable", getPackageName());
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageResource(resourceId);
delayedImage(index+1);
}
}, 500);
}
So every 5 second a new image is set, when i click on the button Share I want to retrieve whatever resourceId is currently in the ImageView set in the method above.
//send image to send text
public void shareImage(View v){
ImageView view = (ImageView) findViewById(R.id.imageView1);
??????
}
I checked the ImageView object while debugging and I saw someone get the int by doing this:
Field f = ImageView.class.getDeclaredField("mResource");
f.setAccessible(true);
Object out = f.get(view);
But i'm unable to get the actual R drawable name from this which I need when i'm trying to retrieve it from
String path = "android.resource://your.package.name/" +getPackageName() +"/" +???;
.. can anyone please help?
I have been able to pass data to other activities except this one. Can anyone see what I'm doing wrong. The only error i'm getting is that my TextView showmsg is NOT showing up in the new activity. Does anyone know why?
public class MyScanActivity extends Activity
{
private static final String MY_CARDIO_APP_TOKEN = "NOT THE PROBLEM";
final String TAG = getClass().getName();
private Button scanButton;
private TextView resultTextView;
private Button buttonBack;
private TextView showmsg;
private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.myscan);
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
resultTextView = (TextView)findViewById(R.id.resultTextView);
scanButton = (Button)findViewById(R.id.scanButton);
buttonBack = (Button)findViewById(R.id.buttonBack);
showmsg = (TextView) findViewById(R.id.showmsg);
There are not too many options for how your text can be not shown.
You have the view itself messed up: Check this by putting some sample text in the XML file using android:text="TEST" in the TextView showmsg. Your text should appear, unless your text is the wrong color or size, or something else happens to be above it.
You aren't actually finding it with findViewById() (I hope you've double checked that in a debugger) I agree with alex that you might not want R.id.showmsg. Did you mean to put R.id.resultTextView there instead?
Your passed text is not actually coming through. You should do a log statement, like Log.v(TAG, "Passed text is " + in.getCharSequenceExtra("urs")); and make sure the text is actually coming through.
I haven't test it. but i think that is the reason.
change here:
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
with this:
showmsg = (TextView) findViewById(R.id.showmsg);
if (in.getCharSequenceExtra("usr") != null) {
showmsg.setText(in.getCharSequenceExtra("usr"));
}
Here's the thing:
I have two classes: Main and control.java
the Main class is an Activity class where I build my app and the control class I just use for variables controls that I must access from other classes.
The problem is: In class Main I got 2 methods, and I have an ImageView in each of them, I need to set the image view resource of the second method on a click listener of the first one. Like this:
public void first() {
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
//And then, I want something like this: first.setBackgroundResource(first);
}
Thanks guys!
Why don't you just do something like:
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
Otherwise, you'd have to use a private/public class variable and define it outside the method.
I admit to being a bit confused by your question but I think below is at least the start of what you're looking for
#Override
protected void onCreate(Bundle savedInstanceState) {
control.setImageView((ImageView) findViewById(R.id.myview));
second = (ImageView) findViewById(R.id.myview2);
}
public void first() {
control.getImageView().setBackgroundResource(R.drawable.myimage);
}
public void second() {
second.setBackgroundDrawable(control.getImageView().getDrawable());
}
ImageView second;