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);
Related
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.
my xml is like follow I want to add image view and text view programmatically to given xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="#+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
but I want output like following programmatically ie a linear layout contain image view and old text view and add that linearlayout below to new textview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="25dip" >
<TextView
android:id="#+name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginBottom="12dip"
android:src="#drawable/app_icon_big" />
<TextView
android:id="#+text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dip"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outerLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="#string/app_name"
android:textSize="24.5sp" />
</LinearLayout>
MainActivity.java
public class MyActivity extends Activity {
private LinearLayout outerLinearLayout;
private TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outerLinearLayout = (LinearLayout) findViewById(R.id.outerLinearLayout);
text1 = (TextView) findViewById(R.id.text1);
LinearLayout innerLinearLayout = new LinearLayout(this);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin=12;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(R.drawable.ic_launcher);
innerLinearLayout.addView(imageView);
TextView textView = new TextView(this);
textView.setTextSize((float) 24.5);
textView.setText("New Text View");
textView.setLayoutParams(params);
outerLinearLayout.removeAllViews();
innerLinearLayout.addView(text1);
outerLinearLayout.addView(textView);
outerLinearLayout.addView(innerLinearLayout);
}
}
change inner layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip" >
By
<LinearLayout
android:id="#+id/internal_image_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="25dip"
android:visibility="gone" >
Then whenEver you want to show just
findViewById(R.id.internal_image_textView).setVisibility(View.VISIBLE)
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);