I want to implement the following layout in code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget92"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/widget164"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignTop="#+id/widget163"
android:layout_alignLeft="#+id/widget161" />
<Button
android:id="#+id/widget163"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignTop="#+id/widget162"
android:layout_toLeftOf="#+id/widget161" />
<Button
android:id="#+id/widget162"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="#+id/widget161"
android:layout_toLeftOf="#+id/widget160" />
<Button
android:id="#+id/widget161"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/widget160" />
<Button
android:id="#+id/widget160"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/widget159" />
<Button
android:id="#+id/widget159"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
Can anyone tell me how to implement this in code.
Thanx in advance.
You can create all UI elements manually with the same hierarchy as in layout xml file.
Say in Activity.onCreate()
onCreate(...) {
super.onCreate(...);
RelativeLayout layout = new RelativeLayout(...);
Button button = new Button(...);
layout.addView(button);
setContentView(layout);
}
RelativeLayout rl = new RelativeLayout(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rl.setId(R.id.widget92);
Button btn = new Button(this);
btn.setId(R.id.widget164);
btn.setText("Text");
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, R.id.widget163);
lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.widget161);
rl.addView(btn, lp);
btn = new Button(this);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
...
setContentView(rl);
Related
I am trying to inflate a layout which contains a few textViews. When I add that to a tableRow, the textViews inside that layout are getting overlapped. In another words, the layout_width and layout_height are being ignored I guess.
But when I add rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); after inflating, nothing is appearing.
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.layout_result_row, null);
rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
layout_result_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/results_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp">
<TextView
android:id="#+id/results_row_match_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_match_no"
android:layout_alignParentEnd="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_teams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/results_row_match_no"
android:text="TextView" />
<TextView
android:id="#+id/results_row_turn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_turn"
android:layout_toStartOf="#+id/results_row_turn"
android:text="TextView" />
<TextView
android:id="#+id/results_row_toss_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/results_row_match_no"
android:layout_marginTop="22dp"
android:text="TextView" />
<TextView
android:id="#+id/results_row_home_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/results_row_toss_ml"
android:layout_alignBottom="#+id/results_row_toss_ml"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="#+id/results_row_rank_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/results_row_home_ml"
android:layout_toEndOf="#+id/results_row_turn_text"
android:text="TextView" />
<TextView
android:id="#+id/results_row_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/results_row_turn_text"
android:layout_alignStart="#+id/results_row_home_ml"
android:text="TextView" />
</RelativeLayout>
The inflated layout already has the Layout Params all the chid views are not redraw with the new Layout params. If you want to have the views same as before after you inflate you can use the below code.
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setBackgroundColor(Color.RED);
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.activity_main, null);
tableLayout.setStretchAllColumns(true);
// rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
setContentView(tableLayout);
Instead of:
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
Try:
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
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.
I am having trouble generating below mentioned android layout with code .
<RelativeLayout
android:id="#+id/profile_info_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/ProfileTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D9D9D9"
android:baselineAligned="false"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.5"
android:background="#F3F3F3"
android:orientation="horizontal" >
<TextView
android:id="#+id/gender_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:textColor="#color/info_left"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:background="#color/white"
android:orientation="horizontal" >
<TextView
android:id="#+id/profile_info_text_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
i tried alot of things from google and blogs but could not end up with this layout
any help will highly be appreciated and will be admired
thanks
Try this..
LinearLayout first_lay = new LinearLayout(this);
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
first_lay.setOrientation(LinearLayout.HORIZONTAL);
first_lay.setLayoutParams(lp_icon);
LinearLayout left_lay = new LinearLayout(this);
LinearLayout.LayoutParams left_icon = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
left_lay.setGravity(Gravity.CENTER);
left_lay.setBackgroundColor(Color.parseColor("#696969"));
left_lay.setLayoutParams(left_icon);
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
TextView text1 = new TextView(this);
left_lay.addView(text1);
text1.setLayoutParams(tests);
text1.setText("Left Text");
text1.setTextSize(18);
first_lay.addView(left_lay);
LinearLayout right_lay = new LinearLayout(this);
LinearLayout.LayoutParams right_icon = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
right_lay.setGravity(Gravity.CENTER);
right_lay.setBackgroundColor(Color.parseColor("#D9D9D9"));
right_lay.setLayoutParams(right_icon);
TextView text2 = new TextView(this);
right_lay.addView(text2);
text2.setLayoutParams(tests);
text2.setText("Right Text");
text2.setTextSize(18);
first_lay.addView(right_lay);
Why not do this:
RelativeLayout profile_info_gender = new RelativeLayout(your activity);
LinearLayout ProfileTable = new LinearLayout(your activity);
profile_info_gender.addView(ProfileTable);
For orientation, use LayoutParams.
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)
i have following layout,i want to set margin programmaticaly . This layout i am accessing this layout in baseadpter. How can i access it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="20dp"
android:layout_height="17dp"
android:button="#drawable/my_custom_checkbox"
android:id="#+id/check"
android:layout_marginTop="5dip"
android:checked="false"
>
</CheckBox>
<RelativeLayout
android:id="#+id/Relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
android:layout_weight="0.5">
<TextView android:id="#+id/list_item_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="5dip"
android:layout_marginBottom="15dip"
android:ellipsize="marquee"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceSmall"
android:fadingEdge="horizontal" />
//Want to access this layout
<LinearLayout
android:id="#+id/LinearCal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dip" to 100dip
here is your code..
LinearLayout ll = (LinearLayout) findViewbyid(R.id.LinearCal);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 100, 0, 0);//set margin left, top, right, bottom
ll.setLayoutParams(layoutParams);
Use the findViewByID method.
http://developer.android.com/reference/android/view/View.html#findViewById(int)
then access its layoutparams
Try this way.
//LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 10, 0, 0);
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearCal);
layout.setLayoutParams(layoutParams);