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.
Related
I am working on a mini-game that includes 40 different buttons. I guess I can make them manually on the XML code and then group them into an array, but can I make buttons using the code in the first place?
Yes, you can do it from code as well:
Button button = new Button(getContext());
button.setText("Button_text");
button.setId(xyz);
// ... add onClickListener etc.
// Add it to parent view, e.g. LinearLayout
parentLayout.addView(button);
If you have to add 40 different buttons, wrap it to a for loop for example.
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.
I have a relative layout tree, let's say that I have a couple of buttons in root layout and other one is placed on top of the buttons, covering them with it's backgound.
Visually everything is ok, but I still can activate invsible buttons placed under the layout, is there any property related to this?
I have tried elevation, translationZ, etc. I would like to avoid programatically fixing the problem (isShown for example), is there anything else I can change in xml to prevent them from activating?
In your layout xml file, add setClickable="true" to the same layout you've set the background to.
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
I'm currently making an android app in which I'd like the user to be able to change the background image.
I've got 3 images and a screen where I can choose a picture and a button for applying.
The issue:
I can allow the user to see all images in the way I want, but I don't know how to set the selected image as app background.
What I want to do:
I want the user to click a button, which exports the selected image to "bakgrund.png" in "/res/drawable-mdpi" and replaces the current one. This would allow me to easily integrate the background switcher. Renaming of current files also works.
PS: My current background images are located in /res/drawable-mdpi named 1.png 2.png and 3.png.
Easiest way would be to call the setBackgroundResource(image_id) method on the root layout. Like if you have a LinearLayout which has android:id="#+linear" as the root layout in the layout xml, then this code will help:-
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linear);
linear.setBackgroundResource(R.drawable.1);//call this in the OnClickListener's OnClick Method
Firstly, you need different themes which has different backgrounds. So you may use this.setTheme method in your Activity.
Indeed I suggest you, two different layout (with different backgrounds but have same components) and using setContentView during onClick.
I hope it solves your issue.