i have in xml tis code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/examp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/examp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/examp1" />
</RelativeLayout>
I want to create these buttons programmatically and i use:
RelativeLayout mainLayout;
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
for (int i=0; i<1; i++)
{
Button new_button = new Button(this);
new_button.setText("blabla");
new_button.setId(i);
new_button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mainLayout.addView(new_button);
}
How can I translate this command android:layout_below="#id/examp1" programmatically?
Use RelativeLayout.LayoutParams. Use addRule(), for your example use it with RelativeLayout.BELOW and the id (R.id.examp1)
Related
I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?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_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column
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);
}
I need some help I have a scrollview that shows some question with radio buttons and I want to add a button when the user reaches the last question and I want this button to be available only after the user has selected an answer to all the questions.
This is my code so far but it is not displaying anything. I can display the questions without the button but I want to display the button too.
String countryName[] = { "India", "Pakistan", "China", "Nepal",
"Bangladesh" };
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
for (int k = 1; k <= 10; k++)
{
//create text button
TextView title = new TextView(this);
title.setText("Question Number:" + k);
title.setTextColor(Color.BLUE);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < 5; i++)
{
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(countryName[i]);
}
mLinearLayout.addView(rg);}
ScrollView scroll = new ScrollView(this);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
mLinearLayout.addView(scroll);
mLinearLayout.addView(btn);
setContentView(mLinearLayout);
}
The XML file:
<!--?xml version="1.0" encoding="utf-8"?-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:text="Continue" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</RelativeLayout>
mLinearLayout.addView(btn);
scroll.addView(mLinearLayout);
setContentView(scroll);
You should place your LinearLayout inside the ScrollView, because ScrollView is a container and its children are scrollable. Also, set the ScrollView as the activity's content view and define its layout params.
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
scroll.addView(mLinearLayout);
setContentView(scroll);
Hi I have found the right way to do it! The easiest way is to fix the XML file and add a LinearLayout within the LinearLayout and put the questions within the inner LinearLayout and the button in the outside LinearLayout.
Here is the trick:
<!--?xml version="1.0" encoding="utf-8"?-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.87"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.87"
android:text="Button" />
</LinearLayout>
</ScrollView>
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 );
I hope question title gives you a good description of the problem.
I want to create this XML, but programatically (please don't suggest not doing it programmatically ^_^ )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Messages:"/>
<EditText android:id="#+id/messageHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:layout_weight="1"
android:editable="false"
android:gravity="top"
android:scrollbars="vertical"
android:scrollbarSize="10px"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<EditText android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight="1"
/>
<Button android:id="#+id/sendMessageButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="Send"/>
</LinearLayout>
As you can see, there are two LinearLayout's, one embedded in the other. I need to reproduce this and then add it to a ViewFlipper.
This is what I have so far:
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
Button btn=new Button(this);
EditText messageHistory = new EditText(this);
EditText newMessage = new EditText(this);
TextView windowTitle = new TextView(this);
btn.setText("Send");
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
}
});
windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
windowTitle.setPadding(0, 5, 0, 10);
windowTitle.setText("Chat with: ");
messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
messageHistory.setGravity(Gravity.TOP);
messageHistory.setMovementMethod(new ScrollingMovementMethod());
messageHistory.setClickable(false);
messageHistory.setFocusable(false);
messageHistory.setPadding(0, 0, 0, 5);
newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
newMessage.setGravity(Gravity.TOP);
newMessage.setMovementMethod(new ScrollingMovementMethod());
newMessage.requestFocus();
l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l1.addView(windowTitle);
l1.addView(messageHistory);
l2.addView(newMessage);
l2.addView(btn);
l1.addView(l2);
flipper.addView(l1);
Where flipper is defined as:
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);
Without a problem, I can see "l1" in the window when it loads up, but l2 is no where to be found. Did I mess up with LayoutParams? Can I use addView to add a LinearLayout?
I think you forgot to set layout weight (only my opinion though, could be wrong).
because you are adding more views with layout-height set to FILL_PARENT