i have a couple of questions about activity's
i have a activity which i want to create some options for it. for eg. i want the user to be able to change the font size of text views.
i am a noob in java and eclipse. first i thought i can change the xml values via java but then i found out that i can't and they are read only.
so what is the best solution for creating options which are visual like changing colors and font sizes and picture through entire project?
for eg. i have 10 activities and inside each activity i have some text views. i want to change all of the font sizes. in xml you can create a dimen and all of the text views with android:fontSize:"#dimen/example will have the same size. but in java it takes more code and time.
what should i do ? couple of examples would be nice
thanks in advance
so what is the best solution for creating options which are visual
like changing colors and font sizes and picture through entire
project?
Have a look at the setTextSize() method of the TextView class. This will let you set the size of the text programatically.
setTextColor() will let you change the color.
setTypeFace() will let you change the font.
Having said that about TextView, it applies to all the subclasses of TextView like Button, EditText, etc.
i have a activity which i want to create some options for it.
I suggest you create a singleton instance that encapsulates all the information to change the visuals of your activities like text size, typeface, etc. This instance should then be shared among all of your Activitys.
couple of examples would be nice
No, an SSCCE would be nice. We need to see what you have tried. Code or you didn't do prior research.
Related
I'd like to create a layout displaying a music album's tracks, with the text and background colors being taken from the album cover's color palette. I know how to retrieve the color palette, but I am unsure how to easily apply it programmatically to an entire layout, without manually finding each individual view and changing the colors appropriately. I have only found solutions where themes are changed before creating the views, but those themes are already predefined in the resources, not dynamically generated on the album cover.
Is there some way for me to write a layout in XML referencing colors like "color1" and "color2", and then at runtime assigning an actual value to these colors, such that I don't need to painstakingly modify every view programmatically myself?
I'm not sure it's possible to do that through XML normally, since you're referring to resource IDs and those resources are fixed at runtime. The Theme class also links attributes to resources, so you can't just inject the values you want (as far as I know) into a Theme and then apply that to the Activity. (I don't know a lot about the styling/theming system though so it's possible you can do some wild stuff with it!)
There's a couple of things you could look into though. If you're happy to use the new Material 3 system, that has dynamic colour baked into it, including theming parts of the app with colours derived from content like album art.
But from that outline, it looks like it pulls a single colour from the image, and derives a couple of complementary, neutral colours from it - so it's not pulling multiple colours from the image like the Palette library does. The Material 3 stuff has a particular tone to it (limited colour variation, not too much contrast, consistent look) and if that works for you, great! But if you want a more vibrant palette, you might need to use Palette instead (that link up there shows how you're meant to use it, the examples are basically "apply colour to each view/component in code")
The other thing you could try is using data binding, and instead of using resource IDs and attributes in your XML, you could reference properties on a ViewModel. So you could create LiveDatas for things like primaryColour, secondaryColour etc, and bind to those when setting the colour attributes in your XML. And then by updating a value in the ViewModel, any views bound to that property will see the change. And that binding is defined in the XML, you don't need to know which View is using which colour in your code.
I've never actually used data binding, never mind for colour updates, so I'm not sure how well it would work or if you need to give things a kick to get them to refresh visually - I'd imagine it just works like setting a new value through code, but I'm not sure so I just want to be clear about that!
But if you want smooth colour transitions (e.g. switching albums fades the colours) then I'm not sure there's anything that does that out of the box - maybe the Material 3 stuff does it. You could probably do it with the ViewModel approach though - when you set a new colour, kick off a coroutine that interpolates between the current value and the new one over 1 second or whatever, updating the LiveData every tick.
I have to create number of buttons and textbox, etc. depending upon a certain number. For ex: if the number = 5, I need to create 5 buttons, and if its value is 10, I need to create 10 buttons.
To achieve such a functionality, the conventional xml GUI won't make it up. I need to develop the GUI dynamically. How can I do that?
Take a look at RecyclerView. You need to apply the DataSet (what u receive dynamclly) and then let the Adapter handle all of the Binding.
Take a look at this tutorial, there are many more.
Notice that you will need to create TWO Viewholder (button and a textview) and override the getItemViewType
Add a Linear/Relative layout in xml and on run time according to given number add view(buttons and text views) in this layout.
See this tuts:
https://androiddesk.wordpress.com/2012/08/05/creating-dynamic-views-in-android/
http://www.javacodegeeks.com/2012/09/android-dynamic-and-xml-layout.html
Please have a look at the following image.
This is a text view which I need in my next android app. However, I really don't know how to implement this kind of textview. I only know how to put a normal rectangle style textview. So, how can I achieve this? If textview is not possible, what are other options?
The simplest thing to do is to create an image (a 9patch would be perfect) and put your text over it.
I did a little image for you. Give it a try:
https://docs.google.com/file/d/0B-Ax5zr61_Dvb2QwcmtWUml1Q2s/edit?usp=sharing
Otherwise you can use a Layout that will wrap your TextView (with the rectangle background) and the image of the little triangle below.
You can also try to "paint" it (using Canvas I guess), but seems overkilling to me.
I wonder if there is some library I have missed that allows me to distribute text among different text objects and have it reflow depending on size, newspaper style? it's something pretty easily done in web but I can't find anything like it on Java for Android.
I don't know of any Android Layouts that work that way. You could probably create one that mimics the way html and css work though.
another option is to make your content with the Web technologies that you are used to and just display it inside of a WebView in your application. That would likely be a lot easier than trying to create the Layout yourself.
I had two text fields of different size and different default justification plus offset from the screen border. I simply overlapped them in the same space. Depending on what needed to be displayed, I would null out one field and fill in the other. It kept me from creating 2 different layouts and having to get creative with the text formatting.
I'm trying to make an android app (I'm new in the Android programming world), and I'm having problems creating the GUI.
The point is that I get information of some data that is divided in days... The problem is that I don't know, until I retrieve the information, how many days the GUI should display.
http://img574.imageshack.us/img574/3787/mainscreen.jpg
The grey part will be a TextView, and, also, the black part will be another TextView with multiple lines.
So, the point is, how can I do to have multiple TextView's without knowing before the exact number? I suppose that I can't declare them in the layout.xml
The only solution that I've been thinking about is to create in the layout 7 pairs of TextView and, when I know the exact number, just use what I have to, and don't use the others... (It's a bad solution)
What do you suggest?
Thank you for your answers!
You should create a ListView, which inflates TextView for the items you have.
You can use this example of how to create sectioned ListView, which will look exactly like you want.
I'm not familiar with Android, so other people may offer better, more specific advice.
In the environments I'm familiar with, the problem of displaying an unknown number of items is solved by using not a series of display elements for each data item but a list control. The list component will display as many items as you give it, and can usually be modified to have different appearance for different data, so you're flexible in making it as pretty as you want.
Well you can consider this one also,
Create a table layout in XML, give it a id: TableLayout table=(TableLayout)findViewById(r.id. ....)
Create dynamic TextViews using: TextView day=new TextView(this); day.setText(day name);
Now add this text view to your table layout : table.addView(day);
Run the code for textView creation and addition to table in loop
Hope this helps...............