Hi people!
I'm trying to learn how to develop android apps. I'm not trying to become a developer but for fun and learn this. So I go step by step.In the past I just have very basic visual basic and fortran experience...I don't know java lang. But while searching I found out that android is based on java. so I grabbed netbeans and jdk. and it was easy and understandable to do some basic math(with tutorials) with it, like + - * /
So I thought I'd give a try to appinventor. Ok easy enough.Done basic math. But I want to learn the code itself.
So I'm dealing with eclipse right know. I learned some basic stuff which you can not learn with appinventor.
But when it comes to basic math, I'm stuck!
Let me explain:
with appinventor it's easy to make this (pics are at the end of the post)
when I try to make this with eclicpe...I'm going nuts. I couldn't find any tutor.
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Lorem Ipsum</string>
<string name="input"></string>
<string name="butcel">Button</string>
<string name="result">result</string>
<string name="ClickHandler">ClickHandler</string>
</resources>
Click to expand...
Click to collapse
Forum does not let me enter my main.xml and main.java codes. so I atteched the pics and txt files below.
this gives me a "app has stopped unexpectedly" error
what I'm trying to do is
1)user inputs a number
2)clicks on a button (there will be several buttons in my app,this is just a test app)
3)application does some math with that number
ie. ((number*5)/2)
4)textView1 shows the result
Should it be simple like in appinventor or am I missing sth in my codes?
And I'm not sure but do I need to add a java math function or sth like that in my app to do simple math like this?
Any advice is appreciated.
Thanks in advance.
The first thing to check is your AndroidManifest.xml file. The Eclipse ADT plugin should create this file and include the main activity for your app automatically if it has been installed. However, an app will throw the error you are receiving if an activity is run that isn't listed in the manifest and it's very common(at least for me) to forget at first to add a newly created activity to the manifest. Below is an example of an AndroidManifest.xml file from the Hello, Android tutorial.
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Hi there desisamay,
to be honest i did not really look and your code to try and find the error, but i did write you an example app you can expand or use to learn .
Attached to my post is the example app and my whole eclipse project directory of it.
The app has an edittext to input a number, a button to start the calculation x*2/5 and a textview to show the result.
I hope you enjoy it and if anything is still unclear, just ask!
when the activity starts you try to get the text of the edittext and convert it to string which is null since the edittext is empty at the beginning. You should get the input when you click the button (and maybe even check if the edittext is empty)
Dark3n said:
Hi there desisamay,
to be honest i did not really look and your code to try and find the error, but i did write you an example app you can expand or use to learn .
Attached to my post is the example app and my whole eclipse project directory of it.
The app has an edittext to input a number, a button to start the calculation x*2/5 and a textview to show the result.
I hope you enjoy it and if anything is still unclear, just ask!
Click to expand...
Click to collapse
Thank you very much! This was exactly what I was trying to do! I was trying to do this with onClickListener and defining values... But your lines are clear and understandable and now I know how to make definitions and basic math functions. I think there'd be more lines to write with onClickListener. But this way is simple and works like a charm. Thank you again.
Also I've added
android:inputType="numberDecimal|numberSigned"
Click to expand...
Click to collapse
to EditText in main.xml just to be sure the user inputs numbers only. maybe just "number" will do the same.
But one more question:
we write the result to output like this
output.setText("The result of " + mInput + " times two, divided by 5 is " + mOutput);
do we have to use some text in it? if I try just
output.setText(mOutput);
eclipse warns me
The method setText(CharSequence) in the type TextView is not applicable for the arguments (double)
Click to expand...
Click to collapse
and underlines setText with red. but we 've already defined mOutput before like double mOutput = (mInput*2/5);
@MongooseHelix
My AndroidManifest.xml was the same as you posted. Since I only have one action. Thanks.
@nemoc 23
I was trying to that. get the input when button clicked. Convert to string of course and do some math. But I couldn't.(now I can ) But you're right for checking the edittext field after clicking the button. I'm pretty sure if you click the button with an empty edittext area, app will give an error.(i tried and it did)
When writing my little app I'm planning to add this and warn the user with a toast text.
output.setText( mOutput.toString() );
Related
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.
I understand that home-replacement apps are a little (a lot) different than regular app. How exactly would I go about binding (if that's the right word) a view() to the home. When the home button is pressed, I want my home-replacement to be in the list. How would I do that? What specific property or activity() would allow me to do that? Thanks in advance
Code:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
In the android manifest for your specific activity
Thanks
Thank you so much If you would like to know what I'm working on, you can visit my site at futurinnovations.blogspot.com. I would like all the help I can get.
Now that I can bind a gridView to the home, I need to know how to launch applications from that gridView. The gridView is made up of all the application icons, but I don't know how to make it to where the applications are waiting behind their icons. Oh, and can someone help me with making a free-form surface to place applications on that can be freely dragged and dropped? I have attempted to get the code from the example "Making Sense of Multitouch" from the Google Dev Blog to work. I enter in the code (not copy and paste, since I have my own variables and such) exactly like they do, run the application, and all I get is a blank screen. What am I doing wrong?
Sounds to me like you aren't inflating the XML
Sent from my ADR6300 using XDA App
Ummmm...it doesn't have an xml. I'm pretty sure that it is dynamically created. What would I put in the default xml? and how would I inflate it?
I'm essentially trying to create a MadLibs app for my kids. I'll type in the text of the books they already have and they can play with it in the car. But I'm a noob and I haven't done much else other than the first few simple apps in tutorials - my brain got tired trying to figure out sqlite stuff, so this all went on the backburner and now here I am again giving it another effort.
So I was thinking something ridiculously simple, like just having a textview string of the first part of a sentence, then an edittext with a hint set for the type of word to fill in, followed by the rest of the sentence in another textview string. Lather, rinse, repeat. Easy, right?
I can't seem to figure out how to get the objects to flow properly in the layout... First problem is that the edittext is gigantic compared to the textview. I could probably figure that one out easy enough, but my second problem prevents me from actually seeing the effects of my changes, and that problem is that the text doesn't flow onto the next line.
I've tried linearlayout and relativelayout, but I'm turning here for some guidance before I waste countless hours. Should I just build each paragraph (textview) as a single string comprised of other strings and edittext variables?
Any help would be great appreciated. And remember: go easy on me.
Here's my current activity_main.xml:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBody01"
android:weight="1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint_adjective"
android:weight="1" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textBody02"
android:weight="1"/>
</LinearLayout>
etnpnys said:
I'm essentially trying to create a MadLibs app for my kids. I'll type in the text of the books they already have and they can play with it in the car. But I'm a noob and I haven't done much else other than the first few simple apps in tutorials - my brain got tired trying to figure out sqlite stuff, so this all went on the backburner and now here I am again giving it another effort.
Click to expand...
Click to collapse
Could you post a quick picture (just a rough one in paint or something) showing the desired layout ?
Absolutely. I'll mock one up and get it on here shortly.
Sent from my SPH-L710 using Tapatalk
PicomatStudios said:
Could you post a quick picture (just a rough one in paint or something) showing the desired layout ?
Click to expand...
Click to collapse
Here's a little bit of how I'm thinking this should work out pretty easily. In the end, I would like to have the filled-in words in bold red text and the rest of the body paragraphs as regular black text, and I would like to make the background black to hide the body of text. I would then have a button that calls a function to change the background to white to make the body text visible "onclick" -- or maybe toggle between black/white/whatever.
The first hurdle is right here with the layout. I mean, this is REALLY simple, right?
View attachment 2380717
etnpnys said:
Here's a little bit of how I'm thinking this should work out pretty easily. In the end, I would like to have the filled-in words in bold red text and the rest of the body paragraphs as regular black text, and I would like to make the background black to hide the body of text. I would then have a button that calls a function to change the background to white to make the body text visible "onclick" -- or maybe toggle between black/white/whatever.
The first hurdle is right here with the layout. I mean, this is REALLY simple, right?
View attachment 2380717
Click to expand...
Click to collapse
Maybe not that simple
As I understand it you want the text to flow continuously left-to-right, top-to-bottom, a bit like floating CSS layouts ?
That is you might want it to do...
Mary had a [edit text] lamb. Its
fleece was white as snow.
Where that's 2 TextViews with an EditText between them ?
You will struggle to do that on Android, since there is not built in concept of 'flow' as you may be imagining.
The closest you will get is a RelativeLayout but you'll have to work hard to line the views up correctly.
You could try something like this, or maybe this. The second one links here where there is somebody's FlowLayout code towards the end of the page. I haven't tried it but it looks close to what you need at first glance. The concept appears to be a ViewGroup which calculates where its children should be and positions them accordingly.
You get my ideas exactly. I'll look into those! Much appreciated!
Sent from my SPH-L710 using Tapatalk
Just to button this up, I wanted to chime in to let everybody know which direction I went.
Basically, I have a vertical linear layout comprised of:
several editText fields for user input. @hint points to strings that indicate what kind of word (noun, adjective, etc) needs to be filled in.
a "Create" button
a "Clear" button
a textView
Behind the scenes, I set a series of "sentenceFragment" variables that contain the body text. Those all end with a "[" and start with a "]" so that the filled-in words can be easily seen once the story is built. Users fill in the editText fields and click the "Create" button. When clicked, I used getText on each of the editTexts to store each as a string, then a "paragraph" variable is built from concatenating all of the sentenceFragments + editTexts, then I used setText to the textView to display the entire story below.
The "Clear" button then uses setText on the textView and the editTexts to blank them out.
So, in the end, the user is presented with stuff like:
Sometimes you just want to get a [green] car. In your search, you find several [bugs] on the seat.
Thanx for you help, everybody! Now I'm looking into putting a spinner at the top to let the user select different stories... Anybody wanna point me in the right direction there?
Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
coyttl said:
Hey all -
I'm getting into an area I hadn't gone before - drawable XML files, and am stumped, since what I think should work is giving me an exception.
Some background -
I have multiple images, used as a background. Then there are a couple other images used for foreground symbols. (These are for markers on a map.) So, for example, I have 5 different colors, and 5 symbols. Instead of creating 30 different markers (5 symbols + blank) I figured I could include the resources, and then use a list-layer to show the symbol on top of the marker. Would save some room when the APK's created.
So, I made a simple test, like this. XML file is simply named "marker":
Code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="34dp"
android:drawable="@drawable/marker_green_blank"
android:left="0dp"
android:right="20dp"
android:top="0dp"/>
<item
android:bottom="20dp"
android:drawable="@drawable/marker_symbol_int"
android:left="3dp"
android:right="19dp"
android:top="4dp"/>
</layer-list>
Oddly, when I go to use this, I get an exception when it's tried to be used:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Any thoughts? I obviously missed something - probably something idiotic, since I do that a lot.
Or, is there another way to combine two images/res's together?
--Mike
Edit:
I forgot to add - this is how I'm creating the 'icon' to add it to the map. The second line is map-specific code and may not really apply here. I'm starting to think that the first line - BitmapDescriptor - is causing the issue:
Code:
BitmapDescriptor marker_red_blank = BitmapDescriptorFactory
.fromResource(R.drawable.marker);
<!-- Code that sets other options using a MarkerOptions object ... -->
opt.icon(marker_red_blank);
mark.associatedMapMarker = MapViewFragment.this.mMap.addMarker(opt);
Click to expand...
Click to collapse
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
PicomatStudios said:
Although I haven't used BitmapDescriptors, I do use layer-list a fair bit to overlay shapes, backgrounds etc.
Looking at the documentation for fromResource...
'Creates a BitmapDescriptor using the resource id of an image.'
It seems that you're not passing it an image, you're passing it a drawable (layer-list in this case).
Click to expand...
Click to collapse
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Drawable Button XML resource selector
This technique could be used for animation of XML drawables.
Keep separate drawables in your res/layout folders as usual.
switchable_drawables.xml ( put this in res/layout too)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Focused-->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/active_button"
/>
<!-- Button Focused Pressed-->
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Pressed-->
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/active_button"
/>
<!-- Button Default Image-->
<item android:drawable="@drawable/inactive_button"/>
</selector>
coyttl said:
Thanks, I'll look into that. I had thought I tried to create a bitmap out of it, but it still gave the error - but since my memory is flaky, I'll do it and see what I get.
Cheers-
Mike
Click to expand...
Click to collapse
Hey guys -
Thanks for the info. Unfortunately, same error. The code now looks like:
Code:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferQualityOverSpeed = true;
opts.outHeight = 34;
opts.outWidth = 20;
Bitmap icon = BitmapFactory.decodeResource(MapViewFragment.this.getResources(),
R.drawable.marker, opts);
BitmapDescriptor marker_green_blank = BitmapDescriptorFactory.fromBitmap(icon);
Creating options to generate the bitmap, setting desired height and width, and then using the decodeRecource function, creating a Bitmap, then using the .fromBitmap to turn it into a Descriptor. When the GMaps tries to access it, same null error. I may post this over on StackOverflow, since looking at the docuemtation and other examples, this should work.
Thin ice here...
So here's an excerpt from the Android Manifest.xml of Alreader 1.7.7:
Code:
<activity android:configChanges="locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|uiMode|screenSize|smallestScreenSize|fontScale"
android:label="@string/dialogopen_library"
android:launchMode="singleTop"
android:name="com.neverland.alr.AlReader3GridCatalog"
android:parentActivityName="com.neverland.alr.AlReader3Activity"/>
The activity "com.neverland.alr.AlReader3GridCatalog" is, as far as I can tell, what calls the Library. At least ADW Launcher thinks so, although attempting to create a shortcut fails. In fact, every activity listed for Alreader fails from outside the app as far as I can tell. I would like to be able to go straight to the Library. My reading--probably insufficient--leads me to think that "android:exportable=true" needs to be added somewhere in this mess to make that possible.
I've tried that but apktool throws out all kinds of errors when trying to recompile the app.
Am I being too simple?
nmyshkin said:
"android:exportable=true" needs to be added somewhere...
Click to expand...
Click to collapse
Make that:
Code:
android:exported="true"
You could also add your own intent filter:
Code:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
Which turns it into a regular launchable app.
It also makes the android:exported="true" by default without the explicit line.
Renate NST said:
Make that:
Code:
android:exported="true"
Click to expand...
Click to collapse
OK, so that was just a stupid mistake on my part I actually did use "exported" when I tried, but apparently my apktool version was just too old. I got the app to recompile with the newest version of apktool, adding android:exported="true" to the section where the library activity is mentioned. So far, so good. Then I signed the app and it actually installed!
When I call the activity I actually get a blank screen with a little band at the top which says "Library" (android sarcasm). The menu button brings up a few items, each of which tosses me back out except for a screen formatting option: one or two columns, etc.
So...I'm guessing that more of the activities associated with the Library need the same treatment in the Android Manifest file for the whole shebang to work.
Edit: just to be a bit OCD, I added the exported phrase to each and every activity mentioned. This yields a similar result for each: a blank page with a title bar and some menu-button options that don't work. I am trying:
Code:
am start -n com.neverland.alreader/con.neverland.alr.AlReader3GridCatalog
as an example. I am wondering if the declaration of a "parent activity" in each section:
Code:
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|navigation|orientation|screenSize|smallestScreenSize|touchscreen|uiMode"
android:label="@string/dialogopen_library"
android:launchMode="singleTop"
android:name="com.neverland.alr.AlReader3GridCatalog" android:exported="true" [B]android:parentActivityName="com.neverland.alr.AlReader3Activity[/B]"/>
has something to do with my inability to get to a specific activity from outside the app.
nmyshkin said:
I am wondering if the declaration of a "parent activity"...
Click to expand...
Click to collapse
parentActivityName really only has to do with navigation, not a real dependency.