0% found this document useful (0 votes)
51 views2 pages

Set Layout Parameters in Your Code

The document contains a Java code snippet for an Android application that sets up a simple user interface with a TextView and a Button within a LinearLayout. It demonstrates how to create layout parameters programmatically and add views to the layout. Additionally, there is an XML layout file that defines a LinearLayout with a TextView displaying a string resource.

Uploaded by

amits.bhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Set Layout Parameters in Your Code

The document contains a Java code snippet for an Android application that sets up a simple user interface with a TextView and a Button within a LinearLayout. It demonstrates how to create layout parameters programmatically and add views to the layout. Additionally, there is an XML layout file that defines a LinearLayout with a TextView displaying a string resource.

Uploaded by

amits.bhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Set Layout Parameters in your code

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Test extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("This is a TextView");
tv.setLayoutParams(params);

Button btn = new Button(this);


btn.setText("This is a Button");
btn.setLayoutParams(params);

layout.addView(tv);

layout.addView(btn);
LinearLayout.LayoutParams layoutParam =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT );

this.addContentView(layout, layoutParam);
}
}

//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

You might also like