0

Currently, I have a relative layout defined programatically

 RelativeLayout layout = new RelativeLayout(this);

then I add textviews and buttons like this

 layout.addView(myText, params);
 layout.addView(myBtn, param);

However, this makes the entire screen fill up with my relativelayout, how do I (programatically) set only certain number of pixels(dp) take up the screen? My goal is to have like the bottom half of the screen relativelayout

Anyhelp?

EDIT: This is what I am trying to achieve ultimately : https://i.stack.imgur.com/Zsxnm.png

I think this (in XML) might help me

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

I tried converting it into java like this before I add the textviews:

 parameters_layout = new 
         RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
     200);
 layout.setLayoutParams(parameters_layout);

EDIT 2 I tried adding:

    parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    parameters_layout.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
    layout.setGravity(Gravity.BOTTOM);
    layout.setLayoutParams(parameters_layout);

Still no luck

AndroidLove
  • 123
  • 4
  • 11
  • actually, the default values for new views (and layouts) is WRAP_CONTENT.this means that you should provide more code for us to look at. maybe you should also show a sketch of what you are trying to achieve ? – android developer Aug 25 '13 at 18:42

3 Answers3

0

Look on the official Android docs for RelativeLAyout.

http://developer.android.com/reference/android/widget/RelativeLayout.html

In particular,

setMinimumWidth(int)
setMinimumHeight(int)

Example:

layout.setMinimumWidth(500);
layout.setMinimumHeight(100);

If you want to set the height and width of TextView/EditText and Button (like in your example).

Here's the API reference in the official Android docs.

EditText: http://developer.android.com/reference/android/widget/EditText.html

setHeight(int)
setMaxHeight(int)
setMinHeight(int)
setMaxWidth(int)

Buttons: http://developer.android.com/reference/android/widget/Button.html

setHeight(int)
setMinWidth(int)

Just read the official documentations all of the reference are there. :)

Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
0

You didn't give a full code sample, so I'm assuming that your new RelativeLayout is the content view of your activity.

setContentView( layout );

This means your parent view is a frame layout, and you need to create a FrameLayout.LayoutParams. That params type doesn't give you much options, only gravity. You could try this:

RelativeLayout layout = new RelativeLayout(this);
int halfScreenHeight = //calculate from DisplayMetrics
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(MATCH_PARENT, halfScreenHeight, Gravity.BOTTOM);
setContentView( layout, params );

...but it's quite awkward. I think it's easier to just insert an empty View in the top half of your RelativeLayout. It will still be full screen, but no longer look full screen.

Barend
  • 17,296
  • 2
  • 61
  • 80
0

according to what i see in the image, you can either put everything in a relativeLayout that covers everything, while the bottom views are aligned to the bottom (use align parent bottom for this).

alternatively, you can set only the bottom part (which can be any layout you wish) to have a gravity of "bottom" inside the window itself.

in fact, i've recently asked about creating a dialog with transparent background not so long ago, so it might be helpful for you. here's the link . from my understanding, a very similar approach can be done for activities.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I still had no luck (please see my edit). I think it is also important to include that I am later adding this RelativeLayout to a WindowManager (insdie a Serivce class). I am having a similar issue as your's by wanting to put something on top of ongoing activity, so I used a Service and WindowManager with the permission SYSTEM_ALERT_WINDOW but its not editable. I posted it here, noone was able to help with that http://stackoverflow.com/questions/18424613/cannot-click-on-edittext-after-adding-it-to-windowmanager – AndroidLove Aug 25 '13 at 20:23
  • ok, i've added my answer there. for here, again, you can set the gravity to whatever you wish, just like in the code i've written on your question . – android developer Aug 25 '13 at 20:30
  • ok Thanks.. if I can follow your instructions on detaching an XML file on the other question, I wouldn't nee to worry about this because I can just do everything in the XML and bring it to java. thanks again – AndroidLove Aug 25 '13 at 20:49