How to change the style of an Android widget button programmatically? - java

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.

Related

Possible to use FloatingActionButton without it lingering?

I am using FloatingActionButton and it works without issues. Though, I want to change its behavior. I want it to behave like an ImageButton. When I scroll down the layout, I want it to move with the other views and disappear off-screen. I do not want it to stay on top always.
I know there is an alternative solution where I need to copy the style of FloatingActionButton into an ImageView. Though, I wanted to know if it is possible to do what I want using FloatingActionButton only.
Thanks.
Taking from the Material IO documentation, a floating action button has the following usage:
A floating action button (FAB) performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center. FABs come in three types: regular, mini, and extended.
But as you asking, you want to use it as simple view that scrolls with others on the screen. Thinking this way, you can set the most visible characteristic of a FAB less contextual, the elevation.
In your layout.xml file:
<FloatingActionButton
app:elevation="0dp"/>

What's the most efficient way to change the overall theme of the app?

So I got an idea for an app that would change visual theme based on user's selection, something like how a Sub-Reddit would have the option for users to switch between themes. In this case, I would utilize at least 4 themes, each theme changing the color of certain views, such as the background, buttons, image, and etc. I would like to know the the best approach to this. Do I need to keep a list of views that would be affected or something?
I've tried keeping different button backgrounds with different color since setting background color programmatically would reset the background shape back to the default design, but I'm afraid that it will cause the app to be bloated with numerous files. I've tried using the color filter to change the views.
Color id still keeps the filter applied to it, causing it be unusable if user switch to different color, then back.
Hopefully, this question was directed at native development. If so, then you should take a look at the guidelines. You can create your themes in XML and reference them in your layout XML files.
Personally, I would store the theme as a SharePreference somewhere and then when laying out each Activity/Fragment, utilize the user's saved theme.

New to Android, How can I edit my Relative Layout using Java?

I have defined my Relative Layout using the drag and drop tool in Eclipse, so all of my buttons are laid out how I wish. My issue is when I set the onClick listener, that calls a method in another class. So to be able to redraw items on screen, I need to access the layout manager so I can add and remove buttons from the screen as well as update textViews. I have done all of this in a demo I made in Java, and I used a JPanel with GridBagConstraints. Now that I am moving to Android, a system I haven't done much development in, I am at the point where I have to learn some new stuff. For example in my demo I made I could do this:
grid.remove(trueButton);
grid.add(falseButton);
grid.remove(textField);
grid.add(backButton);
Essentially I want to be able to do the same sort of thing in my Android app. If you guys need more info I can provide, I wasn't really sure how much would be needed since I am looking at really just where to start. Everything has been declared in the XML since the drag and drop part of Eclipse does that all for me. It is just the Java part that is giving me some issue.
Why not just setVisibility of the buttons you wish to hide/show? Same with the TextViews.
You can set visibility to 'GONE' and it will be as if the view has been removed (taking up no space in the layout and not responding to touch events.).

Relativelayout circual dependancy

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

Change app background

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.

Categories

Resources