How to place one imageview over another? - android

i want to place one imageview over another. The imageview which i want to place above is getting placed below.
Here's my code:
for (int i = 1; i <= numberofuploads; i++) {
if (i == 1) {
relativeframe1 = new RelativeLayout(this);
RelativeLayout.LayoutParams paramslayout1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT / 2,
RelativeLayout.LayoutParams.MATCH_PARENT);
paramslayout1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeframe1.setLayoutParams(paramslayout1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
pxrparams1a, pxrparams1b);
imgvwphoto1 = new ImageView(this);
imgvwphoto1.setId(1);
imgvwphoto1.setAlpha(500);
imgvwphoto1.setBackgroundResource(R.drawable.plusicon3);
// params1.setMargins(0, 90, 100, 40);
params1.setMargins(left1, top1, right1, bottom1);
imgvwphoto1.setLayoutParams(params1);
relativeframe1.addView(imgvwphoto1);
imgvwphoto1.setScaleType(ScaleType.MATRIX);
imgvwphoto1.setOnTouchListener(this);
RelativeLayout.LayoutParams paramscomponent1 = new RelativeLayout.LayoutParams(
irpc1, RelativeLayout.LayoutParams.MATCH_PARENT);
paramscomponent1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imgvwcomponent1 = new ImageView(this);
imgvwcomponent1.setAlpha(0);
imgvwcomponent1 .setBackgroundResource(R.drawable.leftpartdoubleheartframe);
imgvwcomponent1.setBackgroundResource(componentname.getImagename());
imgvwcomponent1.setLayoutParams(paramscomponent1);
relativeframe1.addView(imgvwcomponent1);
relativeframe.addView(relativeframe1);
} else {
relativeframe2 = new RelativeLayout(this);
RelativeLayout.LayoutParams paramslayout2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT / 2,
RelativeLayout.LayoutParams.MATCH_PARENT);
paramslayout2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeframe1.setLayoutParams(paramslayout2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
pxrparams2a, pxrparams2b);// 106.67dp, 120dp
imgvwphoto2 = new ImageView(this);
imgvwphoto2.setId(2);
imgvwphoto2.setAlpha(500);
imgvwphoto2.setBackgroundResource(R.drawable.plusicon3);
// params2.setMargins(165, 90, 0, 40);
params2.setMargins(left2, top2, right2, bottom2);
imgvwphoto2.setLayoutParams(params2);
relativeframe2.addView(imgvwphoto2);
imgvwphoto2.setScaleType(ScaleType.MATRIX);
imgvwphoto2.setOnTouchListener(this);
RelativeLayout.LayoutParams paramscomponent2 = new RelativeLayout.LayoutParams(
irpc2, RelativeLayout.LayoutParams.MATCH_PARENT);// 103.33dp
paramscomponent2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgvwcomponent2 = new ImageView(this);
imgvwcomponent2.setAlpha(500);
imgvwcomponent2
.setBackgroundResource(R.drawable.rightpartdoubleheartframe);
imgvwcomponent2.setLayoutParams(paramscomponent2);
relativeframe2.addView(imgvwcomponent2);
relativeframe.addView(relativeframe2);
}
}
on click of plus icon, image is uploaded on that imgvwphoto,that is overlaping with heart frame, i want it to be viewed inside heart frame only. not outside of heart frame.

You should use two separate layouts and place one at another using <include> statemant like this
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="#layout/lay1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<include layout="#layout/lay2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</merge>

Related

Why is this code for adding buttons programatically does not work?

I am trying to add some buttons to a Relative Layout programatically. I used some examples seen on StackOverflow, but for some reason I cannot understand, my code is not working: I want to have my buttons one above the other, but they end up being one on top of the other.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
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"
tools:context="com.android.myApp.MainActivity" >
</RelativeLayout>
And here is my code:
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(0);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams.addRule(RelativeLayout.BELOW, 0);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
Checkout this code....Hope this will help
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(10);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lprams.addRule(RelativeLayout.BELOW, 10);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);

Add relative layout background

I am having a relative layout I want to create the layout dynamically the layout in XMl lookes like
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
android:background="#drawable/step_background"
android:orientation="vertical" >
I made this like -
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
But how to add the background I am doing this in a Fragment not in a activity
try as follows...
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
layout.setLayoutParams(lprams);
layout.setBackgroundResource(R.drawable.step_background);
Try This : -
RelativeLayout mRelativeLayoutPopup = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5,5,5,5);
mRelativeLayoutPopup.setLayoutParams(layoutParams);
mRelativeLayoutPopup.setBackgroundResource(drawable_background);
You can use setBackground for that.
Drawable bg_image= getResources().getDrawable(R.drawable.image);
myLayout.setBackground(bg_image);
Hope it helps.

Android: Connect buttons with lines

