Setting stacked up layout programmatically in android - android

I would like to have a relative layout with linear layouts nested in between them but i am unable to understand how ill nest them programmatically
This is what i have as my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="Add items" />
<LinearLayout
android:id="#+id/above_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_top"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="#+id/label_farmerprovince"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Province"
android:textSize="12sp"
android:textStyle="normal" />
<EditText
android:id="#+id/fivms_farmerprovince"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="#+id/distric_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="District"
android:textSize="12sp"
android:textStyle="normal" />
<EditText
android:id="#+id/district"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:id="#+id/label_farmeragriblocks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Agriblocks"
android:textSize="12sp"
android:textStyle="normal" />
<EditText
android:id="#+id/agri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background" />
</LinearLayout>
</LinearLayout>
This produces the layout that i want:
Now i would like to add the above layout and change it to a java way
I hav tried
//top layout
RelativeLayout newlayout = new RelativeLayout(getContext());
LinearLayout belowlayout = new LinearLayout(getContext());
LinearLayout above_part = new LinearLayout(getContext()); //lin with id of above_part
LinearLayout in_above_part_one = new LinearLayout(getContext()); //lin with id of above_part
//layouts properties
belowlayout.setOrientation(LinearLayout.HORIZONTAL);
//setids for the layouts
newlayout.setId(12);
belowlayout.setId(13);
//Button
Button additemsbtn = new Button(getContext());
additemsbtn.setText("Log In");
additemsbtn.setBackgroundColor(Color.BLACK);
//Username input
EditText username = new EditText(getContext());
additemsbtn.setId(int 1);
username.setId(2);
RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
);
RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
//Give rules to position widgets
usernameDetails.addRule(RelativeLayout.ABOVE, additemsbtn.getId());
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
usernameDetails.setMargins(0,0,0,50);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
Whatever i have started fails since am unable to know how to nest the different linear layouts,
I would probably like guidelines on how to do this nesting programmatically so that i can achieve the same thing generated by the xml file

Little example how it can be done. So, all you need to do is to add inner items to my nested layouts and configure margins/paddings etc. Also added background for better visualization.
RelativeLayout root = (RelativeLayout) findViewById(R.id.root);
Button addButton = new Button(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addButton.setLayoutParams(params);
addButton.setId(123);
root.addView(addButton);
LinearLayout rootLinearlayout = new LinearLayout(this);
RelativeLayout.LayoutParams linearRootParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
linearRootParams.addRule(RelativeLayout.BELOW, 123);
rootLinearlayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
rootLinearlayout.setLayoutParams(linearRootParams);
// Nested Linears
LinearLayout linearOne = new LinearLayout(this);
LinearLayout linearTwo = new LinearLayout(this);
LinearLayout linearThree = new LinearLayout(this);
LinearLayout.LayoutParams linearParamsWithWeight = new LinearLayout.LayoutParams(
0,
300);
linearParamsWithWeight.weight = 1;
linearOne.setBackgroundColor(ContextCompat.getColor(this, android.R.color.black));
linearTwo.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_red_dark));
linearThree.setBackgroundColor(ContextCompat.getColor(this, android.R.color.holo_blue_dark));
linearOne.setLayoutParams(linearParamsWithWeight);
linearTwo.setLayoutParams(linearParamsWithWeight);
linearThree.setLayoutParams(linearParamsWithWeight);
rootLinearlayout.addView(linearOne);
rootLinearlayout.addView(linearTwo);
rootLinearlayout.addView(linearThree);

Related

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);

Different view edittext in Android

When I create EditText programmatically I have no underline (in the second EditText). Like this: http://oi41.tinypic.com/2ut427d.jpg
When I create an EditText in XML everything is fine, see http://oi44.tinypic.com/nmxdnm.jpg. Where is the error?
This is my XML layout:
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<include layout="#layout/input_title_layout" />
<TableLayout
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":" />
<EditText
android:id="#+id/diameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:digits="0123456780."
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
And code to add EditText programatically:
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams.bottomMargin = 0;
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
inputLinearLayout.addView(tableLayout);
TableRow tableRowEdit = new TableRow(context);
TextView titleTv = new TextView(context);
titleTv.setTextAppearance(context, R.style.tvTitleStyle);
titleTv.setText(context.getString(context.getResources().getIdentifier(inputTvs[i] , "string", context.getPackageName())));
TextView separatorTv = new TextView(context);
separatorTv.setGravity(Gravity.LEFT);
separatorTv.setPadding(5, 0, 5, 0);
separatorTv.setText(":");
separatorTv.setTextColor(Color.BLACK);
EditText editText = new EditText(context);
editText.setId(i);
tableRowEdit.addView(titleTv);
tableRowEdit.addView(separatorTv);
tableRowEdit.addView(editText);
tableLayout.addView(tableRowEdit);
It's hard to tell exactly what the issue might be given the code you supplied.
-Have you tried setting the layout parameters of the TableRow/TextView/EditText? The TextViews might be taking up all the visible space.
-Are you sure inputLinearLayout has been inflated?

