I want to extend the linear layout class, and alter some of its properties.
One of the properties that i want to alter, is the background.
The attribute background would be set to something, and if this background is shorter than the view's height, i want it to be vertically repeated.
if in the constructor i use getBackground() i get the background that was set to the view.
However, when i try to tile it, it shows great once, but all the locations it should repeat in, it is just smudged.
How do i get the view's ResourceID background instead of the background image, so i can work on it without the "super." methods being called on it first?
If your only reason to extend LinearLayout is to set a repeating background or a non-repeating background then there's no need to extende LinearLayout.
You can create a repeating background from a excisting Drawable using a drawable xml file:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"
android:src="#drawable/your_drawable"/>
Depending on your 'state' you can set the background of the LinearLayout to your choice.
Related
I have a relative layout with child TextViews.
The background of the parent RelativeLayout is white and I was wondering how I could change the alpha to change the opacity of the whole view programmatically (including children).
I am trying:
getBackground().setAlpha(0.4);
But that expects an int and not a float.
If I do:
getBackground().setAlpha((int)(0.4 * 255));
The latter changes the view but makes it darker than I want. Also the children do not seem to change. It seems to affect only the background while I want something that makes everything more "grayed" out/less transparent.
What am I doing wrong?
Have you tried using android:background="#88000000" in layout file. You can change alpha and color values as required.
That's because you are changing the backgound of the layout and not the layout itself. So instead of myRelativeLayout.getBackground().setAlpha(), use this: myRelativeLayout.setAlpha(0.4f).
I was wondering which views have background #null by default and on which views it's recommended to add it in order to reduce the number of overlays? For example in the following construct:
relativelayout
relativelayout
framelayout
view
If I want a white background (where only one view in the xml has this background - no overlaying of white on white), and I only define 'background white' for the highest relativelayout - will all the other layouts have a background "layer" or not (if none is specified)?
How can I create two layouts: the first lighted, the second, in background, less enlightened?
Put both layouts in a RelativeLayout, and set the appropriate background color with desired transparency.
you can use relative layout in which you can add another layout. Set the background of first to lighted and enlighted.
Use FrameLayout to contain them both.
About the "lighted" part - use the View.setAlpha for each Layout
http://developer.android.com/reference/android/view/View.html#setAlpha(float)
I want to draw linearlayout border with different color by Java code, not using xml file
if that's possible? Please give me code.
Thanks
For Android, the LinearLayout layout does not come with a border attribute. You can however, set the background of the LinearLayout to a 9 patch image.
You can modify anything on the xml file you're creating as long as you link it back to your Java file.
Example:
If your xml file was:
<?xml version="1.0" encoding="utf-8"?>
<!-- note that the id is linearlayout. That's important, you want the id to be something you can use to identify that element. -->
<LinearLayout`
xmlns:android="http://schemas.android.com/apk/res/android"`
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
Then in your Java file you'd have to do this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whateverXMLFile);
//get the Linear Layout you're searching for
LinearLayout linLayout = (LinearLayout) findViewById(R.id.linearlayout);
/*background is your background image you want to replace with.
* You can use any that is in your drawable resource.
* Better to use a 9 sketch because it'll expand to fit your width/height, no matter
* how big or small your layout will be.
*/
linLayout.setBackgroundResource(R.drawable.background);
//if you want to set the background colour:
//will set it to RED, you can also specify a resource file you may have for it
linLayout.setBackground(Colour.RED);
}
You just need to create a nine-patch image and set the background.
I would like to create a button with circular or rectangular background, text and an image below or above the text.
Here is the CustomButton Layout where I added the objects (background and text - ImageView is missing):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center_vertical|center_horizontal">
I would like to create a CustomButton object with methods setText() and setImage() which would change the button text and image and place multiple CustomButtons into main layout.
Does anyone know how to create a custom layout, place it into another layout(main) and modify its elements from the activity which is bound to main layout?
I would really appreciate your help.
Thanks!
Hey, To create a circular button or rectangular button you can use shape.
It can be done in .xml file.
see this Click Here
If you want programmatic access, you should subclass View and do your work there in java. You can still do the layout in xml, but have the image and text methods that you want. You will then be able to use this in another layout to place your CustomButtons.