String array or new class? - android

How my application works is once a user opens it, it will open to a screen where it will find out depending on the button the user clicks, how many holes of golf they want to play (18 or 9.) From there it will launch the main activity where, depending on what the user chose, will depend on the rules of the application. ie - If they choose 18, the save button wont activate until the 18th hole, same for 9, it will activate on the 9th hole. This will also trigger a final score notification, etc.
Im curious if I should create a separate class for 9 holes and 18 holes, or if I should just pass some sort of value from the open screen, to the main activity that sets the values at 9 or 18?
I guess I am curious on this programming etiquette as I am not very familiar with the best practice of something like this.
Entry screen will look something like this as of now (I have not finished 9 hole button or help button but will be the same as 18 unless launching a seperate class)
case R.id.button18Holes:
//*********************************//
//***LAUNCHES ACTUAL APPLICATION***//
//*********************************//
Intent myIntent = new Intent(src.getContext(), EasyPar.class);
startActivityForResult(myIntent, 0);
Intent iStartValues = new Intent(this, EasyPar.class);
String[] startValues = new String[] {"18"};
iStartValues.putExtra("strings", startValues);
startActivity(iStartValues);
break;
case R.id.button9Holes:
break;
case R.id.buttonHelp:
break;
}
Im not sure if that string array is the proper way to pass one to another activity either?
Thanks in advance!

Pure OO people would say you should create an abstract base class containing common operations and fields, and then for the specialisations, create sub classes. Case statements and if statements like you have above are not pure OO.
Same goes for arrays in general - in pure OO you might have them as a field in a class, but any operations performed on them would be inside a class.
Personally, I would say go with whatever you think will be easier to maintain, quicker to program and more obvious to other people reading the code. I guess that doesn't really answer the question though :-)

You should definitely not be using an array for only two objects! That is overkill. This is important because you have very little memory to work with on a mobile device and arrays eat up some memory. Also you should be using button listeners instead of switch/case statements to find what is going on.
First, I would highly suggest diving into OOP and learning the fundamentals of program using Java before diving right into Android. You do not have to go this route though, but I will say that if you choose to not learn the basics and fundamentals... prepare for a long hard road.
With that said, the simplest way to do this in Android IMHO is like this... The comments should provide you with enough insight to what is going on.
The Class files:
GolfTestActivity.class
package com.jmarstudios.golf;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GolfTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This is the main xml layout: res/layout/main.xml
setContentView(R.layout.main);
}
#Override
protected void onStart() {
super.onStart();
// Get a handle to the two buttons in main.xml
final Button _nineHoles = (Button)this.findViewById(R.id.button1);
final Button _eighteenHoles = (Button)this.findViewById(R.id.button2);
// Create a listener for button1
_nineHoles.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start the nine hole activity
GolfTestActivity.this.startActivity(new Intent().setClassName("com.jmarstudios.golf", "com.jmarstudios.golf.NineHoleActivity"));
}
});
// Create a listener for button2
_eighteenHoles.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start the eighteen hole activity
GolfTestActivity.this.startActivity(new Intent().setClassName("com.jmarstudios.golf", "com.jmarstudios.golf.EighteenHoleActivity"));
}
});
}
}
NineHoleActivity.class
/**
*
*/
package com.jmarstudios.golf;
import android.app.Activity;
import android.os.Bundle;
/**
* #author DDoSAttack
*
*/
public class NineHoleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We simply inflate the layout: res/layout/nineholeslayout.xml
setContentView(R.layout.nineholeslayout);
}
}
EighteenHoleActivity.class
/**
*
*/
package com.jmarstudios.golf;
import android.app.Activity;
import android.os.Bundle;
/**
* #author DDoSAttack
*
*/
public class EighteenHoleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We simply inflate the layout: res/layout/eighteenholeslayout.xml
setContentView(R.layout.eighteenholeslayout);
}
}
and in the XML files...
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Do you want 9 holes or 18 holes?" />
<Button
android:text="Nine Holes"
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Eighteen Holes"
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
res/layout/nineholeslayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Nine Holes"
/>
</LinearLayout>
res/layout/eighteenholeslayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Eighteen Holes"
/>
</LinearLayout>
Finally you need to add the activities to your AndroidManifest.xml file
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jmarstudios.golf"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".GolfTestActivity" 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=".NineHoleActivity"></activity>
<activity android:name=".EighteenHoleActivity"></activity>
</application>
</manifest>
Here are some handy references that I HIGHLY recommend:
http://developer.android.com/reference/packages.html
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/resources/faq/commontasks.html
Hope all that helps as this is pretty much a simple copy/paste thing

