Reputation: 1
Im overlaying a linearlayout.The layout contains a button.The button should be top right of the linearlayout.But gravity doesnt seem to work.
CODE: Inside the onCreate method of my service.
final WindowManager.LayoutParams params3 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LinearLayout ll=new LinearLayout(this);
LinearLayout ll2=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.RIGHT;
lp.width=30;
lp.height=30;
b=new Button(this);
b.setBackgroundResource(R.drawable.x);
params3.gravity=Gravity.TOP;
params3.height=200;
params3.width=200;
ll.addView(b, lp);
wm.addView(ll, params3);
The linearlayout 200X200 is created and is on top.But the button isnt top right.I tried using b.setWidth and b.setHeight. would not help.
Upvotes: 0
Views: 305
Reputation: 17115
LinearLayout by default is horizontal You can't align horizontally in horizontal LinearLayout (e.g right, center_horizontal, left) and you can't align vertically in vertical LinearLayout (e.g top center_vertical, bottom) in vertical LinearLayout.
If you need to align it to the right you must either set your LinearLayout to be vertical or use a different ViewGroup, for example FrameLayout.
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinarLayout.VERTICAL);
Ant the Buttom will always be on top since it's a first item. And why not doing it in xml? It will be much easier when less code.
Edit: To put button to top right of the VideoView your layout will look like this.
<?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"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="200dp"
android:layout_height="200dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_alignTop="@+id/videoView1"
android:layout_alignRight="@+id/videoView1"
android:text="Button" />
</RelativeLayout>
Put this layout in a layout res folder of the project. Project/res/layout/your_layout.xml
To attach a layout to Activity's window:
public final class YourActivity
extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// Get VideoView
VideoView vv = (VideoView) findViewById(R.id.videoView1);
//get Button reference
View button = findViewById(R.id.button1);
}
}
Upvotes: 2