Why does adding this empty padding View mess up my layout? - java

So I'm using layout weights to scale parts of my layout to a certain fraction of the screen, and when I try to add an empty view for padding between elements, it causes a 0-size error on the views.
The original layout, which works before adding anything is:
http://pastiebin.com/?page=p&id=4e9777a2639f4 (Sorry for the pastebins instead of pasting it here, it wouldn't format correctly because of the xml tags)
And adding this between, or before or after any of the DWIButtons cause the error:
http://pastiebin.com/?page=p&id=4e9777caefc2d
I have no idea what to do here ):
EDIT 1:
Here it is without the padding view added
EDIT 2:
Also, adding any other type of view doesn't mess it up. I added a TextView instead of a blank View, and it resized correctly

adding images might help us understand the situation. but i suspect the layout weight is what is messing up your layout. setting layout weight to 1 means you want the blank layout to fill the available space(most of the time the entire screen).

Related

How to draw vertical line between RecyclerView elements and also within element?

Here is screenshot
You can see on the screenshot RecyclerView with a different type of the elements.
Question is: How to connect consistently each element by vertical line (as you can see on the right side of the screenshot)?
Also there is a question about the vertical line that consist within one of the RecyclerView element (as you can see on the screenshot). Height of the line should be able to dynamically change according of the number of elements that it lead(you can see blue line and bullets on it).
Fell free to ask
Appreciate any kind of ideas
You can use ItemDecorator
1) Override method onDraw(...)
2) find interested views via recycler.getChildAt()
3) if you need view holder to check type, you can get it via getChildViewHolder
4) draw vertical line within bounds of view

scrolle to a table raw in a scrolle view at run time

In my Android project I'm using a table layout contained in a scroll view. When a product is selected from a spinner the table view must automatically scroll to the table raw showing the relevant product's details. Unfortunately I can't post any of my code work. I have a big problem in scrolling the view. So can someone help me with some helpful code.
Thank you.
I think one solution is to know the index in your list of the selected product. Then you'll have to measure the childs in your ScrollView. When you know the sizes of your children and the index of the child you want to scroll to you can use ScrollView.scrollTo(x, y)
the y will be something like this: index * heightOfChild.
This is ofc if your childs are of the same size. Otherwise youll have to measure each child separatly.
Edit:
scrollTo apparently "clamps the scrolling to the bounds of our child." Not sure what it means... But you could try scrollBy(x, y) the documentation doesnt write anything about clamps for that.
if you are using a listView you could try setSelection() as described here:
Android scroll to the bottom of a listview

ActionBar: Center text on navigation bar buttons

