Custom adapter layout - Java for Android App Development

In my application I use a custom adapter to show a list of items. This is shown in my DashboardActivity. To add user input I want to place multiple EditText input fields and a button to add items. However when I am trying to add EditText input fields or buttons in my DashBoardActivity it will show in every item. This because I am using an custom adapter to show my items.
My question is how can I achieve to use a custom adapter with extra layout items under my custom adapter list?
I tried to illustrate it with a simple example:

Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?

CodeMonkeyy said:
Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?
Click to expand...
Click to collapse
I don't quite understand you. But let's try. The adapter is a "tool" that "adapts" a list that you provide to a listview. So it get the items from the list (ArrayList, Cursor etc) and puts them in the listview according to your implementation. So it just fills the listview. It has nothing to do with the layout. The listview uses a listitem layout for every row. If you don't have the edit text and the button in the list item that you provided, then they won't be in the list view items.
Have you correctly created a layout with a listview, and on top of it (or at the bottom of it) to be your edit text and button?

Thanks for your reply.
Sorry I find it quite difficult to explain. (english is not my native language ).
My layout code:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/messageList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:divider="@color/white"
android:dividerHeight="10dp"
android:layout_marginBottom="110dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/message_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:layout_marginTop="10dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop = "40dp"
android:layout_alignLeft="@+id/message_title"
android:layout_alignStart="@+id/message_title"/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:hint="Title"
android:background="@drawable/gradient"
android:singleLine="true"
android:id="@+id/editText_title"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/message_content"
android:layout_marginBottom="70dp"
android:layout_alignRight="@+id/messageList"
android:layout_alignEnd="@+id/messageList" />
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Message"
android:inputType="textMultiLine"
android:background="@drawable/gradient"
android:singleLine="true"
android:id="@+id/editText_message"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/message_content"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignTop="@+id/editText_message"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
And this (quick example) is what I get:
I don't understand why (In this example) my button is getting placed inside my listview.

I got it working. My problem was that I inflated my whole ActivityDashboard layout like this:
Code:
if(convertView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.activity_dashboard, parent,false);
}
And instead I made a new layout for my rows like this:
Code:
if(convertView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.row_layout, parent,false);
}
Now its working the way I wanted

Btw maybe you should initialize the layout inflater once in the constructor so you won't need to do it every time getView() is called.

CodeMonkeyy said:
Anyone? I don't understand how I can place a button or EditText fields outside my listview. Everything I declare in my activity is getting inside my listview, because of my custom adapter. How can I declare something outside my custom adapter and still using the same activity?
Click to expand...
Click to collapse
You can use RelativeLayout, place the EditText and button in a LinearLayout ans set the LinearLayout to android:layout_alignParentBottom

Related

[Q] ScrollView, SetText and scrollTo

Hi,
Im just getting into Android development and picking up pace slowly. Getting confused how people are getting settings style menus with the appropriate colour Summary Line while adding icons for Stars and Checkboxes etc to highlight and tick.
Anyhoo, I have a TextViews inside a LinearLayout inside a ScrollView, seems to work OK. I use SetText to set the Text on the TextViews and I create everything at run time instead of through the XML files.
But the application I have is a polling type application and I need to repopulate the TextViews and redraw the ScrollView in the same position that the user scrolled to previously.
Im accutely aware of problems using ScrollTo after you've used SetText, so I found the following code:
Code:
sView.post(new Runnable() {
public void run() {
sView.scrollTo(0, iTop);
}
});
It works, but there's a screen flicker where it first draws the screen in its normal position then sets the scrollTo.
What is the best way of achieving this, or am I doing everything completely backwards and most normal people wouldn't have this kind of problem?
Thanks
Simon
It sounds more like you might want to consider using a listview instead. It was designed to contain several child views and scrolls and updates seamlessly.
Hi,
Thanks for the reply. Perhaps I am a bit too much of a beginner.
I seem to be favouring TableLayout so that I can display tabular data and have columns resize correctly and have the data presented OK.
I think that's why I went with updating everything at design time because I have a variable number of rows that can be added. IIUC, variable rows (in an XML file) I think can only be created using ListView, but of course a TableRow needs a TableLayout as a parent.
Here is the idea...
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:paddingRight="5px" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
The code I have for TableRows is not dissimilar to, http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout
But of course using setText means I cannot then scrollTo
Duh.
First learning curve mastered.
You only need to add views to containers etc the first time you display the screen.
You can update the text in the views later on without affecting the screen position.
I like this.

