Open top menu

 In this blog create the Button in RelativeLayout and set the height and width  programatically, try this code







package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class example extends Activity  {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.text);




LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, // Width of TextView
LayoutParams.WRAP_CONTENT); // Height of TextView



Button btn = new Button(this);
btn.setText("AndroidBeginner Point");
btn.setPadding(10, 3, 10, 3);
btn.setGravity(Gravity.CENTER_VERTICAL);
btn.setLayoutParams(params);
layout.addView(btn);

}
}

0 comments