Sunday, 7 December 2014

Positioning widgets added to layout using addView



I want to position the stopButton to top-right. It shows on top-left. How do I achieve that?


Here's the code:



private WindowManager wm;
private WindowManager.LayoutParams params1;
LinearLayout layout;
Button stopButton;
TextView text;

@Override
public void onCreate() {
super.onCreate();
/**getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);**/
text = (TextView) findViewById(R.id.texttest);
layout = new LinearLayout(this);
stopButton = new Button(this);
ViewGroup.LayoutParams buttonParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
stopButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.cancel));
stopButton.setLayoutParams(buttonParams);
layout.addView(stopButton);

LinearLayout.LayoutParams mLinearLayout = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layout.setBackgroundColor(Color.argb(66, 255, 0, 0));
layout.setLayoutParams(mLinearLayout);
wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
params1 = new WindowManager.LayoutParams(
200, 150, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);

params1.x = 0;
params1.y = 0;
params1.gravity = Gravity.CENTER | Gravity.TOP;
params1.format = PixelFormat.TRANSPARENT;
wm.addView(layout, params1);


Also, I'm using this in a Service class. Is it possible that I write all of elements I want to add to LinearLayout (for example, stopButton) within XML? If I can do that, how do I add it to the LinearLayout?


No comments:

Post a Comment