To clarify this - what I want to do is:
a) to open a http request to obtain some XML
b) parse that XML
c) given the (fixed) number of elements, put them with a bucle in the layout.
What I am missing is the last part. An example is 'twicca' or just the official Twitter application, that fills the layout with tweets. I want to do something like that. How should I proceed?
My first thought was creating a fixed number of TextView and change those TextView (TextView1, TextView2, ...) with the content I wish. But that doesn't sound very professional...
The standard way to do this in Android is with a ListView (link to developer guide), which automatically creates as many items as needed from the data source. Most examples show pulling from a local SQLite database, but after you've loaded your XML items into an array in memory you can use an ArrayAdapter as the data source for the ListView.
Related
Im developing an Android app in Java using Android Studio. I have a layout called activity_way_bill, where it must show a list of trips. Also, I have a layout called item_waybill_trip, where I have labels for displaying the trips details.
I need to insert X item_waybill_trip layouts into the activity_way_bill layout (the X number I will know at runtime). Right now, I just have it included like this, in the XML file:
<include android:id="#+id/trip" layout="#layout/item_waybill_trip" />
But this is a static solution and only allows me to include 1 (or a predefined number) of layouts. I need to include X, and set different texts for each one. How can I do this?
If you want to display the list of element with the same layout but different data than you have to use recycle view.
You can also define the count at runtime and change the count if you needed.
You can visit the site below and check how to use recycle view. :-
https://developer.android.com/guide/topics/ui/layout/recyclerview
As mentioned in the answer by #Mahavir Jain you could use a recyclerview for that but if you want to go the other way, you will have to create the dynamic layouts at runtime and add them to the parent layout using the .addView() method of the parent layout
If your app needs to display a scrolling list of elements based on large data sets (or data that frequently changes), you should use RecyclerView as described on this page.
I am building an Android app for a forum and the API they have created returns 'bbCode' for posts and private messages. Things like [b]bold text here[/b] for bold text and [spoiler]hidden text here[/spoiler] for hidden text which is then revealed by clicking a button of some kind. I am building a parser that will create a mapping between raw text some text to be hidden and type of component PostComponent.SPOILER for example.
How would I go about inflating this parsed and categorised data into multiple Android views dynamically, and then proceed to merge them into one view which will be displayed in a list (to display a list of posts like you see in a forum)?
I can think of a few hacks to get this done (like creating a layout for each post component and having each of them in a non-scrollable listview stacked on top of one another), but I can't quite think of a nice efficient solution.
as I'm very new to Java and Android programming, I'm slowly getting used to the Android studio and the resource system, xml management and so on..
I'm trying to create an activity with a start count of 10 EditTextes, vertically listed in a LinearLayout.
Till this point, I can manage everything with a XML file & hard-coding the 10 EditTextes.
The problem with it is, that I'd like to provide a button that can be clicked by the user to automatically expand the list, for example add 10 more EditTextes to the list while runtime.
AFAIK, I can't solve this problem with XML only.
I know, I can get the layout (XML layout) to an object in my MainActivity class using LayoutInflater().inflate...
Here's my question:
Is it the right way to define the layout in a XML file and modify it with the method described above or would it be better to create the whole layout with Java in my class:
LinearLayout layout = new LinearLayout(this);
// add EditTexts
// set attributes
//setContentView(layout);
Are there any disadvantages using this way or is it the same as doing it with XML ? I mean, to address the created EditTexts later again, I also do have to pass an ID to them, so I also have to create an ids.xml where all those IDs are listed. That sounds like much work for me..
Am I on the right way or are there better options to create/manipulate layouts?
If you want to fill a listview with multiple views, it'll be best to use an Adapter. An Adapter can be used to fill a list, and add rows to this list.
If you want a tutorial on how to create a list: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The best part about an Adapter is, that you are able to create a custom layouts for it. So if you want to have an List Item with only EditTexts or ImageViews, it's easy to create.
In the end, you'll only have 1 layout file for the row and 1 layout file which will contain the list.
Inflating a layout is always easier than to create them like " LinearLayout layout = new LinearLayout(this); // add EditTexts // set attributes //setContentView(layout);"
There are a lot of tutorials on how to create an Adapter, you'll get it!
This is my application require :
- Get data from a server (JSON)
- Show all data report like image above. Data will display as multi page ( do not scroll), use next and previous button to switch
See how I want:
I can get data from server and show data as listview. But I have some problem and need help.
1. How to display data like image I attached. I found some way but seem it is not good
2. How to display data as multi page. I do not know exactly how much data because it depend data on Server, i have to show all data on server. Pages should auto generate depend on data.
Thanks you.
Simplest way to do it would be to create three datasets (let's say arraylists) and keep previous page data in one and current and future in the next one.
Then put an onClickListener that changes the data in listviews and then notify adapter using .notifyDatasetChanged().
This is not an default Android Behavior, So Please do not follow this design. Better to use Load More Functionality :)
You can refer links for that.
ListView to load more items when reached to an end
LoadMore library
Infinite Scroll ListView
I'm writing an Android app that reads a single text file and display it on a TextView.
What I'm doing right now is read the whole file into a String (using BufferedReader and StringBuilder) and display it on a TextView using setText(string). A 700KB text file can take about 2 to 3 seconds before it is being displayed on the screen.
But I've used some other ebook readers on the market and they can display the same text almost instantly. Anyone know how I can achieve this?
Thanks you.
Edit: Many suggest ListView, but it doesn't work for my particular case. This is from my reply to one of the answer: ...[ListView] doesn't work for me for a few reasons. (1) To make the Listview look like a TextView, we have to break the text up on new line character. If I load a single large paragraph, it's just as slow as a loading a TextView. (2) Since a ListView only measures the item on the screen, I cannot know ahead of time the total 'pages' or 'height' of the entire text.
Essentially, the key is to only load the data that you need, when you need it. One way to do this would be to put every paragraph into it's own TextView, which is put into a ListAdapter, which is included into a ListView. There has to be some kind of an index set in place, such that each paragraph knows where in the data file to find. This interface will allow you to load only what you need, when you need it. Your list adapter looks something like this (This code isn't complete, but it should give you an idea at least of what you should do):
class ParagraphAdapter extends ListAdapter{
ArrayList<Integer> mLocations; // Somewhere define this to your locations, I'll leave that for you to figure out
protected View getView(int position,View convertView, ViewGroup parent)
{
mLocations.get(position); // Read the file starting at this position, until the next value
String text; // This is the output of the above
TextView tv=new TextView(context);
tv.setText(parent.getContext());
}
}
It can be noted that Amazon uses a system of paging for the Kindle App. If you have the app, you can see at the bottom of each page what section you are on. Each "page" is probably closer to a sentence or so in length. Then it's just a matter of getting the right page, which can fairly quickly be done.
To add on what #PearsonArtPhoto has said -
I suggest you implement some sort of paging mechanism, to divide your text into pages.
What you should do is split your text according to let's say N +M number of characters per pages.
N = fixed number of characters.
M = number of characters from N to nearest end of line character (so you won't see the last
line being "cut").
I would suggest that if your android device allows you to hold this "in memory" - do that,
and don't try to fetch this from the file one page after the other, but rather fetch from the "in memory" structure - this will improve performance.
Once you scroll and realize you need to fetch the next page, fetch it from the "in memory" structure.
You can use Android Paging Library from Jetpack with custom View or RecyclerView
https://developer.android.com/topic/libraries/architecture/paging
Implement DataSource abstraction that provides List of data ranges
Implement Storage abstraction that provides real data (from Database, Network, Files, etc)
Use RecyclerView and Adapter to display data
Sample app to display content of large files can be found here
https://github.com/YablokovDmitry/FileView
Lucas Rocha built a nice library called Smoothie for that purpose.
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
At the end of those performance tips for Android listview there's a link to an explanation about Smoothie and finally you'll find the library available on github.
Originally described for loading images, the approach applies for loading text as well.
You must to consider if you are using right components.
Maybe it is mutch better to read lines and put them to listview.
try
List<String> lines;
listview.adapter(new ArrayAdapter<String>(...,lines));
And many times that what you see (Look and Feel) is not like you are think.