Java ListView class? - java

What is the difference between a listview layout xml and a Listview Java class? I am trying to make it when a user inputs text and presses enter it comes on a set position on the right but when the user receives a reply it would show up on the left side of the list view. Like text messaging on android phones. Does anyone know how i can do this? With a java class or an xml layout? I want animations and dynamic content on the listview as well. Any ideas?

ListActivity provides you all the built-in features of and methods of ListView but only ListView can be added into the whole Activity. By using ListView in xml, you add multiple Views such as Buttons, EditText on the same Activity.

Basically, the xml is for seperating the design (the visuals) from the code (functionality).
Although you can do some funcationality via xml, it's usually better to do it in Java code in your class.
I guess you can create a ListView that will iterate through 2 different list enteries:
The first will show the data (the text in your case) on left and the second entry will show on the right. For each list entery you will create a different xml layout with widgets aligned to the left and right as needed.

Related

Custom listview for image and text, or only text

I'm trying to build an app of my website(Just new to programming Java), getting data from my site is all going fine, but now i need to display it. I did some research online for the best way to do it(CustomListView), but coundnt find the solution yet, hope you guys can help me out.
The site i'm making an app of, is just a site where people can post text, or text with a photo.
Getting the text in my listview is going fine, but i also need images to display.
The problem is, how can i tell the Listview that it only contains text, so it wont display any image field, and how can i tell the ListView when there is a photo found by the post,that it also should display that one?
You can use a tag if the post contains an image or not for example if the data is in JSON form just set a tag
[{contains_image:"null"},{contains_image:"yes"}]
Then in your adapter check this tag if the result is a null set visibility gone
You have to create a layout containing a listView.
You have to create an xml lyout corresponding to one row of your list view.
You have to create an adapter which will populate data to insert in your listView
You have to create an onClickListener on your button to add data in your list
If you are using an ArrayAdapter or a CursorAdapter, add the new item you created to the list or the cursor used by your adapter and (notifyDataSetChanged() is automatically called), so your adapter will update the listview
Source : http://developer.android.com/resources/tutorials/views/hello-listview.html
Have at look at this article : http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
Once you are go through with above set placeholder image which will be show for the items that do no get dynamic images from your server response.

Adding views in layout from the app in Android

I want to develop an Android app, where start page of the app GUI, will contain 4 vertical layouts in the main layout. Now, in each layout, I want to add buttons/slider dynamically from the app (instead of adding buttons/slider dynamically in the source code). That means, initially all these 4 layouts will be blank and when user will select any button or slider in another layout, to add it in any of this 4 layouts, the button or slider will be added in that layout. User will be able to add max 10 views in any vertical layout and the views can be either button, slider or custom view.
My attempt:
First I tried to create 4 vertical layout under the main layout for startup page and I got succeed.
I also find after searching that its possible to add views dynamically in layouts in android.
dynamically adding a view to activity layout
But most examples, add views dynamically in android by running loops, instantiating the desired view class and then add it in the main layout. Although, in this way, views are added dynamically in the layout, it is done by modifying the source code.
Is it possible to write the source code in a way, so that it can be done directly from the app? So that when user will click on Add a slider in "layout 1", a slider will be added in layout 1 and then again, when the user will click on "Add a button" in layout 1, a button will be added at the end of the slider. User will be able to customize button or slider properties. Also, if the user change the value of the slider, the app will remember its value.
Now, next time, when the app will be opened, those views will be there in the layouts, they will not be deleted and the values will remain unchanged (for example, a ticked check box will remain ticked), so I think I also need some kind of storage or properties manager.
My question is, is it possible to do this in android (because I never seen such apps in android) and if possible, any idea, how can I implement it?
I am totally new to android, so my knowledge is limited but I completed the basic tutorials on android app development and I have plugin development experience in eclipse.
Thanks a lot in. I will highly appreciate your help.
Of course it is possible:
Every layout (like LinearLayout, RelativeLayout etc.) extends the ViewGroup-class, which offers the addView-method.
To add a new view (like a Slider) to one of your layouts, just instantiate it programmatically (via new) in your activity and assign the appropriate LayoutParams to it
To store the state of user added content, it is the easiest to use SharedPreferences - a simple key-value-store which holds data over the application's lifecycle
Yes. This is possible. To create the Views dynamically, you simply have to either extend the class of the View or just say new Button(Context, AttributeSet); (Not only for Button's every View has a constructor that takes an attribute set and a context).
Using Layout.addView() you can add any View to the Layout.
Using SharedPreferences you can indicate what View belongs in what Layout.
If you decide to extend the View's class, make sure not to do too much in it. I tried that once and it just gave me an OOM (OutOfMemory Error) because I had a ton a Views trying to do stuff at the same time.

Android Staggered/Multi-line Linear Layout?

For my app, I'm trying to have a section listing the tags in an article but I wasn't able to find an Android Layout that can achieve this.
Basically, I have an array of strings which contains anywhere from 1 to 5 elements. I want to be able to list them in an manner in which they're sort of staggered. Similar to what a Horizontal LinearLayout would do except it goes to the next line if the next view is too large. I'd have to do this programatically and without a ListView/GridView since it's inside a ScrollView.
Is there any way to achieve this?
Here is an edited image of what I mean...
http://i.stack.imgur.com/Hhpry.png

Android varying number of textviews to display

For exmaple I have int variable "number" that is passed throughout the program
and if the number = 7 I want to create 7 identical textviews and create 7 IDs for each of them
if the number = 5 I want to create 5 textviews etc.
My guess is that I should not create any textviews in my xml file and rather create views in my java code. Is this the right approach?
I would do this with a XML holding the EditText and a ListView in another xml, then in the Adapter of that ListView you can inflate and instanciate the EditText XML items.
This way you stay much cleaner and it might be less work if you change style etc. You do not need to take care of a scrollview yourself for more items.
Also you can have items and ListView adapting to screendensity, screensize, orientation etc. without going through a hell of code - if this is a requirement.
If the last item in the list needs to be a button, you could inflate and instanciate such an item layout too in the Adapter.
If your layout is unknown at compile time (for example, the number of Views is not known), then your only choice is to do it programatically.

Android: how to create N listviews?

I have an application that sends a request to an API, where JSON data is returned and displayed. Depending on the user, there will be data for multiple locations with each location being a header and having its own listview. How would I do this in Android? I understand the concept of a single dynamic listview, but how would I generate MULTIPLE LISTVIEWS DYNAMICALLY?
Still not sure what do you mean by multiple listviews dinamically, and first of all why would you need this, but perhaps you just need an ExpandableListView?
A view that shows items in a vertically scrolling two-level list. This
differs from the ListView by allowing two levels: groups which can
individually be expanded to show its children.

Categories