How do I create this XML programatically from my Activity? - android

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

Related

Setting stacked up layout programmatically in 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);

How can I programatically create a ScrollView with RelativeLayout boxes like this?

I am trying to create an Activity with a ScrollView in it. In the ScrollView will be "boxes" (sort of like a button) that is just a bit smaller than the width of the parent view and spaced slightly apart vertically. Because the boxes will have a couple different size text in a couple areas on them, I think I need to make the out of RelativeLayouts. (I would post a picture but can't with my reputation point so hopefully this makes sense).
I tried create an XML for the activity and than programatically adding the relativeviews. I got it to work, sort of, but am having trouble with the box spacing.
My test XML looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scrolling section -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
My Activity looks like this...
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
I get the scrollable section, no problem. Problem is my blue "boxes" appear to be touching one another and I can't figure out how to make them look like buttons with padding left and right and between them. I tried
rlView.setPadding(20,20,20,20);
right after the setBackground color. All that appears to do is pad the text... not the RelativeViews.
Any suggestions no how to get this to work?
Thanks
===========
NEW INFORMATION:
Here is some additional information after following some suggestions and trial an error. I was also able to post a pic for easier reference.
===========
What I am trying to achieve, programmatically, is something that looks like this.
I achieved this example using the straight XML which looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
// individual groups start here - this section will be controlled by a database
// #1 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp1"
>
<TextView
android:id="#+id/text1"
android:text="Item #1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext1"
android:text="123.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp" />
</RelativeLayout>
// #2 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp2"
>
<TextView
android:id="#+id/text2"
android:text="Item #2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/leftext2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext2"
android:text="456.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I took this apart and created an XML file and and activity that will programmatically create the middle groups.
The modified XML (activity_main2) looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
I started rewriting my activity code to test some of the suggestions. It currently looks like this...
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(10,10,10,0);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
// other textviews will go here
llGroups.addView(rlView,rlp);
}
}
}
This compiles and runs, but the setMargins is not working. My screen has the "UPDATE ALL" button at the top like it should and (19) "Text #" with a blue background, but they are full width and touching one another so it is a solid blue background.
I am getting close, but not sure how to get the margins to set correctly for the rlView relativeview (not the textviews).
Hopefully this helps explain what I am seeing now..
SOLUTION
After suggestions, and some trial and error... here is what I had to do to achieve the desired layout in case someone else is interested.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.BLUE);
RelativeLayout rl = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setPadding(5, 5, 5, 5);
tv.setText("Test #" + i);
tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(5, 5, 5, 5);
tv2.setText(String.valueOf(i));
tv2.setTextSize(30);
tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, tvp);
rl.addView(tv2,tvp2);
ll.addView(rl);
llp.setMargins(32,16,32,16);
mTvContainer.addView(ll,llp);
}
}
For each entry in the FOR, I had to use a LINEARLAYOUT to be able to set the margins properly, and in that LINEARLAYOUT I had to use a RELATIVELAYOUT to use the addRules to get the proper positioning of the TextViews.
If there is an easier way, let me know. Thanks for everyones help!!
You are using too much nested layouts needed, here is stripped version.
xml:
<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=".MainActivity">
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Activity code:
mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.BLUE);
tv.setTextColor(Color.WHITE);
tv.setPadding(p, p / 2, p, p / 2);
tv.setText("TextView " + i);
TextView tv2 = new TextView(this);
tv2.setBackgroundColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(p, p / 2, p, p / 2);
tv2.setText("TextView " + i);
ll.addView(tv);
ll.addView(tv2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(p, p / 2, p, p / 2);
mTvContainer.addView(ll, params);
}

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

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

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

Categories

Resources