programatically add overlay icon in an activity - android

I've been searching around in Google for a bit but I can't seem to find what I want to do. I want to be able to programatically add an icon as an overlay in an activity at a specified position without using any xml.
An example of what I mean: http://cdn9.staztic.com/app/a/2326/2326236/pollfish-demo-2-1-s-307x512.jpg
Any ideas?

It depends at layout you are using. If you are using RelativeLayout, you can do it this way:
<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"
android:id="#+id/main" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#android:drawable/ic_"/>
</RelativeLayout>
Which is equal with this Java code (except root RelativeLayout):
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);
ImageView child = new ImageView(this);
child.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.setLayoutParams(params);
layout.addView(child);

Related

Layout below layout programatically inside Relative View

I'm trying to programatically add two views as children of a root RelativeLayout, when one view is below another.
Here's the root view (which also resides in another CoordinatorLayout, but I don't think it's related):
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
Now, here is one of the two layouts I'm trying to add programatically:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
and the other one:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_group_members"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/icon"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/members_title"/>
</RelativeLayout>
I added this with this code:
RelativeLayout container = (RelativeLayout)findViewById(R.id.container);
container.addView(topView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, bottomView.getId());
bottomView.setLayoutParams(lp);
container.addView(bottomView);
The result is: the bottom view is not visible.
What I tried:
Changing the first RecyclerView's height to WRAP_CONTENT (thought it might fill all space and hide the bottom layout), which had no effect.
Instead of setting the LayoutParams to the bottom view, to:
container.addView(bottomView, lp);
But it didn't work either.
Using a LinearLayout instead of the RelativeLayout container, same behaviour either.
I have no more ideas what can cause this problem, and by looking at similar questions, nothing worked. Any ideas what am I doing wrong?
Your first view is RecyclerView, which is a scrollable view. It doesn't matter if you set the height wrap_content to RecyclerView/ListView. In all cases, it's going to fill the whole screen unless you set a specific height to the RecyclerView, obviously smaller than the device's height. The second view below RecyclerView would show up then. Here's what you can try:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, HEIGHT_OF_VIEW);
recyclerView.setLayoutParams(params);
The LinearLayout approach should work.
Just mind that:
1) Container LinearLayout should have layout_height MATCH_PARENT
2) RecyclerView should have layout_height of 0 and layout_weight of 1
If instead you want to keep the RelativeLayout approach try:
1) BottomLayout with rule RelativeLayout.ALIGN_PARENT_BOTTOM
2) RecyclerView with rule RelativeLayout.ABOVE
Example 2nd approach:
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
RelativeLayout bottomLayout = new RelativeLayout(this);
bottomLayout.setId(R.id.bottom_id);
RelativeLayout.LayoutParams bottomLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
bottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomLayout.setLayoutParams(bottomLayoutParams);
bottomLayout.setBackgroundColor(Color.RED);
TextView bottomTextView = new TextView(this);
bottomTextView.setText("Bottom Layout");
bottomLayout.addView(bottomTextView);
containerLayout.addView(bottomLayout);
RecyclerView recyclerView = new RecyclerView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ABOVE, R.id.bottom_id);
recyclerView.setLayoutParams(params);
recyclerView.setBackgroundColor(Color.BLUE);
containerLayout.addView(recyclerView);

ImageView added in RelativeView gets scaled down

I have added two imageviews in a relative layout and get a strange behavior. The last image scales one of the last image down to 50% when using RelativeLayout.BELOW and programaticly adding the image. One of my images is displayed below the other one. I use this code:
RelativeLayout layout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setId(10001);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
_imageView = new ImageView(getActivity());
_imageView.setId(10002);
_imageView.setImageResource(_resource);
layout.addView(_imageView, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, _imageView.getId());
params.addRule(RelativeLayout.ALIGN_LEFT, _imageView.getId());
ImageView shadowImage = new ImageView(getActivity());
shadowImage.setId(10003);
shadowImage.setImageResource(R.drawable.card_shadow);
shadowImage.setAdjustViewBounds(true);
layout.addView(shadowImage, params);
But when I use xml instead of programaticly adding the image I get the image in full size:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/cardShadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cardView"
android:layout_alignLeft="#drawable/card_01"
android:src="#drawable/card_shadow"
android:contentDescription=""/>
<ImageView
android:id="#+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/card_01" />
</RelativeLayout>
If you want to overlap your images then you should use FrameLayout
You're using WRAP_CONTENT in Java code for both the Relative Layout params but MATCH_PARENT for it in the XML code. Guess that's the catch. :)
Just added:
shadowImage.setScaleType(ScaleType.CENTER);
Which allows no scaling of the ImageView. Thanks by the way for some good insights and tips.

MATCH_PARENT beside WRAP_CONTENT

On a Dialog I want a MATCH_PARENT EditText left of a ImageButton with WRAP_CONTENT.
This would look like the picture below.
Layout picture http://goo.gl/dxuS5
I dont know how to solve this problem!
It's must programming only in java! I have no chance to write this in XML.
I have just use with LinearLayout. But the MATCH_PARENT swaps the ImageButton with WRAP_CONTENT.
Also I use RelativeLayout but this doesn't looks like so I want.
try this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Assuming you are putting these into a horizontally oriented LinearLayout:
EditText temp = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
someLinearLayout.addView(temp);
someLinearLayout.addView(imageView);

dynamically adding two views one below other

I want to add two views one after the other, I used this way but I am getting an error.
This is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel1"
android:layout_alignParentTop="true"
></RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel2"
android:layout_below="#+id/rel1"
></RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In the two Relative layouts rel1,and rel2 I will add my custom views which are going to be drawn dynamically.
my code:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
RelativeLayout.LayoutParams para2 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);
Here mView and mView2 are two view type which I want to set in the two relative layouts.
ViewWidth and ViewHeight are the width and height of the screen it is running.
The problem:
If only one view is added i.e. mView or mView2 it's showing but if both the views are added(like in above) then only one relative layout is shown.
I want that both of my views get set one below other.
Hope I am clear in my question.Can you please tell me the appropriate way to do this.
you are setting layout params to views dynamically in activity and these are new layout params objects, so your rule to add rel2 to below of rel1 gets cleared, instead try:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = rlStat1.getLayoutParams();
para1.width=LayoutParams.FILL_PARENT;
para1.height=LayoutParams.FILL_PARENT;
RelativeLayout.LayoutParams para2 = rlStat2.getLayoutParams();
para2.width=LayoutParams.FILL_PARENT;
para2.height=LayoutParams.FILL_PARENT;
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);

Xml to equivalent dynamic xml set from class

This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<Button
android:id="#+id/button_weather9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Πίσω" />
<ImageView
android:id="#+id/weather_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:src="#drawable/weather1" />
</LinearLayout>
But in my application I want inside my class, to set a new xml dynamically with the same things as above. I do no know what exactly will be the parameters. So far I have written this. Can you help me with it and complete what is missing:
final LinearLayout test2 = new LinearLayout(this);
test2.setOrientation(LinearLayout.VERTICAL);
test2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
final Button Back = new Button(this);
Back.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Back.setText("Back");
test2.addView(Back);
final LoaderImageView image = new LoaderImageView(this, ImageUrl);
image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I am more interested in ScaleType because the image.ScaleType(FILL) doesn't work.
Can you clarify what do you want to achieve. A java code that construct exactly the same layout as the XML? If so why? Or, are do you want to modify at run time some aspect of the XML layout, if so what aspect?
Possibly there are simpler ways to achieve what you need.

Categories

Resources