How to convert my RelativeLayout from XML to dynamically, programmatically produced Layout? - android

SOLVED - The ID of a View-Element should not be set to 0 !!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
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=".MainActivity" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/Button1" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button1"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_toRightOf="#+id/Button2"/>
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button2"/>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_toRightOf="#+id/Button3"/>
</RelativeLayout>
Now that is my Code in the Activity, i tried to to exactly what i did in the xml, but the items are displayed completely different:
public class MainActivity extends Activity {
private RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams layoutParams;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//button 1
Button button1 = new Button(this);
button1.setText("Button 1");
button1.setId(0);
relativeLayout.addView(button1);
//editText1
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// button2
Button button2 = new Button(this);
button2.setText("Button 2");
button2.setId(2);
layoutParams.addRule(RelativeLayout.BELOW, 0);
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//editText2
EditText editText2 = new EditText(this);
editText2.setId(3);
layoutParams.addRule(RelativeLayout.BELOW, 1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
relativeLayout.addView(editText2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
this.setContentView(relativeLayout);
}
}
SOLVED:
The main issue is, that an id of 0 for a view-element is not allowed...erm yea, beginner's mistake, i know...
THANK you anyone for helping on this question! SOLVED!!!

Try this! It converts xml to java code. However, I am not quite sure if you really need it in this case...

You're doing all these backwards
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Should be
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button2.setLayoutParams(layoutParams);
relativeLayout.addView(button2);
You may need to do more to get it the way you want, but why are you wanting to do this? If it works why change it?

you have already added the buttons to the xml file why are you creating dynamic buttons instead inflate the view if you want to add the layout dynamically also when you dynamically add the buttons to the relative layout you must call addRule function to place them at right place
just create objects of button and edittext refer the as you have refere your relative layout
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
and use them.
Edit----
For that you must remove buttons from the xml layout and then create the buttons like you are doing it's prefect just one thing like this here
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:layout_toRightOf="#+id/Button1"** />
to place this editText you must use addRule() function
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams .addRule(RelativeLayout.RIGHT_OF, 0);
where 0 is your button's id also set the layout params too to the layout like
editText.setLayoutParams(layoutParams );

Related

Assign margins to childviews

I'm inflating a view which just has a TextView in it. The parent view is a LinearLayout. My parent view looks like this:
<LinearLayout
android:id="#+id/posmSelectedBrandsLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_below="#+id/brandPOSMspinner"/>
and my child view looks like this:
<TextView
android:id="#+id/chip12345"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/tag_background"
android:text="TAG"
android:layout_marginTop="50dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#color/textcolorLogin"
android:textSize="22sp" />
This is how I'm inflating view:
final LinearLayout editParentLL = (LinearLayout) findViewById(R.id.posmSelectedBrandsLL);
final View editChildView = getLayoutInflater().inflate(R.layout.tag_layout, null);
editParentLL.addView(editChildView);
TextView tvChip = editChildView.findViewById(R.id.chip12345);
tvChip.setText(p.getProductName());
And the result that comes out is like this:
What I want is to separate the three TextViews from one another. So instead of a single box, it should come out as three different boxes.
I need your help to do it.
Thankyou.
try this
final View editChildView = getLayoutInflater().inflate(R.layout.view_add_item_with_details, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,2,10,2);
editChildView.setLayoutParams(layoutParams);
editParentLL.addView(editChildView);
TextView tvChip = editChildView.findViewById(R.id.chip12345);
tvChip.setText(p.getProductName());
you can set any values in .setMargin method
You should use LayoutParams to set your editChildView margins:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
editChildView.setLayoutParams(params);
more details reffer here: https://android--code.blogspot.in/2015/05/android-textview-layout-margin.html
if you need TextView in One by one, You have to use like Orientation
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

How do I create this XML programatically from my Activity?

So I know how to create each or all of these elements programatically but I am not sure how to place these LinearLayouts with weights etc, can someone perhaps help me a bit with generating the following XML programatically?
<LinearLayout 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:background="#drawable/background_main"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/main"
tools:context=".MainActivity"
android:weightSum="3">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/sa_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/sa_id_small"
android:onClick="onSAIDClicked"
android:visibility="invisible"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="phone"
android:singleLine="true"
android:textColor="#FFF"
android:textColorHint="#FFF"/>
<EditText
android:id="#+id/pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/phone_number"
android:layout_centerHorizontal="true"
android:hint="Pin Code"
android:imeOptions="actionDone"
android:inputType="numberPassword"
android:maxLength="4"
android:singleLine="true"
android:textColor="#FFF"
android:textColorHint="#FFF"/>
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pin"
android:layout_centerHorizontal="true"
android:onClick="onButtonClicked"
android:text="Register"/>
<Button
android:id="#+id/usa_drivers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/usa_drivers_dc_small"
android:onClick="onUSADriversClicked"
android:visibility="invisible"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/usa_passport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/usa_passport_small"
android:onClick="onUSAPassportClicked"
android:visibility="invisible"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</LinearLayout>
Thanks,
Wihan
you can use following example:
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.7f;
child2.setLayoutParams(childParam2);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
You can do this. For e.g :
// Create a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
setContentView(relativeLayout, layoutParams);
To programmatically set the layout weight, and other layout settings, on the LinearLayout, set the properties on an instance of LinearLayout.LayoutParams. The layout weight is the third argument to the constructor here.
LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(param);
The LinearLayout API Docs will provide more detail on the various properties and setters for encoding the data from your XML.
In general, depending on what you're trying to do, it might be better to define the layout in your XML, give the relevant elements IDs, and then access them programmatically using findViewById(R.id.yourElementId).
If you're ever in doubt over which methods to use, or moving between the XML and the Java API, the documentation is a good first stop. Take a look at the docs for Button and RelativeLayout as well.
You can do this easily like this:
In following example Switch, TextView and Relative View are programaticaly created from activity, and you don't need to use
setContentView(R.layout.activity_main)
just use this code after super.onCreate(savedInstanceState) and execute.
//UI Views
Switch btn_toggle_location, btn_toggle_state;
TextView txt_log;
RelativeLayout ad_container_top;
RelativeLayout ad_container_bottom;
RelativeLayout main_layout;
//Define properties of Switch 1
btn_toggle_location = new Switch(this);
btn_toggle_location.setChecked(true);
btn_toggle_location.setClickable(true);
btn_toggle_location.setId(1);
//Define properties of Switch 2
btn_toggle_state = new Switch(this);
btn_toggle_state.setChecked(true);
btn_toggle_state.setClickable(true);
btn_toggle_state.setId(2);
//Define properties of TextView
txt_log = new TextView(this);
txt_log.setMovementMethod(new ScrollingMovementMethod());
txt_log.setId(3);
//Define Layout parameters
RelativeLayout.LayoutParams params_btn_location = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params_btn_location.addRule(RelativeLayout.CENTER_IN_PARENT);
params_btn_location.setMargins(10, 0, 10, 0);
btn_toggle_location.setLayoutParams(params_btn_location);
RelativeLayout.LayoutParams params_btn_state = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params_btn_state.setMargins(10, 10, 10, 0);
params_btn_state.addRule(RelativeLayout.BELOW, btn_toggle_location.getId());
btn_toggle_state.setLayoutParams(params_btn_state);
RelativeLayout.LayoutParams params_log = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params_log.addRule(RelativeLayout.BELOW, btn_toggle_state.getId());
params_log.setMargins(10, 0, 10, 0);
txt_log.setLayoutParams(params_log);
//Add switch click listeners
btn_toggle_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ad_container_bottom.removeAllViews();
ad_container_top.removeAllViews();
txt_log.setText("");
if (isChecked) {
} else {
}
}
});
btn_toggle_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
txt_log.setText("");
if (isChecked) {
} else {
}
}
});
main_layout = new RelativeLayout(this);
RelativeLayout.LayoutParams main_lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
ad_container_top = new RelativeLayout(this);
RelativeLayout.LayoutParams layout_top = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layout_top.addRule(RelativeLayout.CENTER_VERTICAL);
layout_top.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout_top.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ad_container_top.setLayoutParams(layout_top);
ad_container_bottom = new RelativeLayout(this);
RelativeLayout.LayoutParams layout_bottom = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layout_bottom.addRule(RelativeLayout.CENTER_VERTICAL);
layout_bottom.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
ad_container_bottom.setLayoutParams(layout_bottom);
//Set view visibility
ad_container_bottom.setVisibility(View.VISIBLE);
ad_container_top.setVisibility(View.VISIBLE);
//Finally add all views to main layout here
main_layout.addView(btn_toggle_location, params_btn_location);
main_layout.addView(btn_toggle_state, params_btn_state);
main_layout.addView(txt_log, params_log);
main_layout.addView(ad_container_top, layout_top);
main_layout.addView(ad_container_bottom, layout_bottom);
main_layout.setBackgroundColor(Color.WHITE);
this.addContentView(main_layout, main_lp);

