Help storing data in preferences - Android Software Development

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

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.

[SOLVED][HELP]{NEWBIE}Button not working on android app

Hey people..
Just wanted to say that I am a complete noob in both android developing and java.
So just for the sake of experimenting, i was making a pretty lame Terms and Condition and Class and XML file, for my to be developed android app.
So according to me when the person hits Accept the app continues and when the user hits decline . It should finish.
I am having no errors. But in the emulator, when I click both Accept and Decline. Nothing happens. It is understandable for the Accept button as i havent started developing the next java class.
But when it hits the decline button, it is supposed to get closed.
Please help out the newbie here.
Thank You.
I am giving you the android manifest, and the First Java class for the terms and condition
The Java Class ( Terms.class)
_____________________________________________________________________________
Code:
package com.example.cplus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Terms extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button accept, decline; // acept and decline button
accept=(Button) findViewById (R.id.button1);
decline=(Button) findViewById(R.id.button2);
accept.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent next= new Intent(Terms.this, MainActivity.class);
Terms.this.startActivity(next);
}
});
decline.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
}
____________________________________________________________________________________
the android manifest
______________________________________________________________________________________
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="xda wont let me post outside links"
package="com.example.cplus"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Terms"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.cplus.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.cplus.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
______________________________________________________________________________________
the XML file (Main_activity.xml)
_____________________________________________________________________________________
Code:
<RelativeLayout xmlns:android="xda wont let me post outside links"
xmlns:tools="xda wont let me post outside links"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="75dp" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Google Apps for Business (Online) Agreement (the “Agreement”) is entered into by and between Google Inc., a Delaware corporation, with offices at 1600 Amphitheatre Parkway, Mountain View, California 94043 (“Google”) and the entity agreeing to these terms (“Customer”). This Agreement is effective as of the date you click the “I Accept” button below or, if applicable, the date the Agreement is countersigned (the “Effective Date”). If you are accepting on behalf of your employer or another entity, you represent and warrant that: (i) you have full legal authority to bind your employer, or the applicable entity, to these terms and conditions; (ii) you have read and understand this Agreement; and (iii) you agree, on behalf of the party that you represent, to this Agreement. If you don&apos;t have the legal authority to bind your employer or the applicable entity, please do not click the “I Accept” button below (or, if applicable, do not sign this Agreement). This Agreement governs Customer&apos;s access to and use of the Services."
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
android:layout_marginTop="350dp">
"
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="Accept" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="Decline" />
</LinearLayout>
</RelativeLayout>
again Thank you in advance
Try
Code:
Terms.this.finish();
instead.
An easier way of doing this:
In the xml file:
Code:
<Button
...
android:onClick="decline" />
And in your Terms.java:
Code:
public void decline(View v) {
finish();
}
This works for Android 2.0+.
It is important that you need a parameter of type View. In the onClick attribute you specify the name of the method to invoke when the button is clicked.
You do not need to implement any listener in the java code if you use this solution.
---------- Post added at 02:01 PM ---------- Previous post was at 01:53 PM ----------
One strange thing I noticed:
Your layout is called Main_activity.xml but you set the layout with the id R.layout.activity_main.
Is it the right one?
Please attach the project here i will help u with this.
Because figure out the problem form the coding is quite difficult for me..
You should look at this: http://blog.donnfelker.com/2011/02/17/android-a-simple-eula-for-your-android-apps/
I used it and it's very easy to implement. It's done properly too so after every update it will appear again, but it will only appear that one time after each update. It's a good place to put a changelog as well.
I think its because of the
Code:
tools:context ='some other class.'
when i deleted that.line.
The problem was solved.
anyways thnxz for your concern.
Sent from my GT-S6102 using xda app-developers app
hiphop12ism said:
I think its because of the
Code:
tools:context ='some other class.'
when i deleted that.line.
The problem was solved.
anyways thnxz for your concern.
Sent from my GT-S6102 using xda app-developers app
Click to expand...
Click to collapse
I am glad that you were able to solve it. To make everybody know that, you could add a [SOLVED] tag to the thread title.
You say you're a noob to java,
I would advise that you first master java and then get into android remember that learning is a liner process you can't just go to the to from the first step
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
You say you're a noob to java,
I would advise that you first master java and then get into android remember that learning is a liner process you can't just go to the to from the first step
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
The problem was in no way related with java.
Try setting the clock able preference to true
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
Try setting the clock able preference to true
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
He has already solved it.
I have done java till constructors and deconstructors. I am not having that much. I know some other languages fully like php, pascal, xml and c/c++. I am not having that much problem in learning android but in game development .
Sent from my GT-S6102 using xda app-developers app
hiphop12ism said:
I have done java till constructors and deconstructors. I am not having that much. I know some other languages fully like php, pascal, xml and c/c++. I am not having that much problem in learning android but in game development .
Sent from my GT-S6102 using xda app-developers app
Click to expand...
Click to collapse
Which engine do you code on
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
sak-venom1997 said:
Which engine do you code on
Sent from my GT-S5302 using Tapatalk 2
Hit Thanx Button if i helped you!
Click to expand...
Click to collapse
Please use a new thread for this. You could also look at this one which was started recently: http://forum.xda-developers.com/showthread.php?t=2239889
As i have just started it.
I am doing it on eclipse only.
With regular java coding
Sent from my GT-S6102 using xda app-developers app
hiphop12ism said:
As i have just started it.
I am doing it on eclipse only.
With regular java coding
Sent from my GT-S6102 using xda app-developers app
Click to expand...
Click to collapse
What do you want to say with this?
I m actually having some problems in understanding the concept of animation. I am probably going to start a new thread, in which i will put a code of a simple animation. Then there are some particular codes, whose purpose are unknown to me. I want you people to help me out with that. Ill be glad. I will start the thread by tommorrow
Sent from my GT-S6102 using xda app-developers app
hiphop12ism said:
I m actually having some problems in understanding the concept of animation. I am probably going to start a new thread, in which i will put a code of a simple animation. Then there are some particular codes, whose purpose are unknown to me. I want you people to help me out with that. Ill be glad. I will start the thread by tommorrow
Sent from my GT-S6102 using xda app-developers app
Click to expand...
Click to collapse
Yes, that is good. :good: Start a new thread for every topic. BUT use search before.

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

Custom adapter layout

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

Categories

Resources