Related

How do i make multiple pages that i can go to in the app with buttons and make the imagebuttons link to these pages?

So this is my first app and I am trying to code and need some help with buttons. After searching for a answer I just couldn't find one that I understood. I want to be able to make different pages for the app and make imagebuttons that link to these pages. This is the very basic code I have at the minute for my button. Please try to explain where to put the code etc.
Thanks in advance.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:background="#drawable/home_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:nestedScrollingEnabled="true" />
</RelativeLayout>
Since this is your first app, let's start it simple with using only Activities.
You start with a MainActivity, which shall contain your ImageButtons. By clicking on one of these buttons, you will be directed to another activity. If you press the back button, you will arrive back at your MainActivity.
I shall demonstrate some code which shows you how to navigate from one activity to another one. First add the two activities so your AndroidManifest.xml will look something like this:
<activity
android:name=".MainActivity"
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=".SecondActivity"
android:label="#string/title_activity_second_activitity" >
</activity>
If you're using AndroidStudio, it will do this for you when you create a new activity.
Your MainActivity.java will look something like this:
public class MainActivity extends Activity {
//Define your views
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find your views
imageButton = (ImageButton) findViewById(R.id.image_button);
//Assign a listener to your button
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Start your second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
Repeat these steps for every Activity you want to add to your application.
For more information, you will find the Android Docs an usefull source. Please check this link out as a start.
Good luck!
I dont think its a question to be questioned ! Though,make your desired button in your main .xml file and the using java access the button and apply the task which you want to perform from that button ..
You use this in .xml to make a button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
/>
Here's java code for accessing this button
private Button button;
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override public void onClick(View view) {
//do what you want here
}
});
}

android:windowSoftInputMode="stateAlwaysVisible" - without any EditText causes strange behavior like a D-pad

I had an activity that opens already with the keyboard opened because I use the attribute android:windowSoftInputMode="stateAlwaysVisible" at AndroidManifest.
But in this activity I don't have any Edit Text (only one button) and I need to read each character typed (once per time) by user.
So I had overridden the dispatchKeyEvent to read each character.
The problem is that since the keyboard is being showed and there is no one Edit Text, when click in any character Android OS kind selects the button (or any other view) on screen. This selection is kind I was using a D-pad.
And if the back button is pressed it will "select" any other view from back activity.
I think that since there is no Edit Text to works with keyboard the activity does not know how to handle the characters typed.
I had attached at Tiny server a simple project with two activities that can be used as sample to reproduce the issue: http://s000.tinyupload.com/?file_id=70317553185010262971
Also had attached a screenshot at TinyPic:
http://tinypic.com/r/2uhmwdy/8
Below also are all my codes:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testbug"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testbug.MainActivity"
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.testbug.TestActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
</application>
</manifest>
MainActivity.java (1º activity)
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;
public class MainActivity extends Activity {
Button btnStart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), TestActivity.class);
startActivity(i);
}
});
}
}
TestActivity.java (2º activity)
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
public class TestActivity extends Activity {
TextView tvType;
Button testButton;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count);
tvType = (TextView) findViewById(R.id.tv_type);
testButton = (Button) findViewById(R.id.btnTest);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
count++;
if(count==6){
hideKeyboard();
}
return super.dispatchKeyEvent(event);
}
private void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
Layouts:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
</LinearLayout>
activity_count.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestButton"/>
<TextView
android:id="#+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Press any key 3 times to Hide keyboard\n Then Press Back button. The same behavior of selection of button will happens on back activity" />
</LinearLayout>
dispatchKeyEvent only works for physical buttons- think Blackberry. You have a lot of work ahead of you.
First off, in android the keyboard is owned by a view, NOT by the app. SO some view will get the keyboard events. This view must return a non-NULL value in onCreateInputConnection(), with an object that will be called when data comes from the keyboard.
Secondly, you will not always get individual key events. Most Android keyboards work word at a time. This is particularly true for autocorrect features and Swyping. You'll need to consider this in your design.
Thirdly, you need to implement all of the features expected by the keyboards from an edit text. This means correctly supporting composing text, cursor movement notifications, etc. If you react differently than expected, you may screw up autocomplete and swype functionalities and start getting really weird (and wrong) results from the keyboard.
Really, I highly recommend against doing this if you're actually going to do any type of word input. If you're just using characters as NOT words, you should tell it your view is NO_SUGGESTIONS and input type NULL so keyboards should go into dummy mode- although no promises.