Menu Inflate XML problem

Hi, first post here & I did search for an answer first
I'm trying to add a menu to my app, in my onCreateOptionsMenu method I can use
menu.add(0, 1, 0, "Text1");
menu.add(0, 2, 0, "Text2");
etc
It works OK & when I press the menu key it pops up, the onOptionsItemSelected method correctly detects the item selected.
If I try to use the MenuInflater on an XML file, it does not work. When I press the MENU key, it pops up a menu with blank buttons - no text or icons & the number of buttons corresponds to the number of ITEM entries in my XML. The XML looks valid .
Any ideas where I'm going wrong. I would paste the code, but the forum says I cant until I've made more posts.
Thanks.
Still new to this myself, but in my recent menu tries, i had to put in a TextView into the xml to tell it what to say.
<TextView
android:text="@string/menu_button1" />
And define the name you want on that button under the strings file.
<String name="menu_button1">New Game</String>
I may be completly off so take what Im saying with a grain of salt lol
Hmmmm, tried that & it didnt work either. Thxs for the suggestion.
Not a problem the book i have at home had me do some menus, i could post the books complete code later maybe i missed something with what i told you. Its in pdf form so it wouldnt be a big deal. Ill be getting home in around 3 hours.
Maybe its a combination of button and textview together to actually make it work. It was pretty easy last time i did it so im sure its just a small detail being missed. I've slowly learned eclipse isnt able to catch every single error.
is your menu's xml file in the res/menu folder? if not it prob wont work
Yup res/menu
Here is the code for the menu from my book.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="@string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>
And the strings file
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
</resources>
Thats pretty close to what I'm doing, but not exactly. The main difference seems to be your using a LinearLayout & my code uses RelativeLayout. OK, I'll try your code EXACTLY when I get home later. Thanks.
When you say menu, you are referring to the options that pop up when you press the hardware menu button, right?
I ask because... what does that linearlayout code have to do with a menu?
Are you implementing this function?
onPrepareOptionsMenu(Menu menu) {
MenuItem option 1 = menu.findItem(R.id.opt1);
...
}
I thought the Layout may affect how the menu text & icons were displayed & was why I couldn't see them.
No I dont implement onPrepareOptionsMenu, only onCreateOptionsMenu and onOptionsItemSelected - I'd post my code but the forum wont let me yet
I'm following the example from Android Developers "Creating Menus" which only has those 2 methods detailed.
OK guys I've fixed it !!! You'll never guess what it was.......
.... OK, I'll tell you then
In my menu.xml, I had the menu namespace xmlns URI as https not http, I had copied & pasted this from another website. Changed it back & we are working again.
Thanks for the assist - Thanks have been awarded.
Oh man thats a killer...I've had that happen to me a couple of times already
Good example android.text instead of android:text...lol
Glad to here its working though,good luck with the rest of your app.
lol - I either need more or less caffeine - not sure which.
Thanks bud.

Help storing data in preferences

