Android - potential bug found with default text selection CAB - android

I have developed an app that uses one view and loads one of two fragments in to it, depending on the orientation, i.e. portrait/landscape.
Both fragments have the same UI TextView/EditText/Button components.
When the application is in portrait mode and I press a text field for a long(ish) time, the text is selected with selection range markers and the default text selection CAB replaces the application bar.
All as expected.
However when I flip to landscape mode, although my application still works as expected, the default text selection behavior does not work. I can still select text by long pressing a field but although the text is highlighted, there are no selection range markers and the default text selection CAB does not appear. I can do nothing with the selected text.
When I flip back to portrait mode, everything works as expected again.
I am targeting a minimum sdk of 16 and building with 19.
Is this a known bug or have I missed a step when flipping from portrait to landscape?
EDIT:
I did some further investigation in a sandbox and have found what seems to be a bug, though not sure if it is in Android or the phone itself. It also seems that this bug is triggered in both portrait and landscape modes.
I have isolated the problem in the following small app.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.foo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="org.example.foo.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>
</application>
</manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Foo</string>
<string name="action_settings">Settings</string>
<string name="select_me">Select Me</string>
</resources>
ActivityMain.java
package org.example.foo;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.example.foo.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_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:orientation="vertical"
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="org.example.foo.PlaceholderFragment" >
<TextView
android:id="#+id/tv"
android:textIsSelectable="true"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="#string/select_me"
/>
</LinearLayout>
So it seems that the the layout_width and layout_height properties combined are blowing the default text selection CAB functionality.
With these parameters I can run the application and select the text, in portrait and landscape modes, with the default text selection CAB displayed as expected.
The bug is triggered if I increase either the layout_width or layout_height by one or more. Either increase will still allow me to select a word but it stops the default text selection CAB from operating, including the text selection range markers.
Further investigation shows that the bug might have some permanent effects on the current Android state, causing an internal memory leak or similar.
Consider this:
1) run the application with the properties as I have listed here and the application works as expected
2) increment either of the layout_width or layout_height properties by 1, rebuild the application and it no longer runs as expected, it now shows the bug
3) reset the adjusted property in (2), so it is now in the original state (1), rebuild the application and it still shows the bug
4) reboot the phone and the application released in (3) now works as expected
I can't think of anything else to do at this point, except report this as a bug to Android and then try to redesign my app to get around this problem.
A last thought is that this could be a hardware related bug, so perhaps someone could test this for me on a different device?
I am currently experiencing this bug on a Huawei Y300-100 phone, running Android 4.1.1
EDIT
I have reported this bug to google, issue number 68435

I think there's more to the issue than just some dimension parameters. The below works without any issue:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout holder = new FrameLayout(this);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
201,
getResources().getDisplayMetrics());
int hieght = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
51,
getResources().getDisplayMetrics());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width,hieght);
params.gravity = Gravity.CENTER;
TextView textView = new TextView(this);
textView.setTextIsSelectable(true);
textView.setText("Select Me");
textView.setBackgroundColor(Color.GRAY);
holder.addView(textView,params);
setContentView(holder);
}
}

In AndroidManifest.xml, remove orientation of android:configChanges="keyboardHidden|screenSize|orientation" to avoid re-calculating offset for your cursor

Related

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.

Why are my android app changes only showing the first time I run it?

