Android Set ImageView in front of Relative Layout - android

I have a RelativeLayout in my Android App. Now I want to show an ImageView in front of that Layout. The problem is that the ImageView is not in the front, it's a bit transparent and I can see things like EditText and Button. I can't change the Layout (setContentView), because the Layout is created dynamically and after setContentView, the Controls are away.

You can add view programmatically, and in the way that it will be on the top!
create id for you top level layout
Now some code (in my case it's relative layout):
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ImageView imageView = new ImageView(context)
Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image);
imageView.setLayoutParams(getLayoutParams());
relativeLayout.addView(imageView);
imageView.bringToFront();
//here just example layout params, use yours params ;-)
private RelativeLayout.LayoutParams getLayoutParams() {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
return layoutParams;
}

You can bring it to the front once you insert it.
imageView.bringToFront();
If the image is transparent, you can set a white background to prevent things below it from showing.
imageView.setBackgroundColor(0xFFFFFF);

Related

Add ImageView to RelativeLayout in Android

So I have created an ImageView programmatically in my Activity.
I want to add this ImageView at the bottom of one of my RelativeLayout.
This is what I tried:
// creating new ImageView
ImageView shadowView = new ImageView (contextPara);
shadowView.SetBackgroundResource (Resource.Drawable.bottomBar_Shadow);
shadowView.SetScaleType (ImageView.ScaleType.FitXy);
// creating ImageView LayoutParams
WindowManagerLayoutParams imgViewLayoutParams = new WindowManagerLayoutParams ();
imgViewLayoutParams.Width = ViewGroup.LayoutParams.MatchParent;
imgViewLayoutParams.Height = (int)imgViewHeight;
imgViewLayoutParams.Gravity = GravityFlags.Bottom;
// Adding ImageView to RelativeLayout
listViewRelativeLayout.AddView (shadowView, imgViewLayoutParams);
My problem is this adds the ImageView to the TOP of the RelativeLayout, NOT the bottom.
I also tried the following:
After I call the AddView and add the ImageView, I set the Y position of the ImageView.
This actually works. It moves the ImageView down but I'm not sure how much to move down. RelativeLayout size is not absolute.
// this moves the ImageView down 100px.
shadowView.SetY (100f);
How can I put it to the bottom of the RelativeLayout?
Please note: I CAN'T just do the math (RelativeLayout height - ImageView Height) because RelativeLayout height is always 0 until OnCreate finishes.
What are my other choices?
Thank you for your time.
Possible Solutions:
1.
Consider placing a single ImageView within the RelativeLayout XML.
You can access this ImageView inside the Activity/Fragment using
ImageView mImageView = (ImageView) view.findViewById(R.id.imageView);
You can set the starting visibility as gone mImageView.setVisibility(View.GONE), and show the ImageView at a later time mImageView.setVisibility(View.VISIBLE);
2.
You can add a sub-layout (LinearLayout) to the existing RelativeLayout, this sub-layout can be placed at the bottom of the RelativeLayout via XML.
Inside the Activity/Fragment, you can target that sub-layout (linearLayout) by its ID, and than add the ImageView programmatically to that layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.modup.fragment.FeedFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout>
android:id="#+id/linearLayout"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
</RelativeLayout>
I found a way!
Instead of using WindowManagerLayoutParams to set LayoutParameters, using RelativeLayout.LayoutParams does the job.
Here's how:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, (int)imgViewHeight);
layoutParams.AddRule (LayoutRules.AlignParentBottom);
AlignParentBottom does the job!

add drawable in relative layout programmatically

I have Image in my layout and I want when user clicked on image, 4 drawable (corner button) added in corner of the image. How can I do this?
Add id field to the relative layout in the xml. Use that id in the class to create view dynamically.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.YourID);
imageView.setImageDrawable(R.drawable.image);
//Add View to Layout:
relativeLayout.addView(imageView);
well there are many ways to that.
Using layoutparams you can set layout params programatically.
like
LayoutParams param = new LayoutParam(LayoutPara.width, LayoutParam.height);
//your rules...
imageView.setlayoutParams(param);
here you can set margintop, bottom, left and right and alignParentLeft.... so on

Is it possible to use a View and Activity together on a single page on android?