I'm a VB programmer trying to figure out this java/xml. I've got some data in my app that I want to store (when a Save button is clicked) in preferences and have it retrieved at start up if if it exist, but I'm unsure how to code it (the storage/retrieval as well as looping through the widgets holding the data). Any help from somebody having a boring weekend is appreciated. Here is a sample of the data being collected:
</LinearLayout>
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myweighttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My weight is "
>
</TextView>
<EditText
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
>
</EditText>
<TextView
android:id="@+id/myweightextratext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" pounds."
>
</TextView>
</LinearLayout>
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/educationtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I expect "
>
</TextView>
<Spinner
android:id="@+id/ed_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
androidrompt="@string/ed_prompt"
>
</Spinner>
<TextView
android:id="@+id/educationextratext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" yrs of school."
>
</TextView>
</LinearLayout>
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/marriedtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am "
>
</TextView>
<Spinner
android:id="@+id/married_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
androidrompt="@string/married_prompt"
>
</Spinner>
</LinearLayout>
<LinearLayout
androidrientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/incometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My annual household income is "
>
</TextView>
<EditText
android:id="@+id/income"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
>
</EditText>
</LinearLayout>
The content of your layout.xml file does not really help im afraid .
Here are some code snippets to help you out, if you have further questions ask (after consulting google ).
To save something in preferences:
//Get out object to have acces to preferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
//Get an int value named XDA with default value 1
int myInt = settings.getInt("XDA", 1);
To save it again:
//We get an editor object for our settings object
SharedPreferences.Editor prefEditor = settings.edit();
//Now we put our changes in the editor
//We want the XDA value to be 2
prefEditor.putInt("XDA", 2);
//Now save the changes
prefEditor.commit();
You can put the safe part to the onClick action of your button, and load values in onCreate method of your activity.
I guess I'm too big of an idiot right now. Does anyone know where I might find some code (a sample app) that loops through a number of widgets, collecting the data, then stores them in preferences? I think if I could see it written, I could understand it better and modify it to what I need.
Thanks for any help (remember when this was all new to you?).
greydarrah said:
I guess I'm too big of an idiot right now. Does anyone know where I might find some code (a sample app) that loops through a number of widgets, collecting the data, then stores them in preferences? I think if I could see it written, I could understand it better and modify it to what I need.
Thanks for any help (remember when this was all new to you?).
Click to expand...
Click to collapse
What kind of data would you want to collect from a widget?
http://www.kaloer.com/android-preferences
________________________________
http://ron-droid.blogspot.com
Dark3n said:
What kind of data would you want to collect from a widget?
Click to expand...
Click to collapse
I wanted to get data from the widgets that I listed in the xml info pasted in my original post. It could all be treated as string data.
I think what you are looking for is a RemoteView? Set up a RemoteView on a Widget and i think you can use it to retrieve for an example, the text value.
I'm gonna go sleep now, check RemoteView out, if it is not what you are looking for please explain further whats going on .
Dark3n said:
I think what you are looking for is a RemoteView? Set up a RemoteView on a Widget and i think you can use it to retrieve for an example, the text value.
I'm gonna go sleep now, check RemoteView out, if it is not what you are looking for please explain further whats going on .
Click to expand...
Click to collapse
What I have is the 4 widgets in my original post (some EditText and some Spinner) and I need to figure out how to write what I'm guessing would be some type of "For i = 0 to 3" loop that goes through my widgets, collecting the data from each widget, then stores that data as a preference, to be called and put back into the widgets, whenever the app is re-opened. My problem is that I'm a VB programmer, just starting to learn java, so I don't know how to write this...where in a class (java file???) does the code go...do I use a "for" loop or just list the known widget id names one by one to get the values, etc...
That's why I was hoping someone would know of a sample app that might be doing something similar so that I can see the code structure and adapt it to my app.
Create a Preferences.xml file to store the preferences as shown in the link I posted above.
Then create a class to display your view. i.e. If you view is main, display it with setContentView(R.layout.main) as shown below.
Code:
package com.me.mypackage;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
SharedPreferences preferences;
Editor edit;
private EditText mWeightEdit;
private Button confirmButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get a link to the preferences file and an Editor to update it
preferences = PreferenceManager.getDefaultSharedPreferences(this);
edit = preferences.edit();
// get a reference to your edittext field
mWeightEdit = (EditText) findViewById(R.id.weight);
confirmButton = (Button) findViewById(R.id.confirm);
// create a listener for your 'Confirm' or 'Okay' button to know when the user is finished
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// retrieve the values with getText
String myWeight = mWeightEdit.getText().toString();
// Use editor to store the values back into the Preferences.xml file
edit.putString("weight", myWeight);
edit.commit();
}
});
}
Sorry, but put that together in a hurry, and I'm a newbie to Android too. So probably made some mistakes. But maybe that will get you headed in the right direction.
---------------------------------------
http://ron-droid.blogspot.com/
Just create an array of the ids and loop through this. You will have to use findElementById(int) to do this
Sent from my GT-I5700 using XDA Premium App