I'm working on a fairly simple app. It has 3 activities, each with a picture as a background with a different TextView displaying different strings over the image. My environment: Mac OSX Yosemite, Eclipse Juno version 23.
Here's my issue. I'll make a change, such as altering the text from "123456789" to "012345678", and run the app. Logcat and the console display no errors and says the app has been installed. I open the app on either an emulator or device, and it shows the changes I've made ONLY on the first time I run the app. If I make any additional changes, they will not be picked up. I have tried deleting and re-creating my emulator, and it doesn't work. I've tried setting it to wipe previous data, but that doesn't work either. Eclipse will only recognize my Samsung Galaxy S3 ONCE. Then when I try to run it again on the phone, it doesn't come up as a device. If I restart my computer completely, it will all work again, but only once. It's driving me insane and I've spent 2 days trying to resolve it. I haven't found any information online that I haven't tried. It's as if restarting my computer wipes some data so that it'll work again, but where?
Here's my main activity.
package com.autotec.nfcdemo;
public class MainActivity extends ActionBarActivity {
private NfcAdapter nfcAdapter;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onResume() {
super.onResume();
null);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
// }
// return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_joe:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
return true;
case R.id.action_jane:
intent = new Intent(this, Jane.class);
startActivity(intent);
return true;
case R.id.action_john:
intent = new Intent(this, John.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here's the MainActivity XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="#drawable/card"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp"
android:layout_marginTop="250dp"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif"
android:text="123456789" />
</LinearLayout>
And here's my AndroidManifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.autotec.nfcdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc.hce" />
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/aa_launcher_icon_high"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Jane"
android:label="#string/title_activity_jane" >
</activity>
<activity
android:name=".John"
android:label="#string/title_activity_john" >
</activity>
</application>
</manifest>
One thing to check is your console logs... sometimes it will say something along the lines of : Warning: Activity not started, its current task has been brought to the front" If this happens your new changes are not necessarily loaded...This is usually caused by unfinished dialogs or toasts etc.
The way to avoid this is close the app manually on the simulator before loading new versions.
If this doesn't resolve your issue, be sure to clean your project before building and loading. (in eclipse, project->clean...)
Good luck!
Can you show us "where" did you make that change?! As far as I can see, the TextView has the value "123456789" and that would be the one that it's going to show every time you open the app.
You can make this on the onCreate:
TextView yourTextView = (TextView) findViewById(R.id.textView1)
yourTextView.setText("012345678");
and that would change the text from "123456789" to "012345678" every time you open the app.
You can simply change the android:text="123456789" to "012345678" if you don't need the 1st value.
Other way to keep your new value is by using a "SharedPreferences" and save that data (key value) on internal storage of your app and get it later or every time you open the app.

Android emulator rotates but app doesn't redraw

I've seen pictures of this elsewhere, but from some time back where the answer is generally "this is a known issue with Android 2.3" I'm using 4.4, so that's definitely not the answer.
I have about the simplest program ever: "Hello, Android". When I launch the emulator, it load up in portrait mode. Using Fn-Ctrl-F11 (Mac), the emulator rotates to landscape mode. But the application and the phone controls do not redraw - the whole thing just looks sideways.
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.test.helloandroid.Hello"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and the Activity XML file:
<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"
tools:context=".Hello" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
I'm building with Eclipse, the ADT bundle build v22.3.0-887826, although I can't imagine that matters for something this trivial.
My emulator is for device Galaxy Nexus, Android 4.4 API level 19. I've tried this with Hardware keyboard present marked and unmarked. I found reference to a "keyboard lid support" setting which I haven't seen anywhere - this comment is from 3/12 & so may be outdated.
This is my first Android app, so I'm a complete novice at debugging in this environment. TIA for any suggestions on what I'm missing.
EDIT: Adding code for hello.java
package com.test.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Hello extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello, menu);
return true;
}
}
If you are beginner,you should go through this reference document to know about Activity lifecycle.
Here,I'm including few Log and Toast to make it easier to understand the process happens when you rotate the screen.
Example:
package com.test.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Hello extends Activity {
private static final String TAG = "Hello";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Log.d(TAG, "onCreate");
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
}
}
I hope it will be helpful !!
Apparently,everything old is new again: orientation change bug in 4.4
"The more they overthink the plumbing, the easier it is to stop up the drain."
It's good to know that I've not missed something obvious; OTOH this is a pretty obvious FAIL on the part of google's quality assurance... did bill gates sneak in there while no one was looking? Or are they greedy & trying to sell phones for testing by mucking up the emulators? Looks like there's a device in my future.
Edit :
Reference : Answered by CommonsWare,the Framework Engineer of Android.
Impossible to rotate the emulator with android 4.4

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...

String array or new class?

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

Categories

Resources