Dynamic UI - How to add 3 linearlayouts into a parent linearlayout - android

I'm having a little bit of trouble with creating a dynamic UI.
I can get it to work with just one LinearLayout but I have no idea how to add more.
This is what I have now, it's one LinearLayout with 3 buttons.
Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
maakLayout();
}
private void maakLayout() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
setContentView(linearLayout);
for (int i = 0; i < 3; i++) {
Button button = new Button(this);
button.setText("Test");
button.setTag(i);
button.setBackgroundResource(R.drawable.border_red);
LayoutParams layoutTextParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// LayoutParams layoutImageParams =
// new LayoutParams(100,100);
layoutTextParams.topMargin = 5;
layoutTextParams.leftMargin = 5;
layoutTextParams.rightMargin = 5;
button.setLayoutParams(layoutTextParams);
linearLayout.addView(button);
}
}
What I need is the following:
And the static XML code for this is the following:
<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:orientation="horizontal"
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="com.example.test.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="144dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/border_blue"
android:text="Test" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border_red"
android:text="Test" />
</LinearLayout>
</LinearLayout>
Once I know how to add other linearlayouts I'm good I think, thank you for your help.

LinearLayout is itself a subclass of View, so you can add it as a child to other LinearLayouts.
Like so:
LinearLayout horizontalLayout = new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout verticalLayout1 = new LinearLayout(this);
verticalLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout verticalLayout2 = new LinearLayout(this);
verticalLayout2.setOrientation(LinearLayout.VERTICAL);
horizontalLayout.addView(verticalLayout1);
horizontalLayout.addView(verticalLayout2);
Just populate your first and third vertical layout with buttons as you do in your loop.
Hope this helps.

Related

Make Linear Layout not exit the screen

