how to show custom layout in my main layout? - android

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)

Related

Fields overlapping, creating layout programmatically

I have a layout XML and when I click on a floatbutton, I create a layout programmatically, so that the data of this layout is passed to this new one.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundnewlayout"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#drawable/img_interna" />
<TextView
android:id="#+id/txtTitle_avaliacao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Medicamento"
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/txtTitle_avaliacao"
android:layout_marginBottom="40dp"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/linearCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_margin="5dp"
android:elevation="7dp"
android:minHeight="200dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtNome_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Nome:"
android:textSize="10sp" />
<AutoCompleteTextView
android:id="#+id/edtNome_medicamento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNome_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="Paracetamol"
android:maxLines="1"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtNome_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Quantidade:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQtd_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="3"
android:maxLines="1"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/edtQtd_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Posologia:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPosologia_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="2 vezes ao dia a cada 8 horas"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fltNewLayout"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/cardView"
android:layout_marginRight="5dp"
android:layout_marginTop="170dp"
android:backgroundTint="#color/colorPrimaryDark"
android:clickable="true"
android:src="#drawable/icone_check"
app:fab_colorPressed="#color/colorPrimary"
app:rippleColor="#android:color/white" />
<LinearLayout
android:id="#+id/linearReceita"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fltNewLayout"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
</RelativeLayout>
When I click on the floatbutton, I create a new layout with the data of this layout that is fixed.
But, the fields of the layout created programmatically, overlap, how to fix?
The method below is as follows:
private void createNewLayout(){
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,15,5,5);
//linearLayout.setLayoutParams(params);
cardView = new CardView(getContext());
cardView.setLayoutParams(linearLayout.getLayoutParams());
cardView.setPadding(10,10,10,10);
cardView.setMaxCardElevation(6);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(linearLayout.getLayoutParams());
cardView.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
cardView.addView(txtNomeMedicamento);
linearLayout.addView(cardView);
}
I solved the issue by adding a new linear layout, this linear adds the textviews vertically, after adding these fields, it is inserted into the cardview.
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
Change the code below:
private void createNewLayout() {
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 5, 5);
cardView = new CardView(getContext());
cardView.setLayoutParams(params);
//cardView.setPadding(10, 10, 10, 10);
cardView.setMaxCardElevation(6);
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
cardView.addView(linearCard);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(params);
//cardView.addView(txtNomeTitle);
linearCard.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
txtNomeMedicamento.setPadding(10, 10, 10, 10);
// cardView.addView(txtNomeMedicamento);
linearCard.addView(txtNomeMedicamento);
//cardView.addView(linearLayout);
linearLayout.addView(cardView);
}
I was inserting the fields directly into the cardview, without a layout inside it, which made the fields overlap.

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

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

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.

How to add image view and text view to a linear layout

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)

Dynamically change the margins of RelativeLayout which is getting inflated via xml

I am tryin to add ten relativeLayouts dynamically by inflating the xml , but the relative layouts getting overlapped. how to change the margins of the inflating xml dynamically via code.
Below is the code reuse.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/aaa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ballaya" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:text="Srimannarayana earns Rs 8.65 cr on day 1"
android:textColor="#FFFFFF"
android:textSize="11sp"
android:textStyle="bold" />
<View
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#FFFFFF" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/popover_bg_rghtarw" />
</RelativeLayout>
code of item.xml
<?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"
android:background="#06516E" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#49C7F9">
<TextView
android:id="#+id/HeadLines"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latest News"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/newsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp" >
</RelativeLayout>
</RelativeLayout>
Java Code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
newsLayout =(RelativeLayout)findViewById(R.id.newsLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout row[] = new RelativeLayout[10];
TextView text[]= new TextView[10];
for(int i=0;i<10;i++){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
row[i] = (RelativeLayout) inflater.inflate(R.layout.reuse,null);
params.setMargins(0, 50, 0, 0);
newsLayout.addView(row[i],params);
text[i]= (TextView) row[i].findViewById(R.id.text);
text[i].setText("AAAAAAA");
}
You can use LinearLayout instead of RelativeLayout for newsLayout and make the orientation as VERTICAL.
And instead of newsLayout =(RelativeLayout)findViewById(R.id.newsLayout);
write: newsLayout =(LinearLayout)findViewById(R.id.newsLayout);
And rest of the code will be as it is.
This will solve your problem.

Categories

Resources