I have two layouts that I want to be able to switch between using a longclicklistener in a fragment. The layouts are identical except for colors and backgrounds, and the id's for views in each of their xmls are the same, so that code can be run using both layouts the same way. I'm wondering what code to use to change these layouts. Thanks for your help.
If the two layout are the same except the background colours , you can just change the ui components background colours at runtime programmatically.
You can used ViewSwitcher view
http://developer.android.com/reference/android/widget/ViewSwitcher.html
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.
I want to achive that I have a transparent background BUT with a picture !
So if got this in the XML file of my layout :
android:background="#drawable/wallpaperpic"
Now I want to transparent it so the background menu (f.ex. widgets or app menu) is seeable but also my drawable/wallpaperpic and the rest of my buttons etc are normal (100%).Hopefully I was able to express my self !
My Background : https://amazingpict.com/wp-content/uploads/2015/04/polygon-texture.jpg
I want to have this
WHERE you also can see my background picture a bit
Try to set the alpha property of the view, like:
yourView.serAlpha(10);
If, as I'm guessing now, you have two layouts, one within the other, where the parent layout has the background, you can do this in the child layout:
android:background="#null"
This way, you can see the background, and your elements are intact.
Option 1 is to convert your image to a PNG and add an alpha channel.
Option 2 (which I would recommend) is to have a separate view that acts as your background, and set the alpha property for this view only. You can use a FrameLayout to put this background view underneath your other controls.
In my opinion use TintableBackgroundView.
TintableBackgroundView.
If you launch your activity the normal way, the system will not render anything behind it (i.e. the launcher will be completely gone - it could even remove it from memory).
You'll need to implement your activity as an alert window. This answer has details on how to implement it.
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.
i have four images in a row at the top , on click of each icon i want to change the underlaying background (image) and the controls on the layout , this way it achieves tab like structure and behavior , i want to know whats the best way to achieve this ? I think i will have four layouts each layout having one image highlighted showing that tab selected and corresponding components on layout, and will change this layout when user clicks on image.
Is this a good idea to achieve this ? or i have different solution available ?
its nice if u give me some idea about necessary features or API or layout component related code
Suggestions are welcome thanks!
That is not a good idea. You should use android's tab architecture. Here is a example at developer.android
you can change layouts by switching views to invisible and visible. but it's not a good idea when you want to change 4 layouts.
It becomes hard to maintain the code if you have 4 layouts switching.
It's better to use Tabs which will help you in preserving the state of the each layout.
Customize the tabWidget to make it look like you have 4 buttons on the top, not the tabs.
HTH.