0

I have a custom layout java file that I create and add views to like this:

 PredicateLayout layout = new PredicateLayout(this);

    for (int i = 0; i < someNumberThatChangesEveryTime; i++) 
    {            
        TextView t = new TextView(this);
        t.setText("Hello");
        t.setBackgroundColor(Color.RED);
        t.setSingleLine(true);
       layout.addView(t, new PredicateLayout.LayoutParams(2, 0));
    }
 setContentView(layout);

What I would like to do is define my TextView in an xml, so I can add it like so:

TextView t = (TextView) findViewById(R.id.textview);

And of couse also add other kinds of views. I have no idea how to write this xml file, or how to connect it to this activity. The layout I'm using is this one: Line-breaking widget layout for Android

Community
  • 1
  • 1
Rickard
  • 1,289
  • 1
  • 17
  • 27
  • Read this : http://developer.android.com/guide/topics/ui/declaring-layout.html and http://mobile.tutsplus.com/tutorials/android/android-layout/ – Gaurav Arora Apr 22 '13 at 09:04

1 Answers1

1

Find the id of the root layout in xml and add other ui elements programatically to the root layout.

activity_main.xml

<RelativeLayout 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"
andorid:id="@+id/ll
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Blank text" />
 </RelativeLayout>

Your MainActivity

 public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
           RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
            TextView tv= (TextView) findViewById(R.id.tv);
            // add other ui elements to the root layout ie Relative layout  


     }
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • What do you mean by root layout? The thing is, the entire layout is in PredicateLayout.java, so there is no xml file yet. – Rickard Apr 22 '13 at 09:20
  • 1
    @Rickard you need to set the layout from xml file and then get the id of the root layout in xml . Add other ui elements to your root layout programatically. If you want to find id of textview you must set the layout(xml) to the screen first. – Raghunandan Apr 22 '13 at 09:23
  • Ok. So is there a way to use the layout in the first answer of this http://stackoverflow.com/questions/549451/line-breaking-widget-layout-for-android instead of a RelativeLayout? Something like – Rickard Apr 22 '13 at 09:32
  • 1
    you can define your custom view and inflate the same http://stackoverflow.com/questions/3260876/custom-view-in-xml-layout. Heres's another link http://jeffreysambells.com/2010/10/28/custom-views-and-layouts-in-android-os-sdk – Raghunandan Apr 22 '13 at 09:35