I´m writing an Android Aplication that is very similar to a binary tree/graph, there are several buttons created dinamically but I can´t figure out how to create lines connecting the buttons.
Here´s an image showing what I need to do:
Here is my XML file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hscroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#888888" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativescroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF" >
<!-- Buttons and lines connecting them -->
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And here is an example of the activity class:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativescroll);
RelativeLayout.LayoutParams newParams;
// Botão 1
Button btn1 = new Button(this);
btn1.setId(1);
btn1.setText("Botão 1");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn1.setLayoutParams(newParams);
mainLayout.addView(btn1);
// Botão 2
Button btn2 = new Button(this);
btn2.setId(2);
btn2.setText("Botão 2");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btn2.setLayoutParams(newParams);
mainLayout.addView(btn2);
// Botão 3
Button btn3 = new Button(this);
btn3.setId(3);
btn3.setText("Botão 3");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 2);
btn3.setLayoutParams(newParams);
mainLayout.addView(btn3);
// Botão 4
Button btn4 = new Button(this);
btn4.setId(4);
btn4.setText("Botão 4");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 3);
btn4.setLayoutParams(newParams);
mainLayout.addView(btn4);
// Botão 4
Button btn5 = new Button(this);
btn5.setId(5);
btn5.setText("Botão 5");
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.setMargins(0, 0, 20, 50);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.RIGHT_OF, 4);
btn5.setLayoutParams(newParams);
mainLayout.addView(btn5);
// DRAW LINK
NodeLink link = new NodeLink(this);
newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
newParams.addRule(RelativeLayout.BELOW, 1);
newParams.addRule(RelativeLayout.ABOVE, 2);
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
newParams.addRule(RelativeLayout.LEFT_OF, 2);
mainLayout.addView(link);
}
}
Please, any ideas on how can I do that?
You might want to say what NodeLink is doing - custom view class?
Any case, I'd suggest getting the coordinates of "anchors" on your buttons (top-center points, from your diagram), then creating Paths that link associated buttons' anchors. Draw/paint these Paths into a Picture, save it as a PictureDrawable, and then you can set that Drawable as the background of your buttons' parent (the RelativeLayout/mainLayout).

How to create this UI ( android xml ) in Java code

I have this main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/barslide"
android:layout_marginLeft="500dp"/>
</LinearLayout>
</FrameLayout>
I want to create the same UI like in main.xml in java code. I try to code it but it doesn't work, it's not same as the xml. Here is the code:
void createUI(){
LayoutParams params1 = new FrameLayout
(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
LayoutParams params2 = new android.widget.FrameLayout.LayoutParams(
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
Gravity.BOTTOM);
FrameLayout f1 = new FrameLayout(this);
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(params2);
view1 = new page1(getBaseContext());
view360 = view1.img1(getBaseContext());
view360.setBackgroundDrawable(getResources().getDrawable(R.drawable.black1));
view360.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
slidebar = view1.img3(getBaseContext());
slidebar.setBackgroundResource(R.drawable.barslide);
slidebar.setLayoutParams(new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM));
f1.addView(view360);
f1.addView(l2);
l2.addView(slidebar);
addContentView(f1, params1);
}
Your code doesn't quite replicate the xml layout(this was to big for a comment):
// replace THIS(from the constructors) with your context reference
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
ImageView imv1 = new ImageView(this); // the first ImageView
imv1.setLayoutParams(new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
imv1.setImageResource(R.drawable.ic_launcher);
fl.addView(imv1);
LinearLayout ll = new LinearLayout(this);
FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
llp.gravity = Gravity.BOTTOM;
ll.setLayoutParams(llp);
ll.setBackgroundColor(Color.WHITE);
ImageView imv2 = new ImageView(this); // the second ImageView
DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = 300f;
int pWidth = (int) (metrics.density * width + 0.5f);
LinearLayout.LayoutParams imvlp = new LinearLayout.LayoutParams(pWidth,
FrameLayout.LayoutParams.WRAP_CONTENT);
float margin = 500f;
int pMargin = (int) (metrics.density * margin + 0.5f);
imvlp.leftMargin = pMargin;
imv2.setLayoutParams(imvlp);
imv2.setBackgroundResource(R.drawable.food_image1);
ll.addView(imv2);
fl.addView(ll);
You'll have to modify it to use your object page1(I don't know exactly what are you trying to do there).
Also, are you sure you have to use the addContentView()(maybe setContentView?) method?(This method will add the views to the already present layout)

Adwhirl align to bottom

I have a screen where i have 2 fullscreen views, and i want an adwhirl ad at the bottom.
Here is my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:id="#+id/main_layout"
>
</RelativeLayout>
Then i put the views on it from code:
// in activity's onCreate
RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_layout);
v1= new V1(this);
v2= new V2(this, v1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
v1.setLayoutParams(lp);
v2.setLayoutParams(lp);
layout.addView(v1);
layout.addView(v2);
.....
AdWhirlLayout adWhirlLayout2 = new AdWhirlLayout(this, "key");
adWhirlLayout2.setAdWhirlInterface(this);
adWhirlLayout2.setMaxWidth(width);
adWhirlLayout2.setMaxHeight(height);
RelativeLayout.LayoutParams lp_bottom = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_bottom.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
adWhirlLayout2.setLayoutParams(lp_bottom);
layout.addView(adWhirlLayout2);
But the ads are still at the top of the screen, like when i didn't set the layoutparams.
Try :
lp_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

Categories

Resources