android ImageView is not bringing to front in RelativeLayout
Tried to call bringtofront() didn't work
tried setting visibility to true didn't work
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spellingLevel1Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/canvas_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/magnetboard" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="480dp"
android:layout_marginTop="600dp"
android:adjustViewBounds="true" />
</RelativeLayout>
</LinearLayout>
Java Code
public class spellingLevel1 extends Activity {
rel1 = (RelativeLayout) findViewById(R.id.canvas_container);
mImageView1 = (ImageView) findViewById(R.id.imageView1);
image1();
}
public void randomNumber() {
Random rando = new Random();
currentLetter = myImageList[rando.nextInt(myImageList.length)];
}
public void image1(){
//randomNumber();
mImageView1 = (ImageView) findViewById(R.id.imageView1);
mImageView1.setBackgroundResource(R.drawable.spelling_);
mImageView1.bringToFront();
mImageView1.requestLayout();
}
UPDATED CODE
rel1 = (RelativeLayout) findViewById(R.id.canvas_container);
mImageView1 = (ImageView) findViewById(R.id.imageView1);
image1();
}
public void randomNumber() {
Random rando = new Random();
currentLetter = myImageList[rando.nextInt(myImageList.length)];
}
public void image1(){
//randomNumber();
mImageView1 = (ImageView) findViewById(R.id.imageView1);
mImageView1.setImageResource(R.drawable.spelling_); //updated here to setImageResource
mImageView1.bringToFront();
Still does not show
its not the full code but it should be everything to help you see what is included in here
You should define the image source of the ImageView.
because it's size is set to wrap_content and it has no content, the size stays 0.
Related
Im trying to implement a ClipDrawable appear by clicking with diferents images
I have this working with a simple image
Code:
MainActivity.java
ImageView imageview = (ImageView) findViewById(R.id.image);
drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawable.setLevel(drawable.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:src="#drawable/clip_source"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip_source.xml
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/tree"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this is working but for my project I need to change the image displayed with a variable to display diferents images depending of the user input
I only find one way, using diferent files for each ClipDraw
MainActivity.java
if(aa == false){
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip1);
}else{
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip2);
}
setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.image);
imageview.setBackgroundDrawable(back);
imageview.setImageDrawable(t);
t.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setLevel(t.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip1.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/farm"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
clip2.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/werehouse"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this solve my problem but have the disadvantage of need one file for each image, and I will need at least 20
I am stuck trying to resize ImageView objects.
The issue is that when I set the width/height programmatically, I see the width/height change with the debugger tool in eclipse.
However, the image will not scale to the new size, and maintains its current ratios. In some cases it even puts the original image at its current size, and moving to a different activity will resize it (somewhat) correctly.
Screenshots:
After opening the app
http://imgur.com/ha9s9rL
After opening and closing a different activity
http://imgur.com/DrBIJaI
I would like for the images to sit next to each other, with the same width/height for each image.
onCreate() Method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_behavior);
listView = (ListView) findViewById(R.id.display_behavior_listview);
List<Behavior> behaviorList = parseCatagories();
List<View> views = getCatagoryViewsFromBehaviorList(behaviorList);
// Setup array adapter
BehaviorRow behaviorRows[] = getBehaviorRows(views);
int viewCount = 0;
View view;
for (BehaviorRow row : behaviorRows) {
if (viewCount < views.size()) {
view = views.get(viewCount);
row.setBehaviorOne(view);
viewCount++;
view = views.get(viewCount);
row.setBehaviorTwo(view);
viewCount++;
}
Log.i("BehaviorRowDat", row.toString());
}
BehaviorRowAdapter adapter = new BehaviorRowAdapter(getApplicationContext(), R.layout.catagory_row,
behaviorRows);
listView.setAdapter(adapter);
}
onWindowFocusChanged() Method:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView catagoryOne;
ImageView catagoryTwo;
for (int i = 0; i < listView.getChildCount(); i++) {
catagoryOne = (ImageView) listView.getChildAt(i).findViewById(R.id.catagory_space_one);
catagoryTwo = (ImageView) listView.getChildAt(i).findViewById(R.id.catagory_space_two);
resizeImageView(catagoryOne);
resizeImageView(catagoryTwo);
}
}
Resize Image Code:
private ImageView resizeImageView(ImageView image) {
// Set length/width
int length = calculateLengthOfImages();
image.getLayoutParams().height = length;
image.getLayoutParams().width = length;
// Set Padding
int padding = calculatePaddingOfImages();
image.setPadding(padding, padding, padding, padding);
return image;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catagory_row_created"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1" >
<ImageView
android:id="#+id/catagory_space_one"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/catagory_placeholder"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/catagory_space_two"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/catagory_placeholder"
android:scaleType="centerCrop" />
</LinearLayout>
Try changing the scaleType attribute in your ImageViews to:
android:scaleType="fitXY"
Alternatively, you can add this line in your resizeImageView() method:
image.setScaleType(ScaleType.FIT_XY);
Hope that helps!
Replace your xml with this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/catagory_row_created"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/catagory_space_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/catagory_space_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:scaleType="centerCrop" />
</LinearLayout>
In onCreate() of your activity, add this:
final ImageView imageView1 = (ImageView) findViewById(R.id.catagory_space_one);
final ImageView imageView2 = (ImageView) findViewById(R.id.catagory_space_two);
imageView1.post(new Runnable() {
#Override
public void run() {
imageView1.getLayoutParams().height = imageView1.getWidth();
imageView1.requestLayout();
}
});
imageView2.post(new Runnable() {
#Override
public void run() {
imageView2.getLayoutParams().height = imageView2.getWidth();
imageView2.requestLayout();
}
});
So what basically I did is I have stretched the two images' width equally to take the whole space in xml using layout_weight, then I have set the images' height to be like their respective widths in code. So now they are square-shaped.
Hope that helps.
I just want to click to button and to change the Background image. here is my different codes
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="image">
<item>#drawable/photo</item>
<item>#drawable/photo1</item>
<item>#drawable/photo2</item>
<item>#drawable/photo3</item>
<item>#drawable/photo4</item>
</string-array>
</resources>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_above="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/photo"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>
and here is my activity code:
final LinearLayout background = (LinearLayout) findViewById(R.id.back);
Button button = (Button) findViewById(R.id.button1);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.image);
final Random random = new Random();
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
int randomInt = random.nextInt(myImages.length());
background.setBackgroundResource(myImages.getResourceId(randomInt,-1));
int drawableID = myImages.getResourceId(randomInt,-1);
background.setBackgroundResource(drawableID);
}
});
it doesn't work , can you help me
Thanks in advance
In xml, you are using RelativeLayout, but in activity code, you are LinearLayout.
You are casting wrong layout in your code. You have taken the RelativeLayout in your xml file where as you are casting it into LinearLayout in your java file.
In your java file change the LinearLayout to RelativeLayout.
final LinearLayout background = (LinearLayout) findViewById(R.id.back);
write this:
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
If the case is not the LinearLayout do this:
Change the definition of your array to
final String[] myImages = res.getStringArray(R.array.image);
And then, replace your onClick method for this:
public void onClick(View v) {
int randomInt = random.nextInt(myImages.length);
int drawableID = myImages.getResources().getIdentifier(myImages[randomInt], "drawable", this.getPackageName());
background.setBackgroundResource(drawableID);
}
I need to add a textView and ImageView at the bottom of the screen
I need to hide the textView and show it again programmatically
here I have this image to explain the Question and how the application should behave
the left rectangle desplays the TextView shown and the second for Hiding Textview:
I tried the code below to do that:
1- I added click listener to ImageView that hide the TextView and Change the LayoutParams of ImageView
2- On Clicking it again it will show the TextView and align the ImageView above it
but the problem is that when I run the code the ImageView is stuck to
the top of the screen
I'm trying to change the LayoutParams of a view pragmatically using this code :
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#id/tv1" />
</RelativeLayout>
and the class MainActivity.java :
public class MainActivity extends Activity {
TextView tv;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.img);
tv = (TextView) findViewById(R.id.tv1);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
if(tv.getVisibility() == MyNavigationBar.GONE){
tv.setVisibility(MyNavigationBar.VISIBLE);
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
image.setLayoutParams(imageParams);
} else {
tv.setVisibility(MyNavigationBar.GONE);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
image.setLayoutParams(imageParams);
}
}
});
}
}
I think the problem is that the function :
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
but its not work
I also tried to set an Id manually to the textView like:
tv.setId(1);
and changing the previous to :
imageParams.addRule(RelativeLayout.ABOVE, tv1.getId());
Also doesn't work for me !!
I wonder that whether the problem in my code or in the android SDK !!
Seems layout_alignWithParentIfMissing would be much easier:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#+id/tv1"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
And only switch visibility of tv1 in your code:
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(tv.getVisibility() == View.GONE){
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
})
I create an ImageSwitcher by code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageSwitcher
android:id="#+id/ImageSwitcher01"
android:background="#android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageSwitcher>
</LinearLayout>
But when I run this, this ImageSwitcher background still BLACK, not transprent as I expected. How do I fix it?
When you go to set the factory on the ImageSwitcher, be sure that the ViewFactory returns a transparent background:
myImageSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundColor(0x00000000); // <--- return transparent bg
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams( ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
return imageView;
}
});
That fixed it for me.
As a workaround to this problem, I used the ViewFlipper instead of the ImageSwitcher.
I just want to animate between predefined images, so ViewFlipper is ok for me.
I haven't tried but I think you can use 2 imageviews, set the correct image on the next view and call viewflipper.showNext().
Edit: In the end I had to do what I wrote above in the project I was working on, and it works.
In the layout.xml you have:
<ViewFlipper
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/viewFlipper">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/market_product01"
android:id="#+id/imgFirst">
</ImageView>
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/imgSecond">
</ImageView>
</ViewFlipper>
Then you create a class that handles the image switch:
public class MyImageViewSwitcher {
private ViewFlipper flipper;
private ImageView[] views;
private int currentView = 0;
public MyImageViewSwitcher(ViewFlipper flipper, ImageView first, ImageView second) {
currentView = 0;
this.flipper = flipper;
views = new ImageView[2];
views[0] = first;
views[1] = second;
}
public void setImage(Drawable image) {
currentView = (currentView + 1) % 2;
views[currentView].setImageDrawable(image);
flipper.showNext();
}
}
From your activity, you build an ImageViewSwitcher:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
viewFlipper.setInAnimation(android.R.anim.fadeIn);
viewFlipper.setOutAnimation(android.R.anim.fadeOut);
ImageView imgFirst = (ImageView) findViewById(R.id.imgFirst);
ImageView imgSecond = (ImageView) findViewById(R.id.imgSecond);
this.imageSwitcher = new MyImageViewSwitcher(viewFlipper, imgFirst, imgSecond);
Every time you want to change the content you call the method:
imageSwitcher.setImage(Drawable d)
Setting the background color of the ImageSwitcher control does not work. To remove the black background, you just need to set the background color each views inside the Imageswitcher to transparent:
_imageSwitcher.getCurrentView().setBackgroundColor(getResources().getColor(R.color.transparent));
Do this for each call to _imageSwitcher.setImageResource() and your are done :)