[SOLVED][HELP]{NEWBIE}Button not working on android app - Java for Android App Development

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.

Related

[Q] Help me retain my sanity please! Adding app to "share via" chooser

Hi all. I have literally spent days on this, trawling through git source reps, playing with "Intent Playground", scanning logcat over and over again and I'm tearing my frickin hair out. Perleees, put me out of my misery one way or the other.
Developing an app that does funky stuff with text. Works fine except the last TODO which is to add my app to the "Share via" chooser. Source apps for the text I'm targetting are primarily mail clients.
I've added an intent filter (in fact, tried every combination I can think of) but my app never shows in the chooser.
Code:
<intent-filter>
<action android:name="android.intent.action.SEND" /
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
Here's a logcat fragment showing the chooser being created:
Code:
09-30 20:51:07.964: INFO/ActivityManager(124): Starting activity: Intent { act=android.intent.action.CHOOSER flg=0x10000000 cmp=android/com.android.internal.app.ChooserActivity (has extras) }
09-30 20:51:08.374: INFO/ActivityManager(124): Displayed activity android/com.android.internal.app.ChooserActivity: 381 ms (total 381 ms)
09-30 20:51:30.374: INFO/ActivityManager(124): Starting activity: Intent { act=share typ=text/plain flg=0x13000000 cmp=com.htc.android.mail/.ComposeActivity (has extras) }
As you can see, the share action was initiated in this case from the HTC mail client.
Any ideas? A free copy of my app to anyone who can help out. I know it's not much, but what else can I offer
I don't see a "Share Via" in the gmail client.
There is one in Astro file manager though.
Your app should show up if you long press a .txt file from Astro and click Send.
Hi, thanks for the response.
I should have mentioned it works fine as a send to target but it's the share via chooser I want to extend.
Cheers
Found the solution
Hi folks
Finally found the solution. Very pleased about this because I was just about to offer a $50 reward for one
<intent-filter>
<action android:name="share" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
Click to expand...
Click to collapse
There is NO documentation on this that I can find and I've never seen another intent name in this format. Maybe Google only want partners to be able to add apps to the Share Via choosers?
Anyway, up and running now.
Cheers all

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] Simple math, what am I missing?

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() );

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