Programmatically Position items in activity. NO XML - android

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:
RelativeLayout (Layout object that all the child views sit in)
TextView (Title for the page, sits at top of the RelativeLayout)
ScrollView (Scrollable area that holds all the data controls)
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);

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?

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?

Related

how to place buttons on a layout based on condition?

I am creating an android layout in which I will place 3 buttons vertically with some space between them. But based on the requirements, the button count can come down to 2, and I need the buttons to repositioned based on the available screen space.See the screens below.
now the second case with two buttons should be like
How should I go about doing this ?
You can just set visibility of buttons depending upon your condition just like the following:
button.setVisibility(View.GONE);
When you set visibility to gone then the space will considered available for other layouts and your will be set as per your desire.
When you set visibility to invisible then the space will considered as allocated.
You can try below code for achieving the same based on your condition for adding buttons to your layout
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
You can define all three buttons in the layout XML and then :
if you want to show the third button
thirdButton.setVisibility(View.VISIBLE);
if you want to hide
thirdButton.setVisibility(View.GONE);
at least if using a Linear or Relative Layout it should repositione by itself.

Android: ListView covers everything

I am using a ListView inside a LinearLayout and below that another LinearLayout, which won't show up because the ListView appears to take up all the space.
Code:
listView = new ListView(this);
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
listLayout = new LinearLayout(this);
listLayout.setOrientation(LinearLayout.VERTICAL);
listLayout.addView(listView);
listLayout.addView(new NavigationBar(this, "android.intent.action.MAIN", "android.intent.action.MY_ACTIVITY"));
setContentView(listLayout);
NavigationBar is also a LinearLayout containing some buttons.
If added on its one it just play properly if added after the ListView it doesnt display at all.
You should set the weight attribute for the linear layouts or use fixed height for the listview. Please post the xml layout to help understand better.
Change
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
to
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
(assuming you've imported LinearLayout's LayoutParams, otherwise it will be new LinearLayout.LayoutParams)
This will mean the list view itself won't take up any vertical space, so the navigation bar can be laid out to how big it wants to be, but then, any free space will be assigned to the list view because it has a weight.
You should also consider not having the navigation bar at the bottom, that is a very iPhone thing to do.

Horizontal and Vertical linear layout inside a scroll view

I am trying to get a combination of Views established. They must constantly have a Button and Edittext box at the top horizontally next to each other and below that a vertical list of Textviews. The vertical list should be enclosed in a ScrollView to allow the user to scroll down through the TextViews (The Button and EditText at the top should still be visible while this is happening).
protected void initLayout() {
// Declaring the vertical layout
verticalLayout=new LinearLayout(this);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
//Declaring the horizontal layout
horizontalLayout=new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
//set the main view as horizontal at the top
setContentView(horizontalLayout);
//Declaring the scroll view
ScrollView scrollView= new ScrollView(this);
scrollView.addView(verticalLayout);
//set the scroll view
setContentView(scrollView);
//declare and add button to horizontal view
theButton= new Button(this);
theButton.setText("Add Joke");
horizontalLayout.addView(theButton);
//declare and add edittext to horizontal view
theEditText= new EditText(this);
theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
horizontalLayout.addView(theEditText);
}
I believe I might be going wrong with the setContentView but amn't completely sure.
your parent ViewGroup must be one. you are using 2 times setContentView and this is your mistake. use only one and add other linearlayout as child in another one.
I think it's better that you try to do in in xml and then write your code like that.
basically you've got the setContentView wrong...
i would strongly suggest to define the layout in xml - and then set it just once with the following:
setContentView(R.layout.my_xml_layout);
this way - you normally do have the possibility to see your layout, and it's (in my opinion) easyer to define a layout (especially if you'll decide to change this one day).
if you wan't to do it in code however, what you'll have to do is something like the following (untested - but should work)
protected void initLayout() {
// Declaring the vertical layout
verticalLayout=new LinearLayout(this);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
//Declaring the horizontal layout
horizontalLayout=new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(horizontalLayout); //add the Horizontal-View to the parent-grid
//Declaring the scroll view
ScrollView scrollView= new ScrollView(this);
scrollView.addView(verticalLayout);
//set the scroll view
verticalLayout.addView(scrollView); //add the scrollview to the parent-grid
//declare and add button to horizontal view
theButton= new Button(this);
theButton.setText("Add Joke");
horizontalLayout.addView(theButton);
//declare and add edittext to horizontal view
theEditText= new EditText(this);
theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
horizontalLayout.addView(theEditText);
setContentView(verticalLayout);
}
edit: sorry - had a few errors in the first time - i edited it now, it should work like this.
I found a solution to the problem. I had to encapsulate both the horizontal layout and vertical layout inside of another linear layout and set that as the root view.
protected void initLayout() {
// Declaring the vertical layout
verticalLayout=new LinearLayout(this);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
//Declaring the horizontal layout
horizontalLayout=new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
//***SOLUTION*** Create a view to group the other views in
groupLayout=new LinearLayout(this);
groupLayout.setOrientation(LinearLayout.VERTICAL);//vertical as the horizontal view is on top of the vertical view
//Declaring the scroll view
ScrollView scrollView= new ScrollView(this);
scrollView.addView(verticalLayout);//the vertical layout is the only view that should be scrollable and therefore added to the scrollview
//declare and add button to horizontal view
theButton= new Button(this);
theButton.setText("Add Joke");
horizontalLayout.addView(theButton);
//declare and add edittext to horizontal view
theEditText= new EditText(this);
theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
horizontalLayout.addView(theEditText);
//***SOLUTION*** attach the views to the group view
groupLayout.addView(horizontalLayout); //the layout displayed at the top of the group layout
groupLayout.addView(scrollView); //the layout below the top (scrollview already contains the vertical view)
setContentView(groupLayout);//assign the group layout to the content
}

Adding fields dynamically to a layout in Android

In my application, I want to add fields dynamically based on the selection. While there are 4 fixed spinner fields, the values of spinner are dependent on the selection of the first spinner field etc. The second spinner field has to be populated based on the first field and the third spinner field based on the second field and so on.
Based on the options selected in these 4 spinner fields, the layout will contain additional fields that are text fields or radio buttons etc.
Can someone suggest what is the best way to achieve this or refer to other examples for this?
Thanks
You can add a View to a Layout similar to the below code:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.setLayoutParams(layoutParams);
TextView view = new TextView(mContext);
view.setLayoutParams(layoutParams);
relativeLayout.addView(view);
And then you need to add it to an existing ViewLayout:
LinearLayout originalLayout = findViewById(R.id.mylayout);
originalLayout.addView(relativeLayout);
This idea can be expanded on to add custom Views etc to your screen.

Add button to bottom of linear layout programmatically

I have a bunch of buttons displayed using the default gravity. The last button I add to the LinearLayout view is what I'd like to appear at the bottom of the view. How do I add it programmtically to appear at the bottom of the screen? I've tried setting the gravity, but everything falls to the bottom. I just want the one button to fall to the bottom of the screen. Ideally, I won't have to make another view.
Try this:
Button button = new Button(this);
youLinearLayout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
edit
sorry, the code above dont work.
You cant do this using an single LinearLayout if orientation==vertical.
You'll need create another layout(RelativeLayout) and add TextView to it.
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(textView, params);
linearLayout.addView(relativeLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
If the rest of the screen is not empty, you can give android:layout_weight=0.0 to the Button, and 1.0 to the widget on the top.
In that way, widget with 1.0 weight will expand to fill empty areas, and Button with 0.0 will take only the default space, and being the last item addes to a vertical LinearLayout, it will be sticked to the bottom.

Categories

Resources