Hy.... I'm making a project on eclipse. In this project I made an xml layout like this :
<ScrollView 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"
tools:context=".AddRemoveMainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/main_layout"
>
<!-- I will add some view programatically in this line -->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/b_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="addItems"
/>
<Button
android:id="#+id/b_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:onClick="deleteItems"
/>
</TableRow>
</LinearLayout>
</ScrollView>
And on my mainactivity :
public class AddRemoveMainActivity extends Activity {
Button add,del;
LinearLayout ll;
private String[] spinnerContent = {"Ikan","Udang","Kepiting","Kerang"};
private int count = 0;
private int line = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_remove_main);
ll = (LinearLayout) findViewById(R.id.main_layout);
}
public void addItems(View view)
{
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerContent);
final TableRow row1 = new TableRow(this);
Spinner spin = new Spinner(this);
TextView jenis = new TextView(this);
TextView keterangan = new TextView(this);
TextView total = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
spin.setAdapter(adapter);
jenis.setText("Jenis : "+Integer.toString(count));
//jenis.setLayoutParams(params);
keterangan.setText("Keterangan : ");
total.setText("Jumlah(kg) :");
row1.addView(jenis);
row1.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin);
frame.addView(row1);
ll.addView(frame, line);
line++;
}
public void deleteItems(View view)
{
if(count > 0)
{
final LinearLayout temp = (LinearLayout)ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
line--;
}
}
As you can see from my mainActivity, I have added textview(jenis) and spinner(spin) to a tableRow, then I added that tableRow to a linearLayout(frame) then added that frame to the linearLayout that came from in xml file above the two buttons (Add & Del button).......
My problem here is that I just want to add a margin between my textview (jenis) and (Spinner) spin. As you can see again from my mainActivity, I have tried using :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 13, 17, 9);
...
...
...
jenis.setLayoutParams(params);
But this one is not work... And I have tried another way but it's not work too :
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
So Any idea or another way for adding margin to my textview...???
Thanks in advance... :-)
Try the below code and let me know if it works
android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 13, 17, 9);
row1.addView(jenis, layoutParams);
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);
row1.addView(spin, layoutParams);
That works:
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
// NJ forgot the last important step, set LayoutParams on the TextView:
v.setLayoutParams(mlp);
}
Please add this method and check . it worked for me
public static void setRunTimeMargin(View v, int left, int top, int right,
int bottom) {
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
.getLayoutParams();
mlp.setMargins(left, top, right, bottom);
}
Related
So I want to add x amount of buttons to a layout in android (where x is usually between 4 and 24), and I can achieve this using the code below, but it doesn't scroll so it cuts off some of the buttons
I am using a fragment, which contains a LinearLayout and BottomNavigation, and one of those navigation options leads to the fragment contained below
At the moment my.xml file looks like:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_course_units, container, false);
final LinearLayout unitsBtns = (LinearLayout) view.findViewById(R.id.units_group);
//I actually get this from a volley request but didn't want to include all the code
String[] units = {"1", "2", "3", "4", "5", "6", "7", "8"};
for(int i = 0; i < units.length; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
params.setMargins(0, 30, 0, 0);
Button b = new Button(getActivity());
b.setLayoutParams(params);
b.setText(units[i]);
b.setId(i);
b.setBackgroundColor(Color.WHITE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// STUFF HERE
} catch (Exception e) {
e.printStackTrace();
}
}
});
unitsBtns.addView(b);
}
return view;
}
Is there any way to get the buttons to be scrollable? I have tried to put the scrollView around the parent of this too and it still doesn't seem to work.
Note: I am still pretty new to developing native Android apps so please correct me if I'm doing something incorrectly
when you have to use ScrollView you should give android:layout_height="wrap_content" in child view of ScrollView then only you have to scroll your view
You should use user LinearLayout like this
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
here also you have to replace
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Note: when you give match_parent in height attribute, it will limit your view to device screen
hope it will help you.. :)
Here's a working example (horizontal) :
<HorizontalScrollView
android:id="#+id/hsv1"
android:scrollbars="none"
android:background="#0d47a1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/hsvLayout1"
android:background="#0d47a1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
Get ScrollView and Layout:
HorizontalScrollView hsv1 = (HorizontalScrollView) findViewById( R.id.hsv1 );
LinearLayout layout = (LinearLayout) hsv1.findViewById( R.id.hsvLayout1 );
layout.removeAllViews();
Create a button dynamically:
Button myButton = new Button (this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
lp.setMargins( 20, 0, 20, 0 );
myButton.setLayoutParams(lp);
myButton.setTextColor( Color.YELLOW );
myButton.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
myButton.setText( "Hello World !" );
Add button to layout:
layout.addView(myButton);
I am working on an Android application in which I want to add EditText below an EditText which is at the first added in XML, and then added in code one below the other. Even though I am giving margin from top 200, the new EditText is appearing on top right. What am I doing wrong?
XML file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"
android:orientation="horizontal"
>
<RelativeLayout
android:id="#+id/menuLayout"
android:layout_width="wrap_content"
android:layout_height="1000dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="180dp"
android:background="#drawable/bottom_border"
android:padding="3dp"
android:scaleType="fitXY"
android:src="#drawable/mittag_top" />
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="80dp"
android:layout_marginStart="80dp"
android:layout_marginTop="125dp"
android:hint="Menu karte datum..."
android:lineSpacingExtra="10dp"
android:lineSpacingMultiplier="1"
android:paddingTop="7dp"
android:textColor="#color/abc_primary_text_material_dark"
android:textColorHint="#color/abc_primary_text_material_dark"
android:textSize="12sp" />
<EditText
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignLeft="#+id/menuDate"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="#+id/menuDate"
android:layout_below="#+id/menuDate"
android:hint="Opening times..."
android:lineSpacingExtra="10dp"
android:lineSpacingMultiplier="1"
android:paddingTop="7dp"
android:textColor="#color/abc_primary_text_material_dark"
android:textColorHint="#color/abc_primary_text_material_dark"
android:textSize="12sp" />
<EditText
android:id="#+id/firstEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/openTime"
android:textColor="#color/abc_primary_text_material_dark"
android:textColorHint="#color/abc_primary_text_material_dark"
android:hint="Menu entry.." />
</RelativeLayout>
</ScrollView>
Java code :
public class AddStuff extends Activity{
EditText firstEntry, date, time;
RelativeLayout relativeLayout;
Typeface type;
int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ocr_menu);
type = Typeface.createFromAsset(getAssets(),"fonts/MyFont.ttf");
firstEntry = (EditText)findViewById(R.id.firstEntry);
firstEntry.setTypeface(type);
date = (EditText)findViewById(R.id.date);
date.setTypeface(type);
time = (EditText)findViewById(R.id.time);
time.setTypeface(type);
relativeLayout = (RelativeLayout)findViewById(R.id.menuLayout);
firstEntry.addTextChangedListener(new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
addEditText();
}
}
});
}
public void addEditText(){
EditText editText = new EditText(getApplicationContext());
editText.setHint("Entry number "+counter);
// editText.setPadding(2, 200, 2, 2);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(2, 300, 0, 0);
editText.setHintTextColor(Color.WHITE);
editText.setTypeface(type);
relativeLayout.addView(editText);
counter++;
}
}
What am I doing wrong? How can I add EditText below the last added EditText. Thank you.
Update
updated addEdittext()
public void addEditText(){
EditText editText = new EditText(getApplicationContext());
editText.setHint("Entry number "+counter);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int idLastChild= relativeLayout.getChildAt(relativeLayout.getChildCount()-1).getId();
params.addRule(RelativeLayout.BELOW,idLastChild);
editText.setHintTextColor(Color.WHITE);
editText.setTypeface(type);
relativeLayout.addView(editText);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() > 5) {
addEditText();
}
}
});
counter++;
}
Because adding dynamic EditText in RelativeLayout so need to use LayoutParams .addRule instead of margin to align EditText:
EditText editText = new EditText(AddStuff.this);
...
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// get last child id from RelativeLayout
int idLastChild=relativeLayout.getChildAt(relativeLayout.getChildCount()-1);
params.addRule(RelativeLayout.BELOW,idLastChild);
// set id for EditText
editText.setId(idLastChild+1);
// set layout params
editText.setLayoutParams(params);
relativeLayout.addView(editText, params);
It's a relative layout. Not LinearLayout. You should specify how position of your view depends on other views.
RelativeLayout.LayoutParams params
= (RelativeLayout.LayoutParams) yourView.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.firstEntry);
You're creating the params, but not assigning them to the layout. Do as follow:
EditText editText = new EditText(getApplicationContext());
final LayoutParams lparams = new LayoutParams(50,30); // Width , height
editText.setLayoutParams(lparams);
Try this
public void addEditText(){
EditText editText = new EditText(getApplicationContext());
editText.setHint("Entry number "+counter);
// editText.setPadding(2, 200, 2, 2);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(2, 300, 0, 0);
editText.setHintTextColor(Color.WHITE);
editText.setTypeface(type);
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
newParams.addRule(RelativeLayout.BELOW, R.id.date);
editText.setLayoutParams(newParams);
relativeLayout.addView(editText);
counter++;
}
I wanted to set margin to View programmatically. Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<ListView
android:id="#+id/attendeelistview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#F6CECE" >
</ListView>
</LinearLayout>
And the method when my button onClick to call out the popup Window view:
private void openPopUp() {
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.event_attendee_pop,
null);
llAttendeeList = (LinearLayout) popupView
.findViewById(R.id.llAttendeeList);
attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(10, 10, 10, 0);
popupView.setLayoutParams(params);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, 350);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
mAdapter = new ListAdapter(getActivity());
attendeeListView.setAdapter(mAdapter);
}
I tried to set margin to the popupView but no luck. The margin is not there. I wonder which part override it.
Manually set width and height of PopupWindow :
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
popupWindow.setWidth(width-10);
popupWindow.setHeight(height-10);
try to use layout it will work
LinearLayout.LayoutParams params = new LayoutParams
(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
follow this tutorial http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams
I am creating a Fragment that will dynamically create a tableLayout but I am having problems with it as it is not being displayed. Can someone tell me why it is not displaying? The section label can be displayed except for the table.
Here is my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_fragment_bp_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/section_bp_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/tableLayoutBloodPressure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
Here is my Fragment Class - the data is for testing only
public class BloodPressureFragment extends Fragment {
public BloodPressureFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bp_list, container,
false);
TextView txt = (TextView) rootView.findViewById(R.id.section_bp_label);
txt.setText("Blood Pressure History Logs");
TableLayout ll = (TableLayout) rootView
.findViewById(R.id.tableLayoutBloodPressure);
for (int i = 1; i < 10; i++) {
TableRow tbrow = new TableRow(getActivity().getApplicationContext());
tbrow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv1 = new TextView(getActivity().getApplicationContext());
tv1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setId(i);
tv1.setText("Test id: " + i);
tbrow.addView(tv1);
ll.addView(tbrow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
return rootView;
}
}
I have same problem, and my solution is to inflate row layout from fragment view.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_intro_a,container,false); // infalte normally
Tablelayout mytable = (TableLayout) v.findViewById(R.id.mytable); // init table
/* your stuff*/
/* now fill table layout directly on fragment */
TableRow row = new TableRow(getActivity()); // new row
View table_row_view = v.inflate(getActivity(),R.layout.your_layout,row); // inflate from parent view;
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView mytextview = (TextView)vvv.findViewById(R.id.yourtextview-in-yourlayout);
mytable.addView(row, new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
/* TADAAN! rows is visible! */
}
Instead of this hiearchy
[TableLayout < TableRow < Textview]
try to add directly textview to tablelayout, avoiding tablerows.
UPDATE
This works fine for me. Create a tableLayout where generate few tablerows with some textviews inside.
TableLayout tl;
tl = (TableLayout) rootView.findViewById(R.id.fragment1_tlayout);
for (int i = 0; i < 30; i++) {
TableRow tr = new TableRow(getActivity());
tr.setId(i);
tr.setBackgroundResource(R.color.RoyalBlue);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//TEXTVIEWS********
TextView tv1 = new TextView(getActivity());
tv1.setText("TEST NUMBER");
tv1.setId(i);
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tr.addView(tv1);
TextView tv2 = new TextView(getActivity());
tv2.setText("nÂș: "+ i);
tv2.setId(i+i);
tv2.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv2.setPadding(5, 5, 5, 5);
tr.addView(tv2);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
XML:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<TableLayout
android:id="#+id/fragment1_tlayout"
android:background="#color/LightGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
Android XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_height="28dp" android:layout_width="180dp"
android:layout_toRightOf="#+id/signinemailtxt"
android:paddingLeft="50dp"
android:layout_marginTop="65dp"
android:background="#ffffff"
/>
Java:
layout= new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView Txt=new TextView(this);
Txt.setText("Name");
How to get layout_marginTop,layout_toRightOf options in java code
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
For layout_margin :
LayoutParams params = new LayoutParams(context, attrs);
params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)
Txt.setLayoutParams(params);
Don't declare new instances of layout and text view, but get the ones you created in the xml:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
TextView Txt = (TextView) findViewById(R.id.TextViewId);
Now you can edit the layout and textview you are seeing on the screen.