I'm fairly new to android and xml layouting. I've been trying to make an app, and already done with the programming bit. However i'm stuck on the finalisation of the layout. Really need a hand. My app should look like this:
I've divided the screen in 4 Linear Layouts as I have numbered in the picture as seen below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.FirstVersion.android.cwd.Play"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/material_blue_grey_800">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnGoBack"
android:text="Go Back"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Information"
android:background="#color/accent_material_dark"
android:layout_weight="10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LDrawerAction"
android:text="Menu"
android:clickable="false"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp" />
</LinearLayout>
//endregion
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Section2"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Section3"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="5sp"
android:textColor="#color/colorWhite"
android:background="#990000"
android:maxLines = "3"
android:scrollbars = "vertical"
android:id="#+id/txtSection3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15"
android:orientation="vertical"
android:baselineAligned="false">
<TableLayout
android:id="#+id/Section4"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
The problem now is that, the second section is filled programmatically and it has a range of rows from 8 to 14. The same for the columns. Every block is a different button which I add with this code:
Lay= (LinearLayout)findViewById(R.id.Section2);
for (int row = 0; row < numberOfRows; row++)
{
LinearLayout Linearrow = new LinearLayout(this);
Linearrow.setOrientation(LinearLayout.HORIZONTAL);
for (int column = 0; column < numberOfColumns; column++)
{
View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(params);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lp.weight = 1.0f;
Linearrow.addView(a, lp);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lp.weight = 1.0f;
Lay.addView(Linearrow, lp);
}
I have tried even using a TableLayout and assigning a small image as background so it could get resized with the following code:
TL =(TableLayout)fa.findViewById(R.id.Section2);
for (int i = 0; i < numberOfRows; i++)
{
TableRow tableRow = new TableRow(c);
for(int j = 0; j < numberOfColumns; j++)
{
String s = T[(i * 10) + j];
Block b = new Block(c, s);
//int w = Math.round(c.getResources().getDisplayMetrics().density * 20);
//int h = Math.round(c.getResources().getDisplayMetrics().density * 80);
//b.setLayoutParams(new TableRow.LayoutParams(w, h, 2.0f));
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
b.setBackgroundResource(R.drawable.g_3);
tableRow.addView(b);
tableRow.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5f));
final Block fb = b;
}
TL.addView(tableRow);
}
When I run the app, I can see that unless I open it on a big screen phone, the content is not visible entirely(as if the height is set fixed manually of the layouts), and since it is not scroll-able, the content is not visible. How can I rearrange or modify the layout so it looks as in the picture above?
Make fixed height of layouts using layout_weight
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:weightSum="12"
android:orientation="horizontal"
android:background="#color/material_blue_grey_800">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnGoBack"
android:text="Go Back"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Information"
android:background="#color/accent_material_dark"
android:layout_weight="10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LDrawerAction"
android:text="Menu"
android:clickable="false"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:weightSum="1"
android:id="#+id/Section2"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:id="#+id/Section3"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="5sp"
android:textColor="#android:color/white"
android:background="#990000"
android:maxLines = "3"
android:scrollbars = "vertical"
android:id="#+id/txtSection3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:weightSum="15"
android:orientation="vertical"
android:baselineAligned="false">
<TableLayout
android:id="#+id/Section4"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
And in your java class where you add views dynamically, calculate the weights of children accordingly as given below
Lay= (LinearLayout)findViewById(R.id.Section2);
Lay.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int totalWidth = Lay.getMeasuredWidth();
float rowWeight = 1 / numberOfRows;
int columnWidth = totalWidth / numberOfColumns;
for (int row = 0; row < numberOfRows; row++)
{
LinearLayout Linearrow = new LinearLayout(this);
Linearrow.setOrientation(LinearLayout.HORIZONTAL);
for (int column = 0; column < numberOfColumns; column++)
{
View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined
// height is determined by row's layout weight
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
a.setLayoutParams(params);
Linearrow.addView(a);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lp.weight = rowWeight;
Lay.addView(Linearrow, lp);
}
I've not tested these, but just might work with few changes that you can figure out once you run this code.
Use
android:layout_weight
attribute to arrange in equal parts in linear layout.
See in the Link
It is better to use View inside ScrollView. Use the below code:-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.FirstVersion.android.cwd.Play"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/material_blue_grey_800">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnGoBack"
android:text="Go Back"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Information"
android:background="#color/accent_material_dark"
android:layout_weight="10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LDrawerAction"
android:text="Menu"
android:clickable="false"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp" />
</LinearLayout>
//endregion
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Section2"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Section3"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="5sp"
android:textColor="#color/colorWhite"
android:background="#990000"
android:maxLines = "3"
android:scrollbars = "vertical"
android:id="#+id/txtSection3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15"
android:orientation="vertical"
android:baselineAligned="false">
<TableLayout
android:id="#+id/Section4"
android:layout_width="match_parent"
android:layout_weight="15"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Create multiple linear layouts and text views programmatically

Create multiple linear layouts and text views programmatically
I have tried to explain please see the code of xml file to understand the question
See image for my desired output
The xml code below is what I want exactly but programmatically
<RelativeLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
</LinearLayout>
</RelativeLayout
I want to achieve the same output but programmatically
Try This
add LinearLayout into your xml
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
</LinearLayout>
and change in your Java file like this
LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
for(int i= 0; i <5 ;i++) {
LinearLayout parent = new LinearLayout(Main.this);
LinearLayout.LayoutParams param= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(Main.this);
tv.setText("T1");
TextView tv2 = new TextView(Main.this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
ll_main.addView(parent);
}
i just got my answer and its below
LinearLayout ll_main = (LinearLayout) findViewById(R.id.linear);
ll_main.setVisibility(View.VISIBLE);
int j = 5;
final LinearLayout[] linearlayout = new LinearLayout[j];
for (int i = 0; i < j; i++) {
LinearLayout parent = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("T1");
TextView tv2 = new TextView(this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
linearlayout[i] = parent;
ll_main.addView(parent);
}
}
}

How add LinearLayout and RelativeLayout programmatically in a ScrollView?

How I can add LinearLayout and RelativeLayout programmatically in a ScrollView :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1.5"
android:background="#drawable/bottom_conversation">
<TextView
android:id="#+id/isTyping"
android:layout_width="0dip"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:hint="Chat" />
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="#string/edt_Conversation" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/someID"
android:layout_width="0dip"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:background="#drawable/background_selector"
android:contentDescription="#null"
android:layout_marginTop="5dp"
android:src="#drawable/social_send_now" />
</LinearLayout>
</LinearLayout>
Notice : I need that RelativeLayout insert in LinearLayout
TextView tv = new TextView(this);
tv.setText("Testsssssssssssssss");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setId(1);
RelativeLayout relativeLayout = new RelativeLayout(this);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(tv,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
LinearLayout firstLinearLayout= new LinearLayout(this);
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
ScrollView sclView = (ScrollView) findViewById(R.id.scrollID);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Good Luck...
You can add Views ( LinearLayout, RelativeLayout... ) with
scrollView.addView(newLinearLayoutView);

how to show custom layout in my main layout?

im using custom listview in my main layout from here https://github.com/NikolaDespotoski/DoubleTapListViewHandler/tree/master/src/com/nikola/despotoski/doubletaplistview but problem is this wil create custom programicaly layout but in my code i already set content setContentView(R.layout.bid); so how do i add new custom layout in my main layout?? help me what will i do??
private CustomListView bidlist = null;
public class BidSchema extends BackBaseActivity implements OnItemDoubleTapLister {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bid);
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
back = (ImageView) findViewById(R.id.bidback);
bottomlayout = (RelativeLayout)findViewById(R.id.bottom_layout);
scroll_down = (ImageView) findViewById(R.id.down);
scroll_up = (ImageView) findViewById(R.id.up);
Layout XML
<?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="wrap_content"
android:background="#drawable/bg2" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_bar1"
android:contentDescription="#string/contentdesc" />
<ImageView
android:id="#+id/bidback"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="3dp"
android:background="#drawable/back_arr"
android:contentDescription="#string/contentdesc" />
<ImageView
android:id="#+id/iconpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="4dp"
android:layout_centerHorizontal="true"
android:background="#drawable/treasure"
android:contentDescription="#string/contentdesc" />
<TextView
android:id="#+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:background="#drawable/bar"
android:contentDescription="#string/contentdesc"
android:gravity="center"
android:text="Bid"
android:textColor="#000000"
android:textSize="22sp" />
<LinearLayout
android:id="#+id/jobhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bar"
android:layout_marginTop="10dp"
android:layout_marginLeft="10sp"
android:orientation="horizontal" >
<TextView
android:id="#+id/jobDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:textColor="#ffffff"
android:textSize="22sp"
android:gravity="left"
android:text="Plot"
android:textStyle="bold" />
<TextView
android:id="#+id/jobpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="right"
android:paddingRight="50dp"
android:text="Job"
android:textColor="#ffffff"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/bidlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/jobhead"
android:layout_marginBottom="10dp"
android:layout_above="#+id/bottomBar"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/fields_bg"
android:visibility="visible" >
</ListView>
<include
android:id="#+id/bottomBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
layout="#layout/bottom" />
</RelativeLayout>
If you have implement custom Listview then you should change this.
setContentView(R.layout.bid);
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
to
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
UPDATE:
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);
bidlist = new CustomListView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootLayout.addView(bidlist, rlp);
Because you can't call setContentView() two times. So you have to add it in your parent Relative Layout. so check that.
Give an id to your parent RelativeLayout as below to add new view to this layout later...
<?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="wrap_content"
android:background="#drawable/bg2"
android:id="#+id/layout" >
..........
</RelativeLayout >
Now, add your bidlist to the parent layout as below...
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);
bidlist = new CustomListView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootLayout.addView(bidlist, lp);
You don't setContentView.
If main activity show posted xml, just make Adapter implements baseAdapter, and then just listview.setAdapter( you make adapter)

