I'm trying to create my android app for a lot of different devices so I'm trying to avoid using fixed heights and width and instead using the property WRAP_CONTENT.
Now I need to create a textview on top of a button and align that to the bottom. However the documentation states that you can't use WRAP_CONTENT in combination with ALIGN_PARENT_BOTTOM (which is obvious). Is there another way to achieve this?
The structure is something like this. A RelativeLayout which wraps a button and a textview.
RelativeLayout fl = new RelativeLayout(this);
fl.setLayoutParams(relativeWrapContentParams);
fl.addView(filterBtn);
fl.addView(filterCaption);
The buttons are also created dynamically so theres no xml. Instead the buttons are created in java code.
Also is this a good way of programming for multiple resolutions? Or is it ok to use fixed heights because then the problem is easy to fix and I can just give the relativelayout a fixed height and align its children with ALIGN_PARENT_BOTTOM
See this link this article is the bible for the newbies in android.
Now coming to your question you don't need to use relative layout just for this purpose
you can use linearlayout with vertical orientation place text and then button.
and you need to place this linearlayout inside relative layout with property alignparentbottom=true.
in such way you can have this layout of text and button at the bottom of the screen
Related
I have a use case were a user can create own buttons and give each button a label.
To display the buttons correctly i use Recyclerview with StaggeredGridLayoutManager. This works like a charm.
Unfortunately Recyclerview is not supported on Android Widget.
What do you think is the best solution to display an arbitrary amount of buttons (each button can be of different sizes) on a widget? Should I use a GridLayout?
You can find here all the layouts that are supported to use in android widgets.
Of all the supported layouts listed the one that is closer in appearance to StaggeredGridLayout is the GridLayout. So, I would pick GridLayout to start with and then depending on my design needs I would combine it with what fits my needs best. Most probably, if you want to keep StaggeredGridLayout's default appearance, maybe you can combine it with some LinearLayouts wherever needed.
How to have multiple layouts (eg. Relative layouts) stacked one above other (each contains text and images regarding news) and move up/down or animate the top most layout by scrolling action in android studio. Please see this screenshot to kmow what i am talking about
https://drive.google.com/file/d/1MrqgKn7CEVuZwkMi8a6ZZB0MXxR3w10R/view?usp=drivesdk
in the new version of Android studio you can resize your preview layout by drag it. try this
There's a few ways to accomplish this.
Option 1: Dynamic content
- Utilize a Recycler view
- In your ViewHolders, use your RelativeLayouts as the layouts to inflate.
Option 2: Static Content
- Utilize a ScrollView root
- Utilize a vertical LinearLayout with height of wrap_content.
- Stack your RelativeLayouts.
Option 3: Another Static Content Option
- Utilize a ScrollView
- Utilize a RelativeLayout child
- Utilize your RelativeLayout childs inside the previous RelativeLayout and use layout_below in each of them.
(Extra info: Consider using ConstraintLayout or something else in place of RelativeLayout to avoid the double measurement issue with RelativeLayout)
In my project i have 3 xml files.
My main layout
and 2 layouts which i want to include in my main layout
*big_buttons.xml* >contains big size buttons
*small_buttons.xml* >contains the same buttons as above (same id's aswell) but they are smaller
By default i want the *big_buttons.xml* included, but id like to be able to "exclude" the *big_buttons.xml* and include the *small_buttons.xml* programmaticly after an onClickListener
Is it possible to do something like this?
By default you can use setContentView(R.layout.big_buttons);, and then in your onClickListener you could do setContentView(R.layout.small_buttons);
If it's specific buttons you want excluded rather than the entire XML, I think you need to combine the 2 XML files and by default give the "big buttons" the attribute android:visibility="visible" and the "small buttons" android:visibility="gone".
Then programmatically you can do
Button bigButton = (Button) findViewById(R.id.big_button);
Button smallButton = (Button) findViewById(R.id.small_button);
bigButton.setVisibility("View.GONE");
smallButton.setVisibility("View.VISIBLE");
You'll want to use GONE rather than INVISIBLE because GONE excludes layout features like height and width, where INVISIBLE just doesn't display the button, but keeps space for it.
Check out View.setVisibility. You can use this on a layout manager, so that you can make entire groups of controls visible or invisible from Java code.
I would like to change the style of an Android widget button (specifically the corners) completely programmatically. Meaning, I would like to do it without any xml files at all. From my research I am coming to the conclusion that this is not posible. Is my conclusion correct or does someone know how it might be done?
You should look at the inherited methods from TextView and View of Button:
setPadding(int left, int top, int right, int bottom)
to name one... you can also change the layoutparams of the button object, which gives you access to all the xml attributes normally accessible. In general, you can do everything programmatically that you can do in the xml.
If that isn't enough, you can extend the Button class and override the onDraw method to change how android draws your button
EDIT:
Maybe you can add an xml theme much like this thread suggests: How to programmatically setting style attribute in a view
and then in it set
and add the theme programmatically to the button?
I haven't done much work with themes, so I can't attest to whether this will definitely work
Why do you think this is the case? Taking one of your items as an example, here's a method to set padding.
For corners you can use a drawable with a shape with rounded corners. See the documentation for drawables. There are also many questions (with answers) on StackOverflow about this too. If you want to do it programmatically there is the RoundedRectShape.
I'm looking for a way to update a layout's content with a new view. Is there any way to easily do this. It would be similar to how tabs work, but I don't want to have to get into extending the current tab structure if I don't have to.
The final result would be a few buttons that would switch the content in a specific linearlayout for each button.
Any help?
Have a look at ViewSwitcher. This is designed to help with the kind of things you are suggesting.
If you want the contents in entirely different layout files, you can use a LayoutInflater and add the inflated view to the parent view.