The call to getId() will return -1 when you are creating your views programmatically, as you are doing above. You must assign ID's to the objects with the setId() call first.
Note that the documentation for View says that you may specify any ID that you like -- it does not have to be unique within the View's hierarchy. -Nik On Jun 18, 8:31 pm, Jahir <[email protected]> wrote: > Hello, > I am trying to display two TextViews in two separate line. For that, I > am using RelativeLayout. I don't want to use XML for layout as I want > to bundle this Activity in a jar file which may be used by different > APKs. > > The TextViews are being overlapped instead of being displayed on two > separate lines. Any idea what I am doing wrong here? Sample code is > below. > > package test.RL; > > import android.app.Activity; > import android.os.Bundle; > import android.view.ViewGroup.LayoutParams; > import android.widget.RelativeLayout; > import android.widget.TextView; > > public class RelativeLayoutDemo extends Activity { > > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > doMyLayout(); > } > > protected void doMyLayout() { > RelativeLayout topLayout = new > RelativeLayout(getApplicationContext()); > TextView title = new TextView(getApplicationContext()); > TextView prompt = new TextView(getApplicationContext()); > title.setText("My title goes here"); > RelativeLayout.LayoutParams titleParams = new > RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, > LayoutParams.WRAP_CONTENT); > titleParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); > titleParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); > titleParams.addRule(RelativeLayout.ABOVE, prompt.getId()); > topLayout.addView(title, titleParams); > > prompt.setText("My prompt goes here too"); > RelativeLayout.LayoutParams promptParams = new > RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, > LayoutParams.WRAP_CONTENT); > promptParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); > promptParams.addRule(RelativeLayout.BELOW, title.getId()); > > topLayout.addView(prompt, promptParams); > setContentView(topLayout); > } > > > > } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