I have to design a page where, the top portion of the page should have buttons,textview etc.. and the the second portion of the page to have a View to make some animated ball moving around on button clicks of upper part..
Is it relative layout i should use ? or surface view ?
i am not sure how to proceed.. can someone help on this..?
Your best bet is to define the layout with two child views, one which is a self-implemented SurfaceView class, the other is a RelativeLayout or LinearLayout, whichever suits your needs better. Check out this small example below on how to create a layout which has a RelativeLayout attached to the bottom and a SurfaceView on the top.
public void setupContentView() {
RelativeLayout layout = new RelativeLayout(this);
MySurfaceView mySurfaceView = new MySurfaceView(this);
RelativeLayout xmlLayout = (Relativelayout) View.inflate(this,
R.layout.my_xml_file, null);
int xmlId = 0x1234;
xmlLayout.setId(xmlId);
RelativeLayout.LayoutParams xmlParams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
xmlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
xmlParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams surfaceViewParams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
surfaceViewParams.addRule(RelativeLayout.BELOW, xmlId);
surfaceViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(xmlLayout, xmlParams);
layout.addView(mySurfaceView, surfaceViewParams);
setContentView(layout);
}
This creates a layout inflated from an xml file called my_xml_file and names is xmlLayout. It also creates a self-implemented SurfaceView class called MySurfaceView which it calls mySurfaceView. Then, it sets the paramaters using the RelativeLayout.LayoutParams class to both layouts. It sets the mySurfaceView below the xmlLayout and both heights are set to WRAP_CONTENT, which would make the mySurfaceView take up the entire screen if the xml height = 0 and vise versa. You would then draw however you would in the MySurfaceView class however you would normally, and the xml file can be set up however you'd like.Best of luck! Would love to hear back on any success.

How to overlap views in RelativeLayout, dynamically?

I've an ImageView inserted in the RelativeLayout. On top of this ImageView I'm trying to insert a progressbar, which would go invisible after the image is downloaded. But, when I add progressbar after adding the ImageView, it gives me an error -
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here is the code:
mRelativeLayout = (RelativeLayout) mGallery.findViewById(R.id.relative_progress_spin_layout);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.progress_spin);
progressBar = (ProgressBar) mGallery.findViewById(R.id.progress_spin);
image = new ImageView(GalleryModuleActivity.this);
image.setPadding(4, 4, 4, 4);
image.setScaleType(ImageView.ScaleType.FIT_XY);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
mRelativeLayout.addView(image);
mRelativeLayout.addView(progressBar);
mHorizontalLayout.addView(mRelativeLayout);
Thanks..
You already have the ProgressBar in the layout(you search for it with findViewById) so you shouldn't add it again to the layout(the same thing with the mRelativeLayout RelativeLayout if it is already in the layout file). Remove this lines:
mRelativeLayout.addView(progressBar);
mHorizontalLayout.addView(mRelativeLayout);
If you have the views in the layout you don't add them again to the layout!
What you exactly want to do with images and progress bar. If you want to display ProgressBar on images use FrameLayout. In that also you can use VISIBLE and GONE stuff.
Where you want to display that dynamic generated views ?
Because we do have adapter to display same type of data with different content.

Android FrameLayout and TextView - why does LayoutParams work where setGravity() does not?

I'm using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job... but no, it seems to have no effect whatsoever on where the text goes (and other objects like ImageView don't even have this method).
So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)
Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout... say, without creating a new LayoutParams object as I'm doing now?
Thanks! For reference, below is a (working) code snippet showing how I'm setting up this FrameLayout and TextView.
FrameLayout fl1 = new FrameLayout(this);
FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
fl1.setId(9001);
fl1.setLayoutParams(flp1);
.....
tv1 = new TextView(this);
FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
tv1.setId(9006);
tv1.setLayoutParams(tvp1); // This works
tv1.setBackgroundColor(Color.GRAY);
tv1.setTextColor(Color.BLACK);
tv1.setText("Dynamic layouts ftw!");
tv1.setGravity(Gravity.CENTER); // This does NOT work
.....
fl1.addView(tv1);
1) view.setGravity means hows the view should positions if children.
In the case of the textview it refers to the positioning of the text.
When in linearlayouts or viewgroups it refers to its child views.
2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.
Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.
Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.
If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.
This should give what your are trying to do
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
FrameLayout fr = new FrameLayout(this);
fr.setLayoutParams(lp);
fr.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(lp2);
l.setGravity(Gravity.CENTER);
l.setBackgroundColor(Color.BLUE);
ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(vp);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
l.addView(t);
fr.addView(l);
tv1.setGravity(Gravity.CENTER);
belongs to the gravity of the TEXT inside the TextView.
As documentation states:
Sets the horizontal alignment of the text and the vertical gravity
that will be used when there is extra space in the TextView beyond
what is required for the text itself.

Categories

Resources