Related
Hey guys,
I'm new to android development and have been working through the Android Development guides on Google's site. I'm using what I've learned so far to create a simple app that enables the user to press a button and enter a second screen. I'm incredibly confused at this point and any help would be much appreciated. I have a class Project001.java with a button and would like class Project002.java to be launched when Button 1 is pressed. I have main.xml providing the layout for Project001 and main2.xml providing the layout for Project002. Here is what I have so far in Project001 (which I know is wrong).
package com.smith.Project001;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.smith.Project002;
public abstract class Project001 extends Activity implements OnClickListener {
Button button = (Button)findViewById(R.id.Button01);
abstract void setOnClickListener(View v);
// Implement the OnClickListener callback
public void onClick(View v) {
//do something when the button is clicked
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = new Intent();
myIntent.setClassName("com.smith.Project001", "com.smith.Project002");
startActivity(myIntent);
}}
I've been searching through the Google archives and can't seem to put all the pieces together. Thanks in advance for your help!
Try the tutorial here. The second one, called menus and intents, I think, shows starting one activity from another.
http://www.vogella.de/articles/Android/article.html
________________________________
Unrevoked forever
SkyRaider Sense 3.5
Radio 2.15.00.09.01
Try something like this in your main activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startNewActivityBtn = (Button) findViewById(R.id.newactivitybtn);
startNewActivityBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
// put your intent and start activity here; should work fine
}
});
}
Click to expand...
Click to collapse
You really don't need abstract or implements. And there is no reason to import "Project2" into "Project1". Project2 should handle all that goes on in the new window. If you need to pass data to Project2 you can do that too
Thanks for the help so far guys,
I'm still having troubles with this code (new to development and very much appreciative of your help). I've done a lot of reading and I don't think I need to use intents because I have to data to pass from Project001 to Project002. All I need is to click the button and to move from Project001 to Project 002. My problem right now is that as soon as I run the app in the emulator, I get a force close. I've been trying to find a solution but haven't been able to. I've tried to implement the method you gave me and took the entire tutorial two posts up. Here is the new code. Again, any help is much appreciated.
package com.smith.Project001;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Project001 extends Activity implements OnClickListener {
Button button = (Button)findViewById(R.id.Button01);
void setOnClickListener(View v) {
}
// Implement the OnClickListener callback
public void onClick(View v) {
setContentView(R.layout.main2);
//do something when the button is clicked
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startNewActivityBtn = (Button) findViewById(R.id.Button01);
startNewActivityBtn.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
// put your intent and start activity here; should work fine
}
});
}
Always look into logcat, it's REALLY helpful. Anyway, here's a couple of things I see wrong;
1) I'm not sure why you're having two layouts for one activity, just put them into one layout file
2) setContentView() needs to be in onCreate()
3) You can't call findViewById() before the layout file has been processed by setContentView()
Try this example. I think it does exactly what you want. It has two xml files (screen 1 and screen 2). When you press the button on the first screen, it displays the second screen.
http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
________________________________
Unrevoked forever
SkyRaider Sense 3.5
Radio 2.15.00.09.01
Yep, that should work.
Please note that on the second Activity, a "Previous" or "Return" type button is not required; the hard Back Key on Android phones does the exact same thing as the code.
I've run into a little trouble and I'm wondering if someone can help me out.
I have a button with several sound effects that go with it. I'm wanting to play a different sound each time the button is pressed, but I just can't get things to work out. Can someone show me the way? Any help would be huge. This is the only thing keeping me from finishing. Here's a little code:
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.finn_wordtoyourmother);
Button SoundButton = (Button)findViewById(R.id.Button1);
SoundButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
});
}
}
If you want to play a different sound with each click, then you'll have to keep a global counter variable or global string that you change to the next audio file name with each click of the button--sort of a circular buffer. The code you have there tries to play both sounds on the same click and likely mixes them together at the same time if "playSound()" is asynchronous, but I don't know what the SoundManager class is so I have no idea.
Gene Poole said:
If you want to play a different sound with each click, then you'll have to keep a global counter variable or global string that you change to the next audio file name with each click of the button--sort of a circular buffer. The code you have there tries to play both sounds on the same click and likely mixes them together at the same time if "playSound()" is asynchronous, but I don't know what the SoundManager class is so I have no idea.
Click to expand...
Click to collapse
Okay, so I understand what you mean, but I don't know how to say that in Java. Could you give a short example? As for SoundManager, it's really a useless class I have that I was using to play my media. You can take a look here:
Code:
package com.andrew.finnandjake;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
OK, I see it uses SoundPool. First, change your definition for addSound to:
Code:
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put([COLOR="Red"]Index[/COLOR], mSoundPool.load(mContext, SoundID, 1));
}
Here's code that should work to do a different tone each push and loop back to the beginning.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonSound extends Activity{
private SoundManager mSoundManager;
private int next=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager=new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0,R.raw.freeze);
mSoundManager.addSound(1,R.raw.ascend);
mSoundManager.addSound(2,R.raw.bubble);
mSoundManager.addSound(3,R.raw.chiff);
Button SoundButton=(Button)findViewById(R.id.button1);
SoundButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mSoundManager.playSound(next);
next++;
if(next>3)
next=0;
}
});
}
}
Gene Poole said:
OK, I see it uses SoundPool. First, change your definition for addSound to:
Code:
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put([COLOR="Red"]Index[/COLOR], mSoundPool.load(mContext, SoundID, 1));
}
Here's code that should work to do a different tone each push and loop back to the beginning.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonSound extends Activity{
private SoundManager mSoundManager;
private int next=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager=new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0,R.raw.freeze);
mSoundManager.addSound(1,R.raw.ascend);
mSoundManager.addSound(2,R.raw.bubble);
mSoundManager.addSound(3,R.raw.chiff);
Button SoundButton=(Button)findViewById(R.id.button1);
SoundButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mSoundManager.playSound(next);
next++;
if(next>3)
next=0;
}
});
}
}
Click to expand...
Click to collapse
Mkay, so I adjusted my code according to the example and everything came back error free; however, when I run the app and click the button, nothing happens. No sound, but no force close either.
I ran the Debugger and it's telling me that the SoundPool sample is not ready. "Sample 1 not READY", "Sample 2 not READY", etc.
I don't know. I assume you used different names for your sound files. I just grabbed those out of the "notifications" directory of my phone.
Gene Poole said:
I don't know. I assume you used different names for your sound files. I just grabbed those out of the "notifications" directory of my phone.
Click to expand...
Click to collapse
Alright, that's cool. Thanks a lot for all of the help. I owe you one.
Dear XDA,
I am a noob developer and I am trying to create my first real android application. (outside of Hello World prompts and predetermined tutorial programs) I am looking for a bit of guidance on how to best approach my project idea. What I want to do is create a sort of personal cook book for a friend with a main menu with options that takes you to a submenu with additional options that will take you to content pages that have return home buttons.
Please keep in mind that I am still in the learning process and want to learn. Any tips, comments, or approaches would help me a lot.
I am using eclipse to program currently and heavily relying on resources like http://developer.android.com to do even the simplest things.
Thank you!
~obewah
Well without getting into much detail i would say that you would need to come up with a data structure to store your recipes in, make some layouts for the screens you want to display. Also try to reuse the Activity that displays the recipe. When you start that Activity you can send an Extra in the Intent to specify which recipe to display. This will make it very easy to add recipes on the fly
From something awesome
Additional to what killersnowman said, I would recommend you to study ListActivity, and also databases, so you can add recipes from app.
Also you may want take a look to shared preferences.
If you have some coding question, feel free to contact me
Cheers From Colombia
D4.
Depends on if you want to be able to add/edit recipes or not. If you're going to add/edit etc., you'll want to do a database. If you just want to display recipes, reading in flat files would probably be easiest.
Probable be best to use strings.XML for the recipes. And for the buttons search for information about onClickListener and intent.
lukemovement1 said:
Probable be best to use strings.XML for the recipes. And for the buttons search for information about onClickListener and intent.
Click to expand...
Click to collapse
This might be the easiest so do this.
Sent from my SGH-T959 using XDA App
lukemovement1 said:
Probable be best to use strings.XML for the recipes. And for the buttons search for information about onClickListener and intent.
Click to expand...
Click to collapse
Easiest but not the most flexible
From something awesome
Thanks! Progress Update #1
First off thank you for all of the help so far! So far I have two XML layouts (one for the intro page with a picture, dedication, and button to the main.xml and the main with buttons to different categories inside the application)
This is my first independent code (which I am oh so proud of even if it only clicks through to another XML screen-- you have no idea how long it took me to make it work correctly)
Code:
package com.android.obewah.baristame;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class BaristaMeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
final Button button = (Button) findViewById(R.id.startupBU);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click {
setContentView(R.layout.main);};}
);}}
I am still trying to wrap my brain around how I am going to put in the recipes.
My current plan of action is to create a scroll menu that pops up when you select a category (via button on main page) that leads you to an XML of the recipe.
However this seems like it may be an unnecessarily difficult approach. I don't plan to have an option of adding recipes from the app just for it to be a reference.
Thoughts/Comments?
~obewah
final Button button = (Button) findViewById(R.id.startupBU);
Click to expand...
Click to collapse
Im going to change this too-
final Button startupBU = (Button) findViewById(R.id.startupBU);
to keep things ID'ed correctly
obewah said:
First off thank you for all of the help so far! So far I have two XML layouts (one for the intro page with a picture, dedication, and button to the main.xml and the main with buttons to different categories inside the application)
This is my first independent code (which I am oh so proud of even if it only clicks through to another XML screen-- you have no idea how long it took me to make it work correctly)
Code:
package com.android.obewah.baristame;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class BaristaMeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
final Button button = (Button) findViewById(R.id.startupBU);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click {
setContentView(R.layout.main);};}
);}}
I am still trying to wrap my brain around how I am going to put in the recipes.
My current plan of action is to create a scroll menu that pops up when you select a category (via button on main page) that leads you to an XML of the recipe.
However this seems like it may be an unnecessarily difficult approach. I don't plan to have an option of adding recipes from the app just for it to be a reference.
Thoughts/Comments?
~obewah
Click to expand...
Click to collapse
Might i suggest not just changing the layout in the onClickListener but calling startActivity and changing to an Activity that has that layout. This will preserve the back buttons functionality without a bunch of overriding it.
From something awesome
killersnowman said:
Might i suggest not just changing the layout in the onClickListener but calling startActivity and changing to an Activity that has that layout. This will preserve the back buttons functionality without a bunch of overriding it.
Click to expand...
Click to collapse
Please elaborate on how I would do that (and forgive my ignorance)
~obewah
this might interest you
http://developer.android.com/guide/practices/design/seamlessness.html#multiple-activities
Don't Overload a Single Activity Screen
Any application worth using will probably have several different screens. When designing the screens of your UI, be sure to make use of multiple Activity object instances.
Depending on your development background, you may interpret an Activity as similar to something like a Java Applet, in that it is the entry point for your application. However, that's not quite accurate: where an Applet subclass is the single entry point for a Java Applet, an Activity should be thought of as one of potentially several entry points to your application. The only difference between your "main" Activity and any others you might have is that the "main" one just happens to be the only one that expressed an interest in the "android.intent.action.MAIN" action in your AndroidManifest..xml file.
So, when designing your application, think of your application as a federation of Activity objects. This will make your code a lot more maintainable in the long run, and as a nice side effect also plays nicely with Android's application history and "backstack" model.
do you know any html coding? because you can use a webview to load a .html file from R.raw.file.html. i have done this with my change log on my app. also if you using strings.xml have a look at using the \n to add a new link.
Update 3
Well the first two buttons took me about two hours each. Then the next 8 or so took 15 min.
Here is what the code looks like atm; can anyone tell me how to make the back button cause it to go back one step in the program and not exit?
Code:
package com.android.obewah.baristame;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class BaristaMeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.startup);
Button startupBU = (Button) findViewById(R.id.startupBU);
startupBU.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
setContentView(R.layout.main);
//start rec. buttons
}
});}
Class mystuff;
public void toreci(View v) {
setContentView(R.layout.make);
}
public void toramer(View v) {
setContentView(R.layout.ramer);
}
public void tohome(View v) {
setContentView(R.layout.main);
}
public void torcapp(View v) {
setContentView(R.layout.rcapp);
}
public void torlatt(View v) {
setContentView(R.layout.rlatt);
}
public void torbrev(View v) {
setContentView(R.layout.rbrev);
}
public void tordopp(View v) {
setContentView(R.layout.rdopp);
}
public void toriame(View v) {
setContentView(R.layout.riame);
}
public void torilat(View v) {
setContentView(R.layout.rilat);
}
public void toribre(View v) {
setContentView(R.layout.ribre);
}
public void test(View v) {
setContentView(R.layout.test);
}
}
Agree with killersnowman, don't overload the activity.
Have a recipe activity and layout. Make the button start the recipe activity and pass extra's (ints, strings, booleans) across in a bundle.
Here is an example on how to do it.
http://droidapp.co.uk/?p=48
Make the recipe activity read from the bundle, and then populate textviews / images etc based on them.
eatmold said:
Agree with killersnowman, don't overload the activity.
Have a recipe activity and layout. Make the button start the recipe activity and pass extra's (ints, strings, booleans) across in a bundle.
Here is an example on how to do it.
http://droidapp.co.uk/?p=48
Make the recipe activity read from the bundle, and then populate textviews / images etc based on them.
Click to expand...
Click to collapse
Ok, I have some dumb questions for you please forgive me in advance.
1) So what you are trying to tell me is to create another .java activity file that the main activity forwards to in order to preserve the integrity of the main activity. This new activity should be activated by the click here to start button and it will have all of the button code in it:
Code:
public void tolabou(View v) {
setContentView(R.layout.lcoff);
}
public void toldrip(View v) {
setContentView(R.layout.ldrip);
}
public void tolflav(View v) {
setContentView(R.layout.lflav);
Is that right?
2) Can you give me a idiot friendly definition of "activity, ints, strings, booleans, and bundle" and how they compliment each other
3) Right now I have a layout.xml for each recipe is that bad?
Feel free to answered all or any part of those questions and again I am truly grateful for all of the help I have received!
Well i can answer #2.
For more on activities: http://developer.android.com/reference/android/app/Activity.html
An activity is like each "part" of a program. You can have as any as you want. The main one gets called by the launcher to start the app.
For variable types look at: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Int = number without decimal (-7,0,1,8) -2,147,483,647 to 2,147,483,647 max.
for decimals use float or double
string = text. ("Hello World!")
boolean = true/false (1/0, on/off, whatever).
bundle = some android thing. I believe it can save the state of your program to resume and stuff.
For future referance:
Java Tutorials: http://download.oracle.com/javase/tutorial/index.html (Lots of good stuff for starting programming and java)
Android Dev: http://developer.android.com/guide/index.html (Android specific stuff)
OK, I'll try to explain the process:
Main Screen:
1 Activity : main.java (This is the Java class, contain java code)
1 Layout : main.xml (This is the XML layout, linked to the class in setContentView)
Recipe Screen:
1 Activity : recipe.java
1 Layout : recipe.xml
So if in the main screen there are 2 buttons ("Lasagne" and "Pasta Bake"). Pressing either of these buttons will launch the recipe screen, but will pass a different string in the bundle to the new activity:
Code:
LasagneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myintent = new Intent("com.whatever.whateverapp.RECIPE");
Bundle mybundle = new Bundle();
mybundle.putString("recipename", "lasagne");
myintent.putExtras(mybundle);
startActivity(myintent);
}
});
The code above is creating a new intent, and a bundle. Adding a string called "recipename" to the bundle and setting it's value to "lasagne". This bundle is then added to the intent, and the activity is started. For the second button you would do exactly the same but set the string to "PastaBake".
So now when the recipe activity starts you need to extract the string, the next code should be placed under onCreate in your recipe activity:
Code:
Bundle mybundle = getIntent().getExtras();
String recipename = mybundle.getString("recipename");
Now with this recipename string you will need to populate your layout.
Hope that makes it a bit clearer. Here are some short tutorials I have written that may help you understand the basics a bit more (BTW: I am no expert, and have only been programming Android / Java for 6 months or so):
Buttons & Actions: http://droidapp.co.uk/?p=4
Changing Screens: http://droidapp.co.uk/?p=24
You will need to read and understand the second one to understand setting up activities as they need to be declared in the AndroidManifest file.
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash 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 tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Try:
Code:
Intent intent = new Intent(splash.this, TUTORIALONE.class);
startActivity(intent);
TwilightLoz said:
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash 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 tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Click to expand...
Click to collapse
zalez said:
Try:
Code:
Intent intent = new Intent(splash.this, TUTORIALONE.class);
startActivity(intent);
Click to expand...
Click to collapse
Nope, that doesn't work. I don't know why but I have a feeling its something got to do with my emulator. I think its not set up right..
Is there any output from the logcat?
zalez said:
Is there any output from the logcat?
Click to expand...
Click to collapse
Nope, that's the weird thing. I think there is something wrong with the emulator since it does lag alot when I run it and also I cant use the home button, back button and the search button on the emulator...
I would start by deleting your emulator and recreating it. Then if that doesn't work, I would backup any code you want to keep and start deleting your ide environment and android sdk and just start over.
Ive actually done that yesterday since something went wrong with the saving of my project so my project was deleted. Today I started over. Guess im going to have to start from fresh. Oh by the way, can you have a look over here and answer my question:
http://forum.xda-developers.com/showthread.php?t=2343814
actually I just checked the logcat and I do infact have 1 error. I attached the screenshot of it:
TwilightLoz said:
actually I just checked the logcat and I do infact have 1 error. I attached the screenshot of it:
Click to expand...
Click to collapse
This should not be an error of your app.
nikwen said:
This should not be an error of your app.
Click to expand...
Click to collapse
You mean that the error has nothing to do with my app? But now it wont let me run it since im getting that error!! I actually deleted EVERYTHING again and now im going to start fresh again (at least its good practise).
Could you give me the links to get the eclipese and the androis sdk/adt? Since ive downloaded all of them 3 times...
TwilightLoz said:
You mean that the error has nothing to do with my app? But now it wont let me run it since im getting that error!! I actually deleted EVERYTHING again and now im going to start fresh again (at least its good practise).
Could you give me the links to get the eclipese and the androis sdk/adt? Since ive downloaded all of them 3 times...
Click to expand...
Click to collapse
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
nikwen said:
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
Click to expand...
Click to collapse
I just deleted everything apart from my project before reading your reply...
I'll download it again and run my project again. I didnt get any red x by the way.
TwilightLoz said:
Basically I created a button on my app which links it to another java class. I already setted the layout for the java class using the setContentView method.
But whenenever I click on the button, it doesn't take me to that java class. Im still practising right now but ive gone through the whole project nd couldn't find whats going wrong. I literally cant find any errors.
This is what I did to link the button:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash 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 tut1 = (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}
});
}
as you can see, the tutorialone class has the following layout:
Code:
public class TUTORIALONE extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
and the tutorial1.xml layout consists of a radiobutton (this is just rough). so whenever I click on the button, it should take me to the tutorialone class right?
Click to expand...
Click to collapse
First of all, I would make the Button variables as a "Field" Rather than a 1-time instance of your onCreate() method. A Field is a variable defined at the root of your Class. Example:
Code:
package com.tarikh.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
[COLOR="Lime"]Button tut1;[/COLOR]
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[COLOR="lime"]tut1 [/COLOR]= (Button) findViewById(R.id.button1);
tut1.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
[COLOR="lime"] if (v.getId() == R.id.button1) {
startActivity(new Intent("com.tarikh.thebasics.TUTORIALONE"));
}[/COLOR]
}
});
}
The Lime-Green colored text is the only thing that needs to be change.
The 1st is now a Field, where that variable will be seen throughout the whole class, not just onCreate().
Then take away the Button declaration of tut1 in your onCreate() Method. this will make the tut1 Field (Button) = (your_id_here)
Edit: sorry, I misread. Disregard the "if " statement.
Cheers
1. Try it on a device if you can. I never use AVDs because I can run and debug an app about 8 times before my AVD even loads. Plus yours seems buggy.
2. Does it force close or just not work? if it FCs make sure you declared the second activity in your manifest.
3. Does the main xml button have an id?-- android:id="@+id/button1" ?
EDIT. Wait, your button should be in the activity_main.xml, not tutorialone. if its in tutorial one it weould try to reload that same activity.
actually it wouldnt even do anything probably, because your calling a button that isnt where you are.
Thanks for all your suggestions. I think I'm going to start fresh since Ive got more knowledge now. I'll take all your advices on board and if things go wrong, i'll post my problem here.
nikwen said:
Of course, here you go: http://developer.android.com/sdk/installing/installing-adt.html
You say that it does not let you run the app. Can you install it? I guess that you cannot. Even if you could, there would be a bigger log output/stacktrace.
There should be errors shown in your IDE (Eclipse). Is there any red sign in your project code? (If there is one, there will be a red X in the Package Explorer view.)
I think that the problem is in your code.
Check this.
Click to expand...
Click to collapse
Im going to download this as this has everything bundled together:
http://developer.android.com/sdk/index.html
Thanks guys for your co-operation, I just created the splash page and linked the button to the other layout. Got error but fixed them. It was good practice.
Didn't know running the app from your device instead of the emulator would be soo easy!! And not to mention less laggy.
TwilightLoz said:
Thanks guys for your co-operation, I just created the splash page and linked the button to the other layout. Got error but fixed them. It was good practice.
Didn't know running the app from your device instead of the emulator would be soo easy!! And not to mention less laggy.
Click to expand...
Click to collapse
Great.
Hey guys, im on the process of building my app. Basically, so far, this is what should happen:
The user presses a button and a dialogbox appears. In the dialogbox, the user selects the days in which the reminder shall remind him. (If that makes sense...).
This is my Java code:
Code:
package yCM.medireminder;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ADD extends Activity implements OnClickListener{
private AlertDialog.Builder dialogBuilder;
private void days()
{
//Declaring the Variables
final ArrayList weekdays = new ArrayList();
dialogBuilder = new AlertDialog.Builder(this);
final String[] dayss = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Starting the Process
dialogBuilder.setTitle("Check the Days");
dialogBuilder.setMessage("Check the days when you will shall be reminded");
dialogBuilder.setMultiChoiceItems(dayss, null, new DialogInterface.OnMultiChoiceClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
weekdays.add(which);
}else if(weekdays.contains(which))
{
weekdays.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have been set", Toast.LENGTH_LONG);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have not been set", Toast.LENGTH_LONG);
}
});
// OUTPUT
AlertDialog dayDialog = dialogBuilder.create();
dayDialog.show();
}
Does everything look alright, the emulator isn't working for my and I have misplaced my phone (its on silent) so I have no way of checking whether it will work. I have linked the dialog box to a button so Im sure that when the button is clicked, the dialog box should appear.
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
nikwen said:
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
Click to expand...
Click to collapse
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
TwilightLoz said:
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
Click to expand...
Click to collapse
You just need to do this if you want to use the DialogFragment. For the normal dialog you don't need it.
You can use fragments on Android 1.6+ if you include the supportv4 library.
Try it. That's all I can say. There might be other things that make it crash, apart from that code. Maybe related to the Android lifecycle.
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
TwilightLoz said:
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
Click to expand...
Click to collapse
You can get the current time using
Code:
System.currentTimeMillis()
Then create a Date from it:
Code:
Date myDate = new Date(System.currentTimeMillis();
Then you can format it using the SimpleDateFormat class: http://developer.android.com/reference/java/text/SimpleDateFormat.html
For some things the Calendar class might be better: http://developer.android.com/reference/java/util/Calendar.html
However, I have not worked with it yet.