Skip to main content

Programmatically Position items in activity. NO XML



I have an Activity that has all the display elements added dynamically. Theres no xml for the acvtivity at all.





The Activity consists of the following controls:





  1. RelativeLayout (Layout object that all the child views sit in)



  2. TextView (Title for the page, sits at top of the RelativeLayout)



  3. ScrollView (Scrollable area that holds all the data controls)



  4. LinearLayout (Layout object to hold the activity buttons)







I want to know how it is possible to define that the ScrollView sits below the Title TextView and above the LinearLayout button holder where the LinearLayout is set to the Activity Page bottom





I have tried using RelativeLayout.LayoutParams to set up rules but cannot seem to understand the way to do it. Any help or links to tutorials would be apreciated





I have included my Code to see if someone can help







// declare the items for display

RelativeLayout baseLayout = new RelativeLayout(this);

// add the customer name and number field.

// NOTE: We will always require this widget regardless of dialog design fields

tvCustomerNameNumber = new TextView(this);

tvCustomerNameNumber.setTextSize(20);

tvCustomerNameNumber.setText("Customer Name & Number");



// build up the linear layout of controls

LinearLayout ll = new LinearLayout(this);

ll.setOrientation(LinearLayout.VERTICAL);



// Scroll view.

// NOTE: We will always need this widget to enable us to scroll the page

// if too many items are added for the display size

ScrollView sv = new ScrollView(this);

sv.addView(ll);



// buttons

LinearLayout buttons = new LinearLayout(this);

buttons.setOrientation(LinearLayout.HORIZONTAL);



// button edit

Button edit = new Button(this);

edit.setId(EDIT_BUTTON_ID);



// button save

Button save = new Button(this);

save.setId(SAVE_BUTTON_ID);



// button cancel

Button cancel = new Button(this);

cancel.setId(CANCEL_BUTTON_ID);



// add each button to the button layout

buttons.addView(edit);

buttons.addView(save);

buttons.addView(cancel);



// Scroll view Layout parameters

RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

scrollParams.addRule(RelativeLayout.BELOW, tvCustomerNameNumber.getId());

scrollParams.addRule(RelativeLayout.ABOVE, buttons.getId());



// buttons Layout parameters

RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

buttonParams.addRule(RelativeLayout.BELOW, sv.getId());



// add the customer name number field to the base layout

baseLayout.addView(tvCustomerNameNumber);

// add the scroll view to the base layout

baseLayout.addView(sv); //, scrollParams);

// add the buttons to the base layout

baseLayout.addView(buttons, buttonParams);



// set the content view

this.setContentView(baseLayout);




Comments

  1. Deva has already answered your question, but it sounds to me like you could define an xml layout as you have described above, inflate it and populate it dynamically programmatically... perhaps the layout would initially contain an empty LinearLayout, and/or no text set for the TextView, maybe even set everything to android:visibility="gone" and show it when you have added/updated all your views?

    ReplyDelete
  2. See the links below which might help you achieve this :

    Programmatically adding items to a relative layout

    How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.