which layout would be the best solution? - java

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.

Related

Android studio How to configure app for multiple layout for deferent screens sizes when can't use Wrap content?

I hit a brick wall trying to configure my app for multiple screens. I read a lot of information about it but so far I didn't manage to successfully do it. to begin with, I have to use hard-coded sizes for my buttons because there are many of them, 53 to be exact, and they have to be square, 40x40dp, and placed precisely on the layout. so I can't use wrap_content, fill_parent, etc' because then I can't fit all the buttons on the layout.
I've created 5 different layout 'activity_main.xml' with these screen configuration: normal_mdpi, large-hdpi, xlarge-xhdpi, xxhdpi, xxxhdpi. so far so good but if I don't apply (With the "device for preview" dropdown menu) a specific phone configuration to any of the layouts, then they won't fit on the screen. But then the layouts fit only for these phone configurations, they won't fit for one with a similar configuration but slightly different. what am I doing wrong? how many layouts do I need? do I need to create a layout for each phone out there?
You cannot use fixed sizes. I know you want to use fixed sizes, but you can't. Creating multiple layouts for each density is a common mistake. There is usually only one xml layout resource file for each activity, but there are exceptions when making really advanced layouts (fragments will be used).
How to fix the layouts
As the base layout inside the layout resource file, i suggest you use ConstraintLayout as it has a ton of potential and can easily make complex layouts. It will take some time to edit each view to make them use wrap_content or match_constraint (match_constraint is basically match_parent but for ConstraintLayouts), but it is necessary. Android will then take care of resizing and fitting the views for any density/screen size if you use wrap_content and match_constraint (match_parent for other base layouts like LinearLayout).
And remember, never use fixed sizes for views.

How to Add Rules to align views inside Cardview in Android

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!

Eclipse android widget moving

I am designing my activity screen using eclipse but whenever I move an object e.g. a button it snaps around and I find it hard to position things where i want them.
Is there some setting I can change as as soon as I add a second button it snaps the other button to another position it is so annoying!
Also if I have an image button why can't i resize this, even with a normal button if i try to make it larger it just fits to the text and wont make the button bigger or it just flicks to a random place on the activity?
thanks
Probably you're trying to place components freely in the View but you're not considering the LayoutManager behavior. If you try to position your component in a LinearLayout, for example, the components will be placed following its rules, and these components will not stay where you drop them.
Try to understand better how the layouts works on Android. But for now, the AbsoluteLayout or RelativeLayout may be what you're looking for.
About the components size, you'll need to understand better how to use layout properties for these components. See the question How to size buttons for more information.

Create multiple horizontal scrolling layouts within a scroll view

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.

relative positions in android UI

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

Categories