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.
Related
I have been working on an app that adds views programmatically to a linear layout.
The problem is if I add too many views it will go off the screen.
I would like to know how to check if a certain child has hit the end of the same view group so I could add it into another layout (a linear layout below the first one) before it "flows" and go off the screen. How might I accomplish this?
Rather than reinvent the wheel yourself, I suggest that you check out the FlexboxLayout project by Google: https://github.com/google/flexbox-layout
A FlexboxLayout will automatically give you the behavior you're describing, plus the potential for much more.
Well, there are a good number of ways you could implement this in android other than going through this hustle. What ever you are trying to do at the moment may fall under one of the following cases.
Creating views programmatically most likely means you have a dynamic data set probably from an external source.
Your dataset is limited or static but just more than the average screen can display.
if any of the above apple then you are better off using a ListView or RecyclerView (Recommended). That way your data is full displayed as a scroll-able list and you don't have to worry about some items or views not showing or going of the screen. This can range from simple string list to complex nested views.
This will be very efficient as it will automatically handle optimization and usage of memory as well as performance.
I want to create a grid or table of fixed number of rows and columns(Ex. 6x6 grid) in a layout of Android Studio. I tried using GridLayout and GridView but it requires you to put 36 EditText(Plain text) Views in it for creating a 6x6 grid. Same is the case with TableLayout where you can only insert TableRows but cannot fix the number of columns.
Bottom line is, I want a 6x6 grid which has only a 6-letter word(one character in each cell) in the beginning and one letter is typed in every turn to make a word with the existing letters.
For this, from any cell, I should be able to read the characters in the adjacent cells. I don't think creating a GridView or TableLayout and creating 36 objects of EditTextView will be the best idea. Is there a good and efficient way to do this? I need the .xml file code and also its Java class file's code.
Why you don't think that creating a layout holding views in Android framework is good and efficient way for exposing some data in a graphical interface? This is the only reason for which View class even exists. It's a main building material of your application's GUI.
Also you don't need to create it by hand. You just need to be able to address your ViewGroup (Layout) object from your Java code where you build your Activity instance. From there you have an addView() method and you can add views in a simple loop (notice that this way you can create grids of every size, not only 6x6).
Please familiarize yourself with official Android Developers site where you can read pretty much about anything relating Android Framework. For more info about layouts click here.
Try this library it might help you.
https://github.com/InQBarna/TableFixHeaders
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!
I am trying to create a top bar for my app that shows the users level, coins. So i have an image view that displays the coin icon and a textview that displays the number of coins, if the number gets high it overlaps the image rather than pushing back the image and keeping everything aligned, is there a way to do that?
Without knowing your layout XML this is pure guesswork, but I'm assuming these two views are in a RelativeLayout. If so, add to one of them an attribute like this:
android:layout_toLeftOf="#id/otherViewId"
Options available are layout_toLeftOf layout_toRightOf layout_above and layout_below.
Post your layout XML and I'll update this with a better answer!
If you are using the relative layout then you can use the z-index attribute of views to manage the ordering.
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.