Simple aim:
To have default buttons in the navigation bar of my ActionBar with the text centre aligned. I simply want to use the default, simple way of:
ActionBar.Tab myTab = Actionbar.newTab()
myTab.setText("my tab's text");
What's the problem
I cannot find any simple way to do this whatsoever. You can't access the views (as ActionBar.Tab.getCustomView() always returns null due to it being the default view). There is no method that I can see in ActionBar, ActionBar.Tab, etc to get the current view, get or modify LayoutParams (especially Gravity). A visualisation of the problem is below (one line seems to work right, but double lined navigation buttoms definitely seem left aligned):
Unideal solution
Am I right in negatively thinking that the only way to accomplish this is to use custom views for all of my tabs, somehow guess / attempt to copy the default formatting for the tabs (as I can't find anyway to access these views) and assume / hope that the default formatting / font / style of ActionBar.Tab's text does not change any time soon?
Surely there has to be a better way?
AFAIK, there's no way you can access the Views in ActionBar. Using custom layout seems the only solution. However, doesn't Android align the text to center automatically? It apparently does so in all my applications.
please download the follwing example. it will help you. i am sure i take help from this demo.
https://github.com/JakeWharton/ActionBarSherlock

Android ratingbar "ratingBarStyleSmall" loads random amount of stars

I find myself a little baffled here. I'm fairly new to android/java development so please help out here to identify what I'm doing wrong. I have checked aggressively throughout google, android dev and stack but couldn't find much info on this.
I have a string variable with a number as theRating. e.g. 2.5 (out of 10)
I am trying to display this using ratingbarstylesmall... It's for showing only and not rating.
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setStepSize((float) 0.5);
rb.setMax(10);
rb.setRating(Integer.parseInt(theRating));
llTextEtc.addView(rb);
The stars load fine inside LienarLayout (llTextEtc), show up in the right place, it's the right style stars that i want (small) but...
it's completely random. Some show 8 stars, some show 15 then back to 7 and so on. Totally random. What am I doing wrong?
Thanks guys
Update:
With help from the accepted answer:
A LinearLayout was added to hold the ratingbar with wrap_content layout. Otherwise the parent (scrollview) became wrap_content.
LinearLayout llRating = new LinearLayout(this);
RatingBar rb = new RatingBar(this, null, android.R.attr.ratingBarStyleSmall);
rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rb.setIsIndicator(true);
rb.setNumStars(5);
rb.setRating(Float.parseFloat(theRating)/2);
llRating.addView(rb);
llTextEtc.addView(llRating);
setStepSize and setMax was also removed, Rating was set to float instead of integer. The stars are calculated and then set. stepSize and Max doesn't help when isIndicator is true.
I noticed a couple of problems here. First and most important you need to set the layout width to WRAP_CONTENT for it to work properly. The following is from the dev guide:
The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap content (if another layout width is set, the results may be unpredictable).
Secondly I noticed that you have set the stepSize to a float and you are seting the rating using an Interger. I would first convert the value from the URL feed to a Float and then validate it before passing it to the rating bar.

removeAllViews() not removing TableRow children in a TableLayout

Some background:
I am having an issue with one part of my program where I am building a 'edit database' view and then removing the rows again. The fields which the 'edit database' view will show are different each time it is shown. The edit database view is a tablelayout, and each row in the database is a row in the table layout. when the user exits the edit view, the view is supposed to be disposed or wiped of rows for the next time.
My issue, is no matter what I try, I can't get the rows in the tablelayout to delete. Here are the relevent pieces of code:
Get a handle on the tablelayout in the xml file
setContentView(R.layout.database_entry);
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
the layout is passed to another class, where they are added to the layout like this:
//layout is the tablelayout from earler, passed into this function
TableRow row = new TableRow(contex);
row .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//stuff added to the row....
//...
//...
layout.addView(row);
When I attempt to wipe the Tablelayout, I run this code
TableLayout editorLayout = (TableLayout)findViewById(R.id.DatabaseLayout);
editorLayout.removeAllViews();
Whatever I do, the next time I hit the button to edit the code, it will show the previous rows, as well as the new rows added. They are not removed from the table. I get no error messages or any useful debugging information, it just isn't functioning.
I have struggled with this for a while. Tried dynamically creating the tablelayout(no xml file), tried deleting each row (or just ONE row) with it's index. I have used the Hierarchy Viewer to check to make sure everything is a child of that tablelayout (it is). If anyone has any insight, it would be much appreciated.
I was able to figure out the issue. First of all, the comment above was a little bit wrong, the mChildren array did not contain any pointers to the views I had deleted. They were nulled out, just as they should have been.
The reason why none of my rows were deleted is because right after I called removeAllViews() I switched content views setContentView(R.layout.main);. when I commented this line out, the remove children code worked as expected. I assume some android back-end optimization code decided that the removeAllViews() didn't need to be run, or updated somewhere in memory. And all the changes made while inside this function didn't apply.
I will be putting all of this code in a new activity (where it really belonged in the beginning). But I wanted to provide an explanation in case someone else is having the same issue.
After removing the row try setting the visibility to GONE and back to Visible again, This should fix the remove row glitch.

Categories