I have TableLayout in my project which contains 6 TableRow layouts. Each TableRow layout has 7 TextViews in it. I want to set a background image to this TableLayout which is currently displaying 42 TextViews. I also had set some background color to each TextView. I tried this
mDateSelectionBar.setBackgroundResource(resid);
But it is not showing up my Image. However, If I do this for my 42 TextViews..
TextView.setBackgroundResource(0);
Then my Image is visible. But I want to display TableLayout background Image as well as background color of each TextView. How can I do this? Please Help! Thanks :D
The issue is that the background of the individual cells in a table layout is drawn over the overall layout background. If you remove the background of the cell, then the overall background is visible. If you need to display the background of the table as well as some colouring of the cells, consider setting the alpha channel of the cell background colour to less than 100%, e.g. 0.8 or something.
When you set the colour for your cells, use #ARGB syntax, e.g.
setBackgroundColour(Color.argb(192, 255, 0, 0));
Related
I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly
I got a picture that I want to use.
I set it as following:
ImageView menu = new ImageView(this);
menu.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
menu.setImageResource(R.drawable.menu);
They annoying thing is that I get white pixels on the sides of it cause I want to keep the aspect of the pic.
I can stretch the image by using menu.setScaleType(ScaleType.FIT_XY); but that will make the person on it look really fat. The picture is dark and the pixels are white so they do show quite well. :/
Is there I way I can first apply black color and then the image above that color?
To set a background color to any view on android you can use android:background attribute in layout xml or by calling setBackgroundColor(int id) in java code.
But if you really want just to set the image in bounds you can give a try to android:scaletype="centerCrop"
Using set BackgroundColor will also remove any padding, borders and what not attached to the object.
If that is the result you want, that is a reasonable approach.
If blasting the objects current style will cause problems, I would look to use CSS to set the background color and change the css styles through code if things are happening after page load.
Consider using a border around the image that is colored the way you want to hide what is underneath?
How do you fix(prevent them from moving) the size of cells in a tablelayout in android?
I have a tablelayout with image buttons inside each cell, the problem is, when I change (or remove) the image, the size of the button changes. How do I prevent this?
Set each View’s layout_width equal to 0.
Add in each View layout_weight, according to your needs
Reference : http://pournaras.zilsen.com/2010/11/01/fixed-column-width-in-tablelayout/
I am creating a number of imageviews and textviews on runtime depending on the objects in my webservice. I'm creating linearLayout horizontal and adding imageviews and textviews to layout, now the issue is the text are against images, and images are of different width so i want to set x position of my textviews so they all look align how can this be done,
i tried absolutelayout(warning deprecated) , setx(no method showed)
you can use resized image on runtime based on the pannel height and width for resizing image on runtime view this stack overflow thread
resizing image java getScaledInstance
I presume you mean this is a vertical LinearLayout, and each text/image combo is added below the previous one? In that case, you could have two LinearLayouts next to each other like two columns, adding the images to one and the text fields to the other. Then the widest image would stretch its layout to that width and all text fields would be just to the right of it.
Use weight property in your ImageView and TextView
is possible to have a separator between elements of a GridView?
Thanks
You'll probably have to play with padding and background colors. Set the background for the table to one color, and the background to each View in the table to another color. Set a 1 or 2 pixel padding around each View in the table, and you should have a border between.
from #FreewheelNat's comment, "In your xml for your GridView, use android:horizontalSpacing="1dp" and android:verticalSpacing="1dp" to set a 1 (density independent) pixel padding around each cell for example. "
this is a much better solution than #Jeff Barger's