I have set up an ImageView in main.xml, if I want to access it from my View class and make it visible = false, how can I do this programatically?
Thank you
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
theView = new GameView(this);
theView.setBackgroundResource(R.layout.main);
setContentView(theView);
For example, in your layout xml file, there is a imageview1
<ImageView
android:id="#+id/imageview1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
in your java src,
ImageView img=(ImageView)findViewById(R.id.imageview1);
img.setVisibility(View.GONE);
img.setVisibility(View.VISIBLE);
img.setVisibility(View.INVISIBLE);
you can google the difference between View.Gone and View.VISIBLE
First you should retrieve a link to this view object. Than set visibility property of this object to View.INVISIBLE
ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setVisibility(View.INVISIBLE);
Use this property in the xml of that Imageview
android:visibility="visible"
and change the visibility programatically like this on some particular event:
image.setVisibility(ImageView.GONE)
where image is the instance of that imageview received through findViewById()
ImageView myView = (ImageView) findViewById(R.id.myView);
myView.setVisibility (android.View.INVISIBLE);
http://developer.android.com/reference/android/view/View.html#INVISIBLE
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
Suppose you have a ImageView called imageView.
Now access the imageView like
imageView=(ImageView) findViewById(R.id.your_image_view);
Now when you are trying to hide the imageView just use imageView.setVisibility(View.INVISIBLE);
Hope this will help you
Related
I'm trying to do simple interacting interface, I linked the following method with the button by android: onClick so I should when I click the button it should display the alternative image and Text, but when I click on the button nothing happens, what is the missed part here
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView eated = new ImageView(this);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedtext = new TextView(this);
eatedtext.setText("i'm so full");
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
// TODO: Find a reference to the TextView in the layout. Change the text.
}
First problem:
Suppose, you created TextView and ImageView:
ImageView eated = new ImageView(this);
TextView eatedtext = new TextView(this);
In your Activity, I'm sure that you use or xml layout, or custom ViewGroup, so you missed the code, where you adding eated and eatedtext to your layout by calling addView method.
Second problem:
If we suppose that you really created views and added them, then you still have a problem, because you didn't specify layout params for eated and eatedtext by calling setLayoutParams method.
Solution
I think you need to write something like:
ImageView eated = (ImageView) findViewById(R.id.eated_image);
eated.setVisibility(View.VISIBLE);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedText = (TextView) findViewById(R.id.status_text_view);
eatedText.setVisibility(View.VISIBLE);
eatedText.setText("i'm so full");
And in your xml you need to set for those views attribute android:visibility="gone" or android:visibility="invisible"
I suppose, you have defined the TextView in the layout-XML. If so, you don't have to create a new TextView object but instead alter the existing.
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
eatedtext.setText("i'm so full");
Try this one
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
ImageView eated = new ImageView(this);
TextView eatedtext = new TextView(this);
eated.setImageResource(R.drawable.after_cookie);
eatedtext.setText("i'm so full");
linearLayout.addView(eated);
linearLayout.addView(eatedtext);
// TODO: Find a reference to the TextView in the layout. Change the text.
}
Please read the comments
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView eated = (ImageView)findViewById(R.id.android_cookie_image_view);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
eatedtext.setText("i'm so full");
}
Update your XML File android:layout_height="0dp" to android:layout_height="wrap_content"
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
thanks for helping out folks.
Here is the right answer, as you mentioned you don't need to identify the identified object in the xml code file like ImageView and TextView so the right code will be
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView image = (ImageView)findViewById(R.id.android_cookie_image_view);
image.setImageResource(R.drawable.after_cookie);
TextView text = (TextView) findViewById(R.id.status_text_view);
text.setText("i'm so full");
// TODO: Find a reference to the TextView in the layout. Change the text.
}
I am trying to get a Imageview from textview. I tried many methods but not able to set that textview on Imageview as image.
I tried following method:
TextView tv = (TextView)findViewById(R.id.textview);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageview);
img.setImageBitmap(tv.getDrawingCache());
but i am not still able to show that textview on Imageview. Can someone help me that how can i set textview as image on that imageview?
Try this, if it maya help you
yourTextView.setDrawingCacheEnabled(true);
yourTextView.buildDrawingCache();
yourImageview.setImageBitmap(tv1.getDrawingCache());
call
tv.buildDrawingCache(true);
Bitmap b = tv.getDrawingCache(true).copy(Bitmap.Config.ARGB_8888, false);
tv.destroyDrawingCache();
img.setImageBitmap(b);
I want to show a image in a activity . I successfully made it with imageview.But it has no zoom effect ie. I can't zoom the pic. now how can I add this zoom efect.
my .xml code is :
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/cat" />
use the lib https://github.com/chrisbanes/PhotoView
In code
ImageView mImageView;
PhotoViewAttacher mAttacher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Any implementation of ImageView can be used!
mImageView = (ImageView) findViewById(R.id.iv_photo);
// Set the Drawable displayed
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
mImageView.setImageDrawable(bitmap);
// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
mAttacher = new PhotoViewAttacher(mImageView);
}
// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
attacher.update();
Let's say I have 4 images:
ImageView a = (ImageView) findViewById(R.id.a);
ImageView b = (ImageView) findViewById(R.id.b);
ImageView c = (ImageView) findViewById(R.id.c);
ImageView d = (ImageView) findViewById(R.id.d);
And then I set the visibility of those images by this:
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.INVISIBLE);
c.setVisibility(View.INVISIBLE);
d.setVisibility(View.INVISIBLE);
Is there anyway I can group those 4 images and then set the visibility to that group of images?
Something like this:
images[] = {a,b,c,d};
images.setVisibility(View.INVISIBLE);
Thank you.
If you don't want to use a ViewGroup your basic idea is good. I use it often. Just do
ImageView images[] = {a,b,c,d};
for (ImageView view : images) {
view.setVisibility(View.INVISIBLE);
}
The usual approach would be to have all the views in the same ViewGroup i.e. a layout and then set the visibility of the group in order to apply it to all children. But this really depends on how the views are laid out in the first place.
You can put all those images in layout and then control visibility of that layout.
I have a imageButton in android which user clicks. Then after his click I want t0 show a tick or a cross image on the toip of it.
How is it possible?
<ImageButton android:layout_width="wrap_content"
android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:background="#drawable/rhino" android:layout_marginRight="20dp"></ImageButton>
put your ImageButton and the tick image in a FrameLayout and make the visbility of the Tick image "Invisible" . So when you click on the ImageButton then change the state of Tick Image to Visible.
Get a reference to your ImageButton and then use one of its setImage methods, such as setImageResource.
You can achieve the same using ImageView. Use ImageView
testimage = (ImageView) findViewById(R.id.imageview);
testimage.setOnClickListener(listener);
write the logic to set both type of image to imageview in onclick event
public OnClickListener listener=new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("..set image button..");
Drawable[] layers = new Drawable[2];
layers[0] = getResources().getDrawable(R.drawable.btn_call);
layers[1] = getResources().getDrawable(R.drawable.blue_unfocus);
System.out.println(layers[1]+"...Drawable..."+layers[0]);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
}
};
Thanks
Deepak