android: Cannot switch between activities using intent

I have a hard time switching from one activity to another using intent. My app is almost done, I have there several activities and I am able to switch between them with intent. I have added one more activity to my project (in Eclipse: File ... New ... AndroidActivity). As I was used to, it created .java and .xml file. In my main activity, I have defined my button (xml) and in java class method to perform on buttonClick. Everything seems fine, but when I click my button nothing happens. What could be wrong?
Here is defined my main activity class called "Ponuka":
package com.example.s_home;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class Ponuka extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ponuka);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ponuka, menu);
return true;
}
// switch to activity: Osvetlenie --- works fine
public void obrOsv(View view) {
Intent intent = new Intent (this, Osvetlenie.class);
startActivity(intent);
}
// switch to activity: Kurenie --- works fine
public void obrKur(View view) {
Intent intent = new Intent (this, Kurenie.class);
startActivity(intent);
}
// switch to activity: Ochrana --- this isnt working
public void obrBez(View view) {
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
System.out.println("ok"); // It will not even print out "ok"
}
}
Here is activity_ponuka.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
One more note: When my new activity called "Ochrana" was created, I had to manually update the R.java file. What did I do wrong here?
You shouldn't use System.out.println on Android, rather log the message using the Log class, then you can find it in LogCat. System.out.println might get redirected to LogCat too, but it's not guaranteed. Why doesn't "System.out.println" work in Android?
Is your problem that it starts a different activity than you want or doesn't it start one at all? Because you're creating an intent with Kurenie.class where you obviously want to start the Ochrana activity (according to the comment above the obrBez() method).
PS: Zdravim z Brna. :)
You should never manually update the R.java because it automatically updates itself if your code is correct.
You also need to get an instance of your button in your activity and add a OnClickListener to call the methods that switch the activity.
And the last thing you need to do is to add those activities you want to use in your manifest file.
Probably unrelated to your issue but you're calling the wrong Activity in your new button event:
public void obrBez(View view) {
Intent intent = new Intent (this, Kurenie.class);
I would recommend using setOnClickListener() for the buttons instead of the android:onClick in the XML - XML ment for layout and design and java activity files ment for logic, mixing the two can cause confusion. You can also try that for solving your problem (Although I don't see any problem with your code):
Button btn = (Button) findViewById(R.id.bezpecnost);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Assuming you want Ochrana activity from your comment
Intent intent = new Intent (this, Ochrana.class);
startActivity(intent);
}
});
Two more things that won't solve your problem but might help you:
You might want to define Intent intent; for the entire activity class and than create new intent on the same variable to save memory allocating.
And last thing - give your XML file [Ctrl]+A and than [Ctrl]+i to automatically order the spacing - it will do only good (:
P.S.
If you have any problems with your R file - just delete it! Eclipse auto re-create it immediately - solving all kinds of R bad/not updated...
The answer is in your XML layout.... look closely...
<Button
android:id="#+id/bezpecnost" <!-- THIS IS THE BUTTON -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/zabezp"
android:onClick="obrBez"
/>
The comment should rather be in this format <!-- .... //-->
You left out the two forward slashes - that's why nothing is happening!
Edit:
Eclipse would have shown the entire block commented out highlighted in colour green instead of XML syntax highlight colour!
In regards to R.java - that is generated at compile time, so whenever you make changes to your XML layouts, the Android SDK will re-generate the R.java file each time!
The AndroidManifest.xml should have the lines specifying the activity within the tags application, as in:
<activity android:name="com.example.s_home.Ochrana"
android:label="Ochrana Activity"/>
Your xml is all wrong. Your button with "#+id/pohod" is mixed up with button "#+id/bezpecnost" because you missed android:layout_below="#id/lin1" in your second linear layout. I tested the layout below and it is OK. I change to android:text because I have no drawable.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>
<LinearLayout
android:id="#+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="#+id/bezpecnost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ochrana"
android:onClick="obrBez"
/>
<Button
android:id="#+id/kuren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Kurenie"
android:onClick="obrKur" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:gravity="center"
android:layout_below="#id/lin1"
android:orientation="horizontal" >
<Button
android:id="#+id/osvetl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="osvetlenie"
android:onClick="obrOsv"
/>
<Button
android:id="#+id/pohod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pohodlie"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>

Android Building Your First App Basics

I'm having a difficult time getting the results that should be displayed as described in the second step of the First App project on the android developer website: developer.android.com/training/basics/firstapp/starting-activity.html#receivetheintent
I've created the first intent and copied all other code however upon running the project I receive a blank android screen with no input elements. Here's what the emulator looks like:
http://s1278.beta.photobucket.com/user/cetmrw791346/media/1_zps116f17a9.png.html
I've set the Run Configuration under the Nexus type with an allocation of 512MB RAM so I'm not exactly sure if this might have something to do with an installation problem regarding the Java SDK (7.0) (JDK not the JRE) or if it could possible be the Android SDK. I'm fairly certain I've set everything up correctly. I'm using The Eclipse (I'm pretty sure it's an IDE) for Mobile Developers then creating a new Android App project from File, New Project. Here's what my Package Explorer looks like: http://s1278.beta.photobucket.com/user/cetmrw791346/media/2_zps0f2b94a2.png.html
I'm unsure as how to further troubleshoot the problem and would really appreciate any additional help. Thanks again for the help.
And here are the relevant files:
**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.MainActivity"
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.firstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.firstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.firstapp.MainActivity" />
</activity>
</application>
</manifest>
**MainActivity.java**
package com.example.firstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
**strings.xml**
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
**DisplayMessageActivity.java**
package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
It doesn't look like your emulator has started up yet.
Wait for it to boot to the homescreen, and then your app should run.
A couple of points:
It seems you aren't letting your app actually start. The first screen you posted is just the "boot" screen of your emulator
Have you tried switching to the debug perspective in Eclipse? At the bottom you'll see what Eclipse is actually doing. You have to switch to the console view and/or view the logcat to see a bit more detail, but that should actually help you in your efforts.
If you have trouble starting up your Emulator, you can test it by itself. You have (for instance) the option to select the second of the two Android icons that are in the upper bar in Eclipse. It should be the one that says "Android Virtual Device Manager". When you select it, it shows you your configured Emulators, though you can configure new ones as well. You can start one of those in advance and see how they work.
It seems that you have not still executed your app (the emulator is still booting).
I'm quite new to both Java and Android (just a few weeks on it, following an online course) but I found the emulator really slow and I'd really advice you to plug in a real device and use it for running the app.
When connecting my Galaxy S2 to Linux and clicking RUN, Eclipse allows you to use it for execute the app. In the examples of the course I'm following, the apps starts in just a couple of seconds, while running them in the emulator is painful.
If you still need to use the emulator, you can speed it up by editing the properties of your virtual device in ADT and switching on the flag "[X] Use snapshot". By activating this flag, you won't "power off" and "power on" the "virtual device" each time: when you close it, its current state will be saved to disk as an snapshot and when you run it again, you won't need to wait for it to boot. The snapshot will be used and the virtual device will startup very fast.
Got some similar problem with real device. After have been working well on helloworld, keep on displaying HelloWorld after some changes in the code(building the UI). That is the stack i've no idea to resolve...

Android open new window problem

very easy question, i'm embarrassed to ask but cannot find it out on my own.
In MainActivity.java there is a menu. When user clicks on the menu item, a new window should appear but the app crashes ("the app stopped unexpectedly") instead.
MainActivity.java part:
case R.id.Menu6:
Intent intentabout = new Intent(this, About.class);
startActivity(intentabout);
break;
The case should be right, as the other menu items are working.
About.java:
public class About extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutxml);
TextView tv1 = (TextView)findViewById(R.id.TextView01);
tv1.setText("Something");
setContentView(tv1);
}
}
aboutxml.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Something"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
I have included the class in the AndroidManifest.xml:
<activity
android:name=".About"
android:label="#string/app_name">
</activity>
I can't believe i don't know this, i have other class in my app and they are working...
You shouldn't be calling setContentView twice. Remove the second call. That may or may not be your problem, but it needs to go. If that doesn't fix it, you need to post your error log. If you view it yourself, you'll probably figure it out pretty easily, but if not, post it here.

Categories

Resources