How to add margin to listview header?

I am trying to add a textview to listview header and give left margin to it. Here is what i do:
//create a textview, not inflating from layout
TextView selectAdressText = new TextView(getContext());
selectAdressText.setTextSize(12);
selectAdressText.setTextColor(getResources().getColor(R.color.text_black));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 10);
selectAdressText.setLayoutParams(lp);
addressesLV.addHeaderView(selectAdressText);
But it gives nullpointerexception. I also tried AbsListViewLayout params instead of LinearLayoutLayoutParams but it has no setMargins method. So which LayoutParams should i use for this?
Thanks
LinearLayout header = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 10);
TextView t = new TextView(this);
header.addView(t,lp);
addressesLV.addHeaderView(header);
try it.
Margin is overridden by ListView, so you can't use margin directly on the root view of your xml layout. Use padding instead.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="0dp"/>
or wrap a layout (as root node) around it:
<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="0dp"/>
</Framelayout>
just create your textView in xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="0dp"/>
View view = inflater.inflate(R.layout.textView, yourListview, false);
yourListview.addHeaderView(view);

android programmatic layout

I'm building a simple two-column list in android, and need to populate the number of rows dynamically. The xml layout works fine, but when i try to build the layout programmatically the second column doesn't show. Any ideas?
private void addRow(String text, String value) {
RelativeLayout line = new RelativeLayout(this);
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
line.setPadding(0, 0, 0, 15);
TextView caption = new TextView(this);
caption.setLayoutParams(rlp);
caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
caption.setText(text);
rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
TextView val = new TextView(this);
val.setLayoutParams(rlp);
val.setTextColor(0x05b4e4);
val.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
val.setText(value);
line.addView(caption);
line.addView(val);
mLayout.addView(line);
}
The comparable xml for a single row (that works) is this
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="15px" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="What"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/txtClubPath"
android:textColor="#05b4e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="stuff"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Again - the rows populate, but the second column isn't visible. Here's what it's going into:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:keepScreenOn="true">
<LinearLayout
android:id="#+id/viewRoot"
android:paddingTop="20px"
android:paddingBottom="40px"
android:paddingLeft="20px"
android:paddingRight="20px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
The layout params of the view you are adding should be of type LinearLayout.LayoutParams because the parent will be of that type.
This line
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
should become
line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
However, I think this should cause an exception if anything.
It is important for the RelativeLayout children to have a unique non-zero id.
You could, on API 17+, use
myView.setId(View.generateViewId());
Or if you have to support older API levels, you could use (and increment each time) a simple static AtomicInteger value.

How to align a button to bottom of screen

I have added a Button to my WebView. But I cannot get it to align to bottom of screen. I have given the code below.
Button button = new Button(context);
button.setText("Button");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
webView.addView(button);
Does anyone know how to do this.
For your concern try like this :;
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setGravity(Gravity.BOTTOM);
UPDATE :: Try some thing like this for the button U need to place at bottom of screen
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
You can try with an LinearLayout and gravity :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.BOTTOM;
Ok I got the solution. I did button.setY(screenHeight-button.getHeight());

Categories

Resources