I'm in need to set an ImageView as a background for a let's say LinearLayout. Is that possible to do so?
I wish you wrote more details about what you require. If this is only about setting an image background, you can use the android:background property of LinearLayout. However, to answer your question directly, a view can be placed behind another using FrameLayout:
<FrameLayout>
<ImageView/>
<LinearLayout>
...
...
</LinearLayout>
</FrameLayout>
Yes it should be possible. In the layout xml, try wrapping the ImageView inside the LinearLayout. Then in Activity OnCreate function set the xml as content layout.
Related
I have a RelativeLayout and inside it I have a LottieAnimationView and another LinearLayout. The LottieAnimationView serves as my background image and inside the LinearLayout I have my content with EditText and Button. So my problem is now that the content shall be scrollable when the softkeyboard is out but the background image shall not be resized. I have tried several things but nothing really worked.
My XML looks like this:
<RelativeLayout>
<LottieAnimationView>
<ScrollView>
<LinearLayout>
<EditText>
<Button>
</LinearLayout>
</ScrollView>
</RelativeLayout>
My Solution:
I had to put the LottieAnimationView inside the LinearLayout and set android:adjustViewBounds="true" to the Lottie View.
You can also set match_parent to your ImageView or LottieAnimationView in the layout xml and then in the activity/fragment/view set it's height to the current:
val height = yourBackgroundImageView.height
if (height > 0) {
yourBackgroundImageView.setHeight(height)
}
Just make sure it's done after the view was attached to the screen (and measured).
I have a LinearLayout that has 2 children: a ImageView aligned left and a TextView aligned right.
I've set the background of the LinearLayout to be a #drawable XML resource that has two <item> tags. One of them has android:state_pressed="true". Also, the LinearLayout has android:clickable="true".
When the LinearLayout is clicked it correctly changes its background to the android:state_pressed style, but clicking on one of its children doesn't propagate the click action up to the LinearLayout.
Is there a way to easily achieve a click state on the parent view when a child view is clicked?
Dont use Both as it will give Exception
Use this to your parent
android:addStatesFromChildren="true"
Or add in your child views
`android:duplicateParentState="true"`
Hope it helps
not sure if it works for your specific implementation, but a very easy way of achieving this "complex" button is by making a normal button and using android:drawableLeft or android:drawableRight or android:drawableTop or android:drawableBottom and android:drawablePaddig to achieve the same visual result in just one view.
for example:
<LinearLayout orientation=vertical>
<ImageView/>
<TextView/>
</LinearLayout>
is pretty much the same as
<Button
drawableLeft="#drawable/..."
text="..."
/>
that way your whole layout is simpler and the pressed states works out-of-the-box.
Consider using a single TextView and using the android:drawableLeft or android:drawableRight attribute to show your image. It's better design and more performant than a LinearLayout with two children.
If that won't work for you, try adding android:addStatesFromChildren="true" to the LinearLayout.
Folks, I have the following layout: http://dpaste.com/hold/755261/
It has two seekbar widgets. Both are not showing. If I move them to the root linearlayout, they appear, otherwise not. I need them to be inside the first linearlayout. Anyone could help me how to achieve that?
It looks like you need to put the following in your third LinearLayout:
android:layout_weight="1"
Change the second LinearLayout height to wrap_content. If the content doesn't fit on the screen, then maybe you have to use a ScrollView.
I am developing an Android 2.1 app.
I have defined a LinearLayout class:
public class MyTopBar extends LinearLayout {
...
}
Then, I have a layout xml file (content.xml):
<LinearLayout>
...
</LienarLayout>
I have a RootActivity.java , I would like to set MyTopBar as content in this RootActivity.
Then I have MyActivity which extends RootActivity:
public class MyActivity extends RootActivity{
//set xml layout as content here
}
I would like to set the content.xml as content of MyActivity.
As a whole, I would like to use the above way to achieve the layout that MyTopBar should be located on top of the screen always. The other Activities which extend RootActivity will have its content below MyTopBar. How to achieve this??
1 You could add your custom LinearLayout directly to the xml layout of the MyActivity class like this:
<LinearLayout>
<com.full.package.MyTopBar
attributes here like on any other xml views
/>
...
</LinearLayout>
or you could use the include tag to include the layout with the custom view:
<LinearLayout>
<include layout="#layout/xml_file_containing_mytopbar"
/>
...
</LinearLayout>
2 Use :
setContentView(R.layout.other_content);
Have a Layout vacant for the TopBar and add Your Topbar in it by using layout.addView(topbarObject);
Regarding your second question the setContentView can be called only once, as far as I know. You can however have those two xml files inflated using View.inflate(other_content.xml) and added in the parent xml layout whenever you need it. You can removeView() on parent layout and addView() with the new layout file.
Edit:
For the solution of both the question, you can have a parent Layout for eg. like the following:
//Omitting the obvious tags
//parent.xml
<RelativeLayout
android:id="#+id/parentLayout">
<RelativeLayout
android:id="#+id/topLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/contentLayout">
</RelativeLayout>
</RelativeLayout>
Now in your code set the parent layout as content view,make an object of your TopBar layout and add it to the topLayout.
setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.
Now inflate the required xml layout. and add it to contentLayout.
RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.
and when you need to show the other content xml, just call the following code.
contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);
I am trying to place an overlay on top of my Activity. In other words, I would like to put a gradient on top of the whole screen with all views behind it.
I currently have a LinearLayout with my buttons and everything in it. Do I extend the LinearLayout and add something on the onDraw method, or is there any way I can add another layout that overlays everything?
You can do two things:
replace the LinearLayout with a RelativeLayout and so you can easily place anything you want on top of each other
wrap your LinearLayout in a FrameLayout. The FrameLayout draws everything in order of definition/adding so your gradient should be the last added view/layout.
Something like that:
<FrameLayout>
<LinearLayout>
<Your stuff>
</LinearLayout>
<GradientView/>
</FrameLayout>