I am trying to add empty header or footer on ListView to create a space between top and bottom ListView. I could create another empty layout and add into it. But I'm try to find alternative in coding, create for example linear/frame layout with width (fill_parent) and height (say, 50dp) and add header into ListView.
you may do it in this way:
LinearLayout viewHeader = new LinearLayout(context);
viewHeader.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams lp = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 50);
viewHeader.setLayoutParams(lp);
yourListView.addHeaderView(viewHeader, null, false);
Related
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.
In Android, I have a RelativeLayout named A. It has a view named B in the top of A. It has a view named C in the bottom of A.
Code I have so far:
RelativeLayout A = new RelativeLayout(context);
View B = new View(context);
LayoutParams paramsB = new LayoutParams(LayoutParams.FILL_PARENT, 40);
paramsB.addRule(RelativeLayout.ALIGN_PARENT_TOP);
View C = new View(context);
LayoutParams paramsC = new LayoutParams(LayoutParams.FILL_PARENT, 40);
paramsC.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
A.addView(B,paramsB);
A.addView(C,paramsC);
How do I adjust the RelativeLayout size when the keyboard shows?
I think you can set layout parameters of 'A' relative layout to WRAP_CONTENT so that when keyboard comes from bottom then it will adjust the height with relative layout.I doubt about the rules you added to 'B' and 'C' relative layout because you have added Align_parent_bottom and Align_parent_top to true.
There are two ways to achieve that:
First you can define the following attribute in of AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
Second you can add scrollview as your parent view of your whole view.
I've an ImageView inserted in the RelativeLayout. On top of this ImageView I'm trying to insert a progressbar, which would go invisible after the image is downloaded. But, when I add progressbar after adding the ImageView, it gives me an error -
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here is the code:
mRelativeLayout = (RelativeLayout) mGallery.findViewById(R.id.relative_progress_spin_layout);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.progress_spin);
progressBar = (ProgressBar) mGallery.findViewById(R.id.progress_spin);
image = new ImageView(GalleryModuleActivity.this);
image.setPadding(4, 4, 4, 4);
image.setScaleType(ImageView.ScaleType.FIT_XY);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
mRelativeLayout.addView(image);
mRelativeLayout.addView(progressBar);
mHorizontalLayout.addView(mRelativeLayout);
Thanks..
You already have the ProgressBar in the layout(you search for it with findViewById) so you shouldn't add it again to the layout(the same thing with the mRelativeLayout RelativeLayout if it is already in the layout file). Remove this lines:
mRelativeLayout.addView(progressBar);
mHorizontalLayout.addView(mRelativeLayout);
If you have the views in the layout you don't add them again to the layout!
What you exactly want to do with images and progress bar. If you want to display ProgressBar on images use FrameLayout. In that also you can use VISIBLE and GONE stuff.
Where you want to display that dynamic generated views ?
Because we do have adapter to display same type of data with different content.
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?
I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file).
The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance of my custom view, then use addView on the RelativeLayout to add the new view.
I'm not getting any errors, but when I click my button to add the new view, nothing is happening (view isn't being added). Do I need to set additional properties on my custom view (layout width, layout height for example) in order for it to be shown?
EDIT: adding code
// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Please post your code where you add the view.
But yes, you might be missing the params for width and height. Try something like
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
txtView.setLayoutParams(p);
or what you would like the width and height to be. Also in xml layout, layout_width and layout_height are required attributes.