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.
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.
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.
Howdy all,
I'm very much a beginning and basically just toying around with different bits of code.
I've got the Home screen of my app with a button called About App. When I click on it, the app force closes.
If I remove the code between /** Back button **/ the app functions perfectly fine.
I'm assuming there is something I'm missing when I add that additional code.
Is someone able to assist with this? I'm sure it's something simple I'm missing but it's driving me nuts.
Code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class AboutApp extends Activity {
/** Grab aboutapp template **/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutapp);
/** Create the web view for htm file **/
WebView wv = (WebView)findViewById(R.id.aboutapp_webview);
wv.setWebViewClient(new WebViewClient() {
[user=439709]@override[/user]
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/aboutapp.htm");
/** Back button **/
Button btn_ok = (Button) findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Main.class);
startActivityForResult(myIntent, 0);
}
});
/** Back button **/
}
}
I hate that. Looking at this for ages and only when I create a thread detailing my issue do I find the problem... I think.
The above code is looking for btn_ok. In the aboutapp.xml file, I've got android:id="@+id/ok_btn" - it should be btn_ok.
Will try this shortly and report back.
Confirmed - that was all it was.
hmmmmmm great you should keep in mind the name of your resources...so what you are exactly trying to make?
coolbud012 said:
hmmmmmm great you should keep in mind the name of your resources...so what you are exactly trying to make?
Click to expand...
Click to collapse
Not really making anything in particular at this stage. Just trying to understand it all - so I'm throwing together a whole lot of different things and trying stuff out.
I'm not a programmer - never have been. So I'm learning "everything" from scratch. It's going to be a long road a head. :good:
Yeah same here...I have already done java and now understanding the android programming (have already made some apps but for personal use only now going to make some professional apps and will host them on play store.)... If you want we both can work together...I mean as we both are freshers so we can help each other in different things...
Hi there,
I would like some advice on what I should look for if I want to implement the following basic scenario:
the app starts with an activity (call it Activity_1) which contains a button
on clicking the button, takes me to a different activity (call it Activity_2)
next time i start the app, it takes me directly to Activity_2
and Activity_1 won't be seen again
So how can I change the startup activity?
What's happening is that Android is not closing your app in between starts, it's only backgrounding it. One thing you could do is to override the onResume() method in your Activity_2 to call finish() if for instance you have set some variable, let's call it appPaused and it would act just like you had pressed the back button (which will also take you back to Activity_1).
Like below:
Code:
package com.test.example;
import android.app.Activity;
import android.os.Bundle;
public class Activity_2 extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
[user=439709]@override[/user]
public void onPause()
{
appPaused = true;
}
[user=439709]@override[/user]
public void onResume()
{
if(appPaused)
finish();
}
}
thanks for the answer. i want to consider that the app is closed, like after restarting the device. i don't want it to be able to start with Activity_1 unless, let's say, i re-install the app.
i also had an idea about having a 3rd activity as the main one defined in the manifest, which on create changes to one of the other activities. but i don't know how that would be implemented. would it work?
octobclrnts said:
What's happening is that Android is not closing your app in between starts, it's only backgrounding it. One thing you could do is to override the onResume() method in your Activity_2 to call finish() if for instance you have set some variable, let's call it appPaused and it would act just like you had pressed the back button (which will also take you back to Activity_1).
Like below:
Code:
package com.test.example;
import android.app.Activity;
import android.os.Bundle;
public class Activity_2 extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
[user=439709]@override[/user]
public void onPause()
{
appPaused = true;
}
[user=439709]@override[/user]
public void onResume()
{
if(appPaused)
finish();
}
}
Click to expand...
Click to collapse
Oh, I'm sorry, I misunderstood your question the first time. If you don't want an activity to be shown except for say the first time after an install (maybe it's a login screen or similar), I would in my main activity, use the SharedPreferences class to see if I have a preference set like below:
Code:
package com.test.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Start extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
startActivity(new Intent(this,Activity_Logon.class));
}
else
{
//do something else every other time
}
}
}
Code:
package com.test.example;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Logon extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
doLogon();
finish();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
}
}
oh, ok. so from what I get SharedPreferences is a db where you can quickly save variables between sessions. great, I'll try it
octobclrnts said:
Oh, I'm sorry, I misunderstood your question the first time. If you don't want an activity to be shown except for say the first time after an install (maybe it's a login screen or similar), I would in my main activity, use the SharedPreferences class to see if I have a preference set like below:
Code:
package com.test.example;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Start extends Activity
{
boolean appPaused = false;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
startActivity(new Intent(this,Activity_Logon.class));
}
else
{
//do something else every other time
}
}
}
Code:
package com.test.example;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Activity_Logon extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
doLogon();
finish();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
}
}
Click to expand...
Click to collapse
K-RAD said:
oh, ok. so from what I get SharedPreferences is a db where you can quickly save variables between sessions. great, I'll try it
Click to expand...
Click to collapse
That's absolutely right. You can store Strings and primitive values indexed by String keys.
octobclrnts said:
That's absolutely right. You can store Strings and primitive values indexed by String keys.
Click to expand...
Click to collapse
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Code:
package com.example.onetimeactivity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.contains("loggedIn"))
{
//run activity only if never run before
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
else
{
//do something else every other time
startActivity(new Intent(this,SecondActivity.class));
}
}
}
Code:
package com.example.onetimeactivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class SecondActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
doLogon();
}
private void doLogon()
{
//logon code
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putBoolean("loggedIn",true);
edit.apply();
}
}
i managed to resolve it by adding android:noHistory="true" to the manifest. however, now i really can't see the first activity, not even if i rebuild the code ))
K-RAD said:
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Click to expand...
Click to collapse
K-RAD said:
in the second activity, doLogon() needed an edit.apply() at the end. it works somehow, but the problem is when i press the back button, it still takes me to the first activity, and i don'twant to see it anymore after i press the button. here's my code
Click to expand...
Click to collapse
Thanks for catching my mistake. You're right that it does need an apply (or before API level 9, commit()) call.
If you don't want to see the activity MainActivity anymore, then you have to make some changes to your structure. You should make an Activity that will be the one that always shows when the user opens the app. That activity is the one who should check the SharedPreferences for your entry. If the entry does not exist, it should call the one-time-activity. Then the one-time-activity should do it's work and save the entry and call finish() to go back to the main activity (the one you usually want to be shown). This way the activity stack will be correct for the back key functionality.
what it sounds like you want is something like a splash/logon screen
like previously said use sharedprefs.
in the splash/logon call the sharedpref and make the default false (I use booleans, you can use ints or strings):
so it would be like this
Code:
SharedPreferences settings;
SharedPreferences.Editor editor;
boolean splash;
[user=439709]@override[/user]
public void onCreate() {
settings = getSharedPreferences("settings", 0);
splash = settings.getboolean("splash", false);
editor = settings.edit();
if(splash == false){
setContentView(//the view you are using);
//do everything else you need to do
button.setonclicklistener(new OnClickListener(){
editor.putboolean("splash", true);
editor.commit();
finish();
});
}else{
//open up your second activity
finish();
}
if its a login screen, when the user clicks logout you would do the editor and change "splash" to false.
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.