Text layout across different columns - java

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.

Related

Android: edit colors for entire layout programmatically

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.

Replacing specific pieces of text with visual representations in a readable+writable JTextPane

I have a text editor in which the user can define a pattern for data. A pattern can contain a reference to another pattern. A simple example would be { name: $pattern2 }, where $pattern2 refers to a different pattern which will later be substituted into this one. However, patterns are identified by a UUID, not by a name, which makes it unwieldy to use. To compensate, users have a bunch of buttons that insert the UUID into the editor for them. However, UUIDs are not nice to look at.
A visually appealing way might be possible using a JTextPane (or JEditorPane) to replaces the UUID with a small square containing the referenced pattern's name. The first image below shows what the actual contents of the Document are, while the second image shows how I imagine it is displayed to the user, given two other patterns with the names GoodName and Date.
I have tried using the javax.swing.text.Style object obtained from jTextPane.addStyle("", null), I tried with a javax.swing.text.StyleContext, and I looked at extending javax.swing.text.DefaultStyledDocument, but those mostly seem to concern themselves with changing the way the font is rendered. I don't really see a way to render specific text as a non-text shape. Furthermore, I don't see a way of making the reference "atomic", in the sense that the user cannot select and remove half of the UUID, which would reveal the underlying data which I'm trying to keep hidden.
Is anyone aware of a component that supports behaviour like this? Am I overlooking a Swing feature here? Swing has loads of documentation but it's hard to find what you're looking for if you don't know what it's called.
Perhaps this is a case where a JLayer can be successfully applied.
Instead of replacing the UUIDs with small rectangles, an equally large rectangle containing the name could be drawn over each UUID.

How to create Options for activity

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.

Create a properties frame in Java

I am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.

Android - Help needed with designing a screen with either table layout or list views

I am currently developing an Android app that is to be a counterpart to its sister iPhone prototype.
My task is to recreate the screen from a design mockup from the iPhone app in Android, as shown here:
What would be the best layouts / views to use for replicating this screen in an activity?
Thanks.
Your question does not clear some things up. Also, I disagree with Ted Hopp's answer. I believe he is assuming that the medications will be filled statically, or something like that.
By the looks of your app, I assume you will be filling stuff dynamically, probably with many medications at once, or no medication at all. By the arrows in the iPhone mock-up, I also assume you will want to perform actions depending on the medication selected.
All that said, I would use a ListView. My general concept (the one I'd probably use) would be like this:
You have your data source, and use a Loader/LoaderManager/etc. to fill that into a Cursor. I'd feed this cursor to a CursorAdapter (perhaps a SimpleCursorAdapter, which seems likely and easy by looking at that UI concept --- won't need to customize the adapter part itself). Finally, this adapter would be used in the ListView. This is fairly easy and won't take much code (the ListView-SimpleCursorAdapter-Cursor stuff, the data logic is certainly custom).
You can then manage each of your clicks using the proper ListView listeners, IIRC. And act accordingly depending on the item selected. I'm guessing that the user would, for example, startActivity to see a detail about each medication.
However, as I said, if you have a fixed number of medications (here says an experienced former leukemia patient here, so I always assume medications vary widely!), a single TableLayout would do, but I feel that's not the case.
Ah, and about the layout for each component/med, as I said, a simple layout would do. Probably a LinearLayout with horizontal orientation. Again, very simple to implement with a SimpleCursorAdapter.
There's a nice example of how to do this with a custom row view here. This is probably the cleanest way to go.
The closest built-in widget for this is a TableLayout. Take a look at the Hello Views tutorial project for an example of this in action. You might want to wrap it in a ScrollView.
The right approach is highly dependent on the requirements. As I see it, you have (at least) a few options:
TableLayout
ListView
Something custom
As David noted, the TableLayout is most appropriate if you have a fixed set of data, but you can make it work dynamically too simply by adding child views. The benefit of using the TableLayout is mainly a built-in implementation of columns, including dynamic column sizing based on contents. The down-side is the lack of built-in dynamic support, especially when working with large data sets.
A ListView is a better fit for dynamic and large data sets, but comes with the limitation of not supporting columns. Ted's link lets you simulate columns, but unlike TableLayout these columns have a fixed width (in percentage of the parent's width). The columns are not sized based on content. You could potentially try to do something to measure all the children, but it'd be tricky. It can also be mildly annoying to try to deal with headers in a ListView, though with a little searching you'll find plenty of resources to help you with this.
The third option is to roll your own AdapterView or AbsListView. Of course this involves a significant amount more work, but you could take a look at how TableLayout works and resize the columns based on the content. This would also get you support for adapters and all the benefits that come along with that. This is probably quite a bit of work though, especially resizing the columns based on content.
Consider whether your columns must resize to fit their content or not.

Categories