How can I give top blank when I add textview programmatically?

I am doing an app like whatsup. My question is about interface. I can get a message desc and parse it. After that I can add layout programmatically but it is writing same coordinate all the time. I tried tv.layout(l, t, r, b) and tv.setTop but doesn't work.
Where is my issue?
bubbleLayout=(RelativeLayout)findViewById(R.id.layoutbubble);
for(int i=0;i<msgid.length;i++){
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(msgdesc[i]);
tv.setTop(20);
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
bubbleLayout.addView(tv);
My XML File:
<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="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="1300dip"
android:layout_height="80dip"
android:background="#084B8A" >
<TextView
android:id="#+id/messagetitle"
android:layout_width="wrap_content"
android:layout_height="90dip"
android:layout_centerInParent="true"
android:layout_marginTop="30dip"
android:textColor="#ffffff"
android:textSize="12pt" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutbubble"
android:layout_width="1300dip"
android:layout_height="500dip"
android:layout_marginTop="10dip" >
</RelativeLayout>
<RelativeLayout
android:layout_width="1300dip"
android:layout_height="80dip"
android:layout_marginTop="10dip" >
<EditText
android:id="#+id/replytext"
android:layout_width="1000dip"
android:layout_height="90dip"
android:layout_marginLeft="10dip" />
<Button
android:id="#+id/sendbutton"
android:layout_width="200dip"
android:layout_height="90dip"
android:layout_marginLeft="1050dp"
android:background="#084B8A"
android:text="Send"
android:textColor="#ffffff"
android:textSize="12pt" />
</RelativeLayout>
</LinearLayout>
Screen Shot
I solved my problem.Changed my RelativeLayout to LinearLayout in the xml
<LinearLayout
android:id="#+id/layoutbubble"
android:layout_width="1300dip"
android:layout_height="500dip"
android:layout_marginTop="10dip"
android:orientation="vertical">
</LinearLayout>
And change my code as follows:
bubbleLayout=(LinearLayout)findViewById(R.id.layoutbubble);
for(int i=0;i<msgid.length;i++){
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(msgdesc[i]);
tv.setPadding(30, 10, 0, 0);
bubbleLayout.addView(tv);
}
That is it.
I think you are using RelativeLayout or a normal LinearLayout?
Try using a LinearLayout with android:orientation="vertical"

Kinda circular dependency in RelativeLayout with embedded ScrollView

Basically, an Activity should display on screen:
1. A logo on top. Must always be displayed. Must center both vertically and horizontally, when choices leaves extra space on screen.
2. A ScrollView at bottom. Hosts vertical LinearLayout, which in turn hosts a set of choices a user can choose from. Must take minimal space necessary to display all choices (So logo is vertically centered). Can take up up to (Screen - Logo) space on screen. Choices are added programmatically.
Now, I have defined it in layout as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:src="#drawable/logo" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/imageView"
>
<LinearLayout
android:id="#+id/ChoicesPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
If ImageView is layed out using layout_above=scrollView, ScrollView is layed out before, NOT taking into consideration minimum size of ImageView.
If ScrollView is layed out using layout_below=ImageView, ImageView just stays on top. When there are only 2-3 choices, screen looks pretty ugly.
Is there a way I can satisfy the constraints? Or should I define two different layout xml files, and switch to the correct one programmatically?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageView" >
<LinearLayout
android:id="#+id/ChoicesPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_6"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
this seems to work for me. its like 14 items in the scrollview
public class TestActivity extends Activity {
private RelativeLayout mRelativeLayout;
private RelativeLayout mRelativeLayout2;
private ImageView mImageView;
private ScrollView mScrollView;
private TextView TV1;
private TextView TV2;
private TextView TV3;
private TextView TV4;
private Button B1;
private Button B2;
private Button B3;
private Button B4;
private RelativeLayout.LayoutParams lp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRelativeLayout = new RelativeLayout(this);
setContentView(mRelativeLayout);
mImageView = new ImageView(this);
mImageView.setId(1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mImageView.setImageResource(R.drawable.ic_launcher);
mRelativeLayout.addView(mImageView, lp);
mScrollView = new ScrollView(this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, mImageView.getId());
mRelativeLayout.addView(mScrollView, lp2);
mRelativeLayout2 = new RelativeLayout(this);
int itemcounts = mRelativeLayout2.getChildCount();
mScrollView.addView(mRelativeLayout2);
TV1 = new TextView(this);
TV1.setId(2);
TV1.setTextSize(20);
TV1.setText("Im a Text View. The First");
mRelativeLayout2.addView(TV1);
B1 = new Button(this);
B1.setId(3);
RelativeLayout.LayoutParams bl1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl1.addRule(RelativeLayout.BELOW, TV1.getId());
B1.setText("Im a Button. The First");
B1.setTextSize(25);
mRelativeLayout2.addView(B1, bl1);
TV2 = new TextView(this);
RelativeLayout.LayoutParams tvl2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl2.addRule(RelativeLayout.BELOW, B1.getId());
TV2.setId(4);
TV2.setTextSize(20);
TV2.setText("Im a Text View. The Second");
mRelativeLayout2.addView(TV2, tvl2);
B2 = new Button(this);
B2.setId(5);
RelativeLayout.LayoutParams bl2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl2.addRule(RelativeLayout.BELOW, TV2.getI());
B2.setText("Im a Button. The Second");
B2.setTextSize(25);
mRelativeLayout2.addView(B2, bl2);
TV3 = new TextView(this);
TV3.setId(6);
RelativeLayout.LayoutParams tvl3 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl3.addRule(RelativeLayout.BELOW, B2.getId());
TV3.setTextSize(20);
TV3.setText("Im a Text View. The Third");
mRelativeLayout2.addView(TV3, tvl3);
B3 = new Button(this);
B3.setId(7);
RelativeLayout.LayoutParams bl3 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl3.addRule(RelativeLayout.BELOW, TV3.getId());
B3.setText("Im a Button. The Third");
B3.setTextSize(25);
mRelativeLayout2.addView(B3, bl3);
TV4 = new TextView(this);
TV4.setId(8);
RelativeLayout.LayoutParams tvl4 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl4.addRule(RelativeLayout.BELOW, B3.getId());
TV4.setTextSize(20);
TV4.setText("Im a Text View. The Fourth");
mRelativeLayout2.addView(TV4, tvl4);
if (itemcounts < 4) {
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
}
if (itemcounts > 4) {
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
}
}
}
try this

Android Relativelayout Programmatically

Basically I'm attempting to add rows to a table, I need to do this programmatically.
I can make it add the content I want, but not exactly how I want it.
For example I want a textview on the left with a button edittext button on the right.
Basically I want it like the image below:
.
Also I don't want to just and width to the textview.
Anyway here's what I've made so far:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btnOutstandingJob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Outstanding Job" />
<Button
android:id="#+id/btnVanStock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Van Stock" />
<Button
android:id="#+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Register User" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:text="Log Out" />
</RelativeLayout>
</LinearLayout>
Java:
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
TableRow row = new TableRow(vanstock.this);
RelativeLayout RowLayout = new RelativeLayout(vanstock.this);
RowLayout.setId(99);
TextView lblItem = new TextView(vanstock.this);
lblItem.setId(1);
lblItem.setText("ddfsgsdfgs");
RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL,
RelativeLayout.TRUE);
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
RelativeLayout.TRUE);
lblItem.setLayoutParams(lblitemParam);
RowLayout.addView(lblItem);
Button btnPlus = new Button(vanstock.this);
btnPlus.setId(4);
btnPlus.setText("+");
btnPlus.setWidth(40);
RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
RelativeLayout.TRUE);
btnPlus.setLayoutParams(btnPlusParams);
RowLayout.addView(btnPlus);
EditText txtItem = new EditText(vanstock.this);
txtItem.setId(3);
txtItem.setWidth(40);
RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId());
txtItem.setLayoutParams(txtItemParams);
RowLayout.addView(txtItem);
Button btnMinus = new Button(vanstock.this);
btnMinus.setId(2);
btnMinus.setText("-");
btnMinus.setWidth(40);
RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId());
btnPlus.setLayoutParams(btnMinusParams);
RowLayout.addView(btnPlus);
row.addView(RowLayout);
table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
I've tried searching etc., but I still can't get it to work correctly.. Any help would be appreciated!
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow) inflater.inflate(R.layout.table,
_tablelayout, false);
TextView textview = (TextView) row.findViewById(R.id.rowdata);
Like this way you can fullfill your requirement.Is this sufficient??
Best
V.k

Categories

Resources