I have a cardview inside which I have several Views which I want to align one below another. For a relative layout I will use the following
layoutParams.addRule(RelativeLayout.BELOW, idofanotherview);
However I cannot find addRule() method for cardview, how do I achieve this?
As the documentation of the CardView states, this component inherits from FrameLayout. That is normal that the addRule() method is unrecognized.
If you really need to use a RelativeLayout to position your subviews then I suggest that you wrap your subviews with one.
This post gives you a good exemple of how to achieve what you need
Good luck!
Related
I wish to achieve following smooth with android layout without having to implement custom linear layout manager. My question is following achievable with just combination of android design library and nestedscrollview.
<ParentLayout>
Textview
Imageview
Gallery(viewpager)
Recyclerview
Textarea
Textview
Textview
</ParentLayout>
I wish to achieve smooth scroll throughout the parentlayout. if i am at last element of the recyclview the scroll should continue to parentlayout. How can this be achieved and which parentlayout should be used?
From my understanding of your needs, you don't need a nested nestedscrollview. Just place your recyclerview inside of a parent scrollview and you should be good to go
If you provide a code sample or a wireframe of what you want to achieve, it'll be easier to help :)
So I have a Scroll View which contains a relative layout. I want that relative layout to have 3 or more layouts that can be scrolled. I know how to implement that with HorizontalScrollView, but before execution, I have no idea how many elements the HorizontalScrollView is going to have. Should I use ViewPager for this? What I would like the most is a HorizontalListView.
Would you recommend something like this:
https://github.com/sephiroth74/HorizontalVariableListView
Or should I add views programmatically in a HorizontalScrollView?
ScrollView is widget that should be used to keep static layouts inside. In general - ones that just are ~slightly bigger that the screen.
If you need something really dynamic you should use elements based on adapters like ListView, or ViewPager.
i have a problem in designing user interface and layouts. i want that some views relates to others. for example a button related to a TextView and when i sat margin top, it calculates from that TextView not screen top. how i can do this?
Have a look at Relative Layouts. They are the answer to your question. You can relate a view to another by setting it toLeftOf, below, alignParentTop or any other combination.
Use a RelativeLayout and Views will be positioned relative to other Views.
From what you have written it seems like you could group the TextView and Button into a container view, like a RelativeLayout, and fit them inside there with the appearance you would like. Then you can set the margin of the Relative layout.
Edit: Clarity
I am trying to create a layout that can fits my situation where I will need to have a layout that keeps the views like imageview or textview or probably a combination of a few. while on the top right of it, i will need to have two buttons on top.
However, this layout has to be dynamic where I can select whether if I want to show the buttons or not. How should I go about it?
You can use any layout you want really, although it sounds like a horizontal LinearLayout would fit your situation the best.
You can then control visibility of individual elements via setVisibility(View.GONE) and setVisibility(View.VISIBLE), or simply through addView.
I am creating an android game, I tried using the canvas.drawText() method to display my score and level but it caused errors. Now I am trying to use TextViews to display them, I am using a SurfaceVeiw and I wanted to know is it possible and would it be a good way of doing it.
You can't really put TextViews into a SurfaceView. What you can do instead, though, is add TextViews on top of the SurfaceView. Let me know if you need help on doing that if you don't know how.
// You can use a FrameLayout to hold the surface view
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(surfaceView);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(textview1);
relativeLayout.addView(textview2);
setContentView(relativeLayout);
When adding your views, it may be helpful to using LayoutParams to help organize things. Use this documentation for LayoutParams. Hope this helps!
If you're using an XML layout, I would use a RelativeLayout, as mentioned above, contain the SurfaceView inside a FrameLayout. Placing the TextView's on top of the FrameLayout is just a simple matter of configuring the RelativeLayout parameters for each view. You seem new to Android, perhaps this guide will help you with RelativeLayouts and XML layouts in general.