[Q] layout help

I just started to build my app's layout and I'm confused, so I could use a little help.
I want it to look like this:
- there's a bar with buttons which always stays on the bottom of the screen (like position:fixed in CSS)
- there are two (or more) lists with a dynamic number of records which will mos definitely overgrow the screen size, so I need a scrollbar on them. These will not push down the button bar.
This is easy as hell, I know, but I'm not sure how to do it properly so I won't need to change it later.
It's all about learning to love (and hate) RelativeLayout
http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/
Yeah, should just be a ScrollView with a nested RelativeLayout putting your buttons on the bottom...not too painful
I have a problem with my ListView. I want it to be full size without a scrollbar (the parent of ListView is scrollable), but it shows up really short (only 1.5 rows fit in). Here's my code :
Code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/top_controlbar"
android:layout_above="@id/bottom_controlbar">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- GEOTASKS -->
<LinearLayout
android:id="@+id/geotasks_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="@string/main_header_geotask"
android:textSize="18sp"
android:background="@color/header_bg"
android:textColor="@color/header_text"
android:gravity="center"
android:layout_marginBottom="5dp"
/>
<ListView
android:id="@+id/list_geotasks"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/empty_geo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_empty_geo"
android:textSize="15sp"
android:paddingLeft="5sp"
/>
</LinearLayout>
etc, same for timed and simple tasks
I tried changing the height of the parent layout and the listLayout (all 4 combinations), but it didn't help.
Well **** my rusty sheriff's badge. I can't believe nobody knows the answer.
ScrollViews are weird. The way they stretch to fill content is not intuitive.
I can't remember the solution off the top of my head but do some googling for "ScrollView" and I think there are some answers on stackoverflow.
hth
My problem is not the scroll view, but ListView. Scrollview stretches without problems.
I want to achieve a simple thing - make the ListView display all of it's content without scrolling. How is that not possible ? OMG
At first I used LinearLayouts and added new views to the root layout. That was very ugly and I couldn't register those views for context menus. Then I found out about list views, and I couldn't be happier if I could just do this one thing.
Sorry I misunderstood what you were asking.
What's the point of having the parent of a ListView scrollable anyways? I think you're just thinking about things in a strange way and that what has got you stuck.
ListView has an addHeaderView() addFooterView() if you need static items at the top and bottom, and you know how to put bottom and top control bars using relative layout, why not eliminate the ScrollView?
I think you're making things difficult on yourself by putting something scrollable inside something scrollable.
Your other option is to use views like you were using them, they do support context menus: http://developer.android.com/reference/android/view/View.OnCreateContextMenuListener.html
Hope that helps.
Edit: btw, the reason I am recommending it this way is that even if you override ListView's onTouchEvent, I dont think it will expand to wrap all the content and be scrollable by the ScrollView. Instead it will just sit there as an unscrollable listview that leaves the user unable to reach the content beyond what they can see originally.

textview above spinner

i worked it in relativelayout..then i did in linearlayout
all same result
textvie above spinner.. the textview is a bit more onto the left then the spinner..so design not looking that nice.
<TextView
android:layout_marginLeft="8dip"
android:id="@+id/selectStatement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/select_choice" />
<Spinner
android:id="@+id/dropDownList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I solved it adding marginleft="8dip"
but dont think its best solution... why does spinner take a left padding while drawn?
Use RelativeLayout and align it to a certain position and then add required padding. Add this to the Spinner:
Code:
android:layout_alignParentLeft="true"
android:layout_below="@id/selectStatement"
<!-- If required -->
android:layout_marginLeft="xxdp

Categories

Resources