Align and Clickable Linear layout Programmatically?

currently my code display linearlayout
but i want to display it this way
here i attach my code please try to help
public class MainActivity extends Activity
{
LinearLayout MainLinearObj;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainLinearObj = (LinearLayout) findViewById(R.id.MainLinear);
for(int i = 0; i < 3; i++)
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
llp.setMargins(10, 10, 10, 10);
llp.weight = 1;
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
ll.setId(i);
MainLinearObj.addView(ll,llp);
}
}
}
edited code for user1566160 check it and see its output
if I am enter 10 in edittext then i need total 10 square as output per row has 3 square needed
public class MainActivity extends Activity
{
LinearLayout MainLinearObj;
EditText et;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainLinearObj = (LinearLayout) findViewById(R.id.MainLinear);
et = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
// String str = et.getText().toString();
// int no = Integer.parseInt(str);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
String str = et.getText().toString();
int no = Integer.parseInt(str);
// TODO Auto-generated method stub
for(int j=0;j<no;j++)
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
llp.setMargins(10, 10, 10, 10);
llp.weight = 1;
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LinearLayout newLinearLayout = new LinearLayout(MainActivity.this);
newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
newLinearLayout.setLayoutParams(llp1);
for(int i = 0; i < 3; i++)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
ll.setId(i);
newLinearLayout.addView(ll,llp);
}
MainLinearObj.addView(newLinearLayout);
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="3" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Selected Table "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=":-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblChkTabDisSelTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Cover Require"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=":-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblChkTabDisReqCover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:id="#+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Selected Cover"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text=":-"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblChkTabDisToCover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btnChkTabAll"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="All" />
<Button
android:id="#+id/btnChkTabAC"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="A/c" />
<Button
android:id="#+id/btnChkTabNAC"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="3"
android:text="Non A/c" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/lblChkTabDiaName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblChkTabDiaCover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table Cover"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblChkTabDiaNote"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table Note"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/fglblDiaState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Remark"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/llChkTabDisp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="4" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="6" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="4" >
<Button
android:id="#+id/btnChkTabDiaOk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:text="Ok" />
<Button
android:id="#+id/btnChkTabDiaCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout><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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="#+id/MainLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
</LinearLayout>
</LinearLayout>
You can try something like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout MainLinearObj = (LinearLayout) findViewById(R.id.abc);
for(int j=0;j<3;j++)
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
llp.setMargins(10, 10, 10, 10);
llp.weight = 1;
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LinearLayout newLinearLayout = new LinearLayout(this);
newLinearLayout.setOrientation(LinearLayout.VERTICAL);
newLinearLayout.setLayoutParams(llp1);
for(int i = 0; i < 3; i++)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
ll.setId(i);
newLinearLayout.addView(ll,llp);
}
MainLinearObj.addView(newLinearLayout);
}
}
Edit:
Set the Main Linear Layout as Vertical orientation in XML and then use the following code:
LinearLayout MainLinearObj = (LinearLayout) findViewById(R.id.abc);
for(int j=0;j<10;j++)
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
llp.setMargins(10, 10, 10, 10);
llp.weight = 1;
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LinearLayout newLinearLayout = new LinearLayout(this);
newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
newLinearLayout.setLayoutParams(llp1);
for(int i = 0; i < 3; i++)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
ll.setId(i);
newLinearLayout.addView(ll,llp);
}
MainLinearObj.addView(newLinearLayout);
}
Edit2
int no = Integer.parseInt(str);;
int count = 0;
int myvar = (no/3) +1;
for(int j=0;j<myvar;j++)
{
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
llp.setMargins(10, 10, 10, 10);
llp.weight = 1;
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LinearLayout newLinearLayout = new LinearLayout(this);
newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
newLinearLayout.setLayoutParams(llp1);
for(int i = 0; i < 3; i++)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
ll.setId(i);
count++;
if(count<=no)
newLinearLayout.addView(ll,llp);
}
MainLinearObj.addView(newLinearLayout);
if(count==no) {
break;
}
}

Categories

Resources