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.
Related
I am making a game that has a main.xml view (the default one that the game screen is made from. But I have a button for "help" that starts a new view which is just text explaining how to play the game. But I want to add a button to the help menu that will take the user back to the main game. Only problem is when I add a button to the xml file for it, it always crashes as soon as it is called. All the other buttons work fine in main.xml but I cant get the ones in the help.xml to work. Any suggestions?
The way that android's view hierarchy works is actually kind of complicated, and is one thing that I still at times struggle to grasp.
So when you press the help button, and help.xml is supposed to show up, it crashes?
yeah, heres my xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidadding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text goes here" />
</ScrollView>
But it crashes if theres a button inside of it
are you using the new layout in the same Activity or a new one? If the same, try out making a new Activity class (remember to add it to manifest!), it might be a lot easier to then determine your problem if it's separate.
We could do with a logcat output of the crash to help determine what's wrong
I actually yesterday spent some more time with Eclipse Android SDK yesterday and learned how to: center buttons, add buttons, add admob, etc. but I still need to know 2 things:
How do I put buttons side to side?
How do I add sounds to buttons?
P.S. - I am not sure if this is the correct place to post this. I was just spending lots of time trying to the above things out and couldn't, so I figured why not ask the developers pros here at Xda.
I prefer to use RelativeLayouts instead of LinearLayouts.. I feel that LinearLayouts are much more difficult to work with, and are much less intuitive.
When using a RelativeLayout, you could do something like this:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button2"
/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
This would put button1 to the right of button2.
Additionally, if you wanted some space between them, you could specify this in button1:
android:layout_marginLeft="10dp"
To add sounds to a button click... explore the MediaPlayer class.
In the onClickListener of your button, you would have something like media.start()
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
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.
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