1

I have a very basic program which displays a standard Textview and a extended Textview. The extended ones are to be inflated into the main Layout. Now every time I add a new extended textview, it appears fine however I also want some gap between them. This probably means I'll have to add margins to my xml file. Can anyone please assist me how I can go about achieving this?

EDIT : I tried adding the appropriate tags(android:layout_marginBottom="25dip") in my main layout's file but the gap just wouldn't show. Kindly check my source codes and help me with this. Regards

Here is the source code of my program :

ExpenseWatchActivity.java(This is the main Activity)

package com.app.expensewatch;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
import android.view.LayoutInflater;
import android.content.Context;


public class ExpenseWatchActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    LinearLayout ll = (LinearLayout)findViewById(R.id.mainlayout);

    int day,mon,yr;

    Calendar cal = Calendar.getInstance();

    day = cal.get(Calendar.DATE);
    mon = 1 + cal.get(Calendar.MONTH);
    yr = cal.get(Calendar.YEAR);

    String text = getResources().getString(R.string.hello, day , mon , yr);

    TextView tx1 = (TextView)findViewById(R.id.text1);
    tx1.setText(text);

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    List1 list = (List1)inflater.inflate(R.layout.list1, null);
    list.setText(text);
    ll.addView(list);

    List1 list2 = (List1)inflater.inflate(R.layout.list1, null);
    list2.setText(text);
    ll.addView(list2);

    }
} 



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 

    android:id="@+id/mainlayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

     <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    </LinearLayout>



list1.xml

<?xml version="1.0" encoding="utf-8"?>


<com.app.expensewatch.List1

        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list11"
        android:background="@color/solid_red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

/>

1 Answers1

0

When you create the view by inflation you will have to call setMargin() on the LayoutParams object your view uses.

List1 list = (List1)inflater.inflate(R.layout.list1, null);
list.setText(text);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
list.setLayoutParams(lp);
ll.addView(list);
prometheuspk
  • 3,754
  • 11
  • 43
  • 58
  • Hi, I followed your suggestion but I'm not getting what I wanted. After editing as per your post, all that is happening is that list and list2 are getting resized in a weird fashion but no gap is appearing in between them(which is what I want..) – user1455536 Aug 07 '12 at 14:37
  • I'll look into it, please give me some time. – prometheuspk Aug 07 '12 at 18:05
  • Just checked. By my method it's working just fine. But i read your question again. where exactly did you place the `marginBottom` attribute? in the `linearlayout`? that won't work cuz the margin applies to the widget and is not inherited by children. So programmatically is the way to go. Are there any color issues? i mean that it could be that the background color isn't letting you see the change probably. you did place values in the `setMargins()` method right? – prometheuspk Aug 07 '12 at 18:44
  • Ok, I'll try it and let you know. By the way, can you please get a screenshot of the output screen? – user1455536 Aug 07 '12 at 20:02
  • I'll send you complete package by morning. – prometheuspk Aug 07 '12 at 21:01
  • Please don't forget to send me the package. It's very important for me. Greetingz – user1455536 Aug 08 '12 at 08:14
  • gonna need a contact from you. – prometheuspk Aug 08 '12 at 08:22