I want to create a custom view with some text and two buttons all on one line. I need to be able to add multiple (any number) of these views to an existing layout dynamically (needs to be able to scroll). I want to pass a custom object to the view and set the text and buttons. I need access to the button event handlers from the activity. I've looked a little into custom views but I'm still at a loss for how to do what I want. I'm used to .NET custom controls, and I'm looking for the same effect. Any help or example code would be greatly appreciated.
What you want is custom compound view. You should write you own class (usually extending one of the layouts) and whole behavior, inflate the layout the way you want etc.
More of it: http://developer.android.com/guide/topics/ui/custom-components.html
This one helped me a lot too: http://javatechig.com/android/creating-custom-and-compound-views-in-android-tutorial
If you use list activity or list fragment, you will automatically have the many features you have asked for. You only need to create a adapter class for your listview. You can define your layout for your row view(buttons, text etc..) Try to look at the examples on the web for cusom adapter and lists.
Related
My problem is simple: I need to make a layout similar to android.R.layout.simple_expandable_list_item_1 which can fit more than one textview in the layout, as well as show the little '>' symbol that indicates that the tab is expandable.
How can this best be accomplished? My first thought was to use the layout as a background of a linear layout with two textviews in it, but I can't seem to make that work.
Vogella has a pretty good tutorial on how to achieve custom list views through Adapters.
Basically, what you do is you create a Layout with the looks you want for the list items and then you extend an adapter (ArrayAdapter for instance), see section 4. Then, in your activity, you create an instance of your Adapter implementation and populate it from an Array containing the data for the list.
Also have a look at the Developers site for how to make the list scroll smoothly with the ViewHolder pattern.
And if you feel you need more info on the expandable part maybe the hive can help.
in my Android App I show the user some messages. Therfore I have Linearlayouts which contain a textview. Whenever the App receives a new message I add a LinearLayout to my page, set the LinearLayout's background to a custom one and then add the TextView to it in code. What I want to do is to create a custom View, which is basically the LinearLayout with a custom background and the textview in it. And/or a View which displays all the messages by itself.
What I want to achieve is that I only have to add one view to my XML-Layoutfile. In my code I then simply add all the messages to a property of the view and those messages are displayed automatically.
I hope you understand what I want to achieve.
Thank you for your help!
It sound like Andre Classen said a RecyclerView is maybe the best way for your. You can create custom layouts for different Cards, Rows, and display them.
Here you find more about the Recycler View with Official Tutorial.
I have an app where I have to add an informative box in many acivities.
This box is basically a LayoutView with various TextView and an ImageView.
Is there a way to define he whole box in a single class and add this everytime that I need?
I could copy-paste both Layout xml code and methods tha populae the Layou for each Activity, but I want to avoid this (I really hate the redundant code).
Why not use Fragments? They are meant to be reusable.
yoh have tow option
the one is to make your box as different layout then you can include it on other layout
http://developer.android.com/training/improving-layouts/reusing-layouts.html
the seacond is to create custom component
the link below will help you toward this
How can i create custom controls?
I am developing one chat application and i want to create a chat screen layout like following screen.
Currently I am using ListView for my chat texts but the problem is that to read the long chats I have to scroll down,my screen does not scrolls automatically.
I want this bubble kind of chat layout. How do I develop this kind of chat layout?
What you need here is a layout with a header and footer part, having as a main view a ListView.
This ListView should have a custom Adapter to show the message items.
To achieve this bubble-like display, your itemRenderer should be able to deal with states (fromMe - fromOther).
To have the bubbles, you should attach different backgroundDrawable (bubble 9patches pointing to the left and right) to different states.
If you have stuck somewhere, share the relevant code snippets and logcat output, so we can help you further.
try looking at this for creating a custom view for listView.
I would create a parent View and an incoming one and outgoing one that inherit from it and change color and gravity according to the one they are.
You need the following:
A ListActivity, ListView XML Layout.
BaseAdapter which ListActivity Uses.
BaseAdapter getView will inflate each Row.
In Each Row have a row View with you bubble images and your chat text.
In Listview Footer have a EditText View to Type Chat.
Use these for reference:
http://developer.android.com/reference/android/app/ListActivity.html
http://developer.android.com/reference/android/widget/BaseAdapter.html
OK if I understand you correctly all you need to do is implement auto-scroll for the Listview.
if so then try calling smoothScrollToPosition each time something is added. I don't know what your class looks like but maybe can run something like this (pseudo code)
customListview.addNewChat(...);
customListView.smoothScrollToPosition(customListView.getCount());
I am building a Android app and I am a bit struggling with custom Views.
I would like to have a reusable View that consist of a few standard layout elements. Let's say a relativelayout with some buttons in it.
How should I proceed. Should I create a custom view class that extends RelativeLayout and programmaticly add those buttons? I would think that's a bit overkill?
What's the way to do it properly in Android?
Here are some rough steps regarding one way to create a custom aggregate view:
extend RelativeLayout
Provide a constructor in your new class that accepts Context and AttributeSet, making sure to call the superclass first. Do no add anything at this point. Wait until the next step.
override the onFinishInflate method, where you can add your contents through Java code or inflating an XML resource
Add any event handlers, etc
Optionally create a resources file if your widget will require attributes to be set.