I'm trying to perform a simple navigation action using Intent.
I'm using the complete ADT bundle which includes eclipse, SDK and ADT plugins, so no need to configure ADT separately in eclipse.
1.) I started by creating two layouts named activity_main.xml and test2.xml.
2.) Corresponding java files are mainActivity.java and test2.java.
3.) Now activity_main.xml contains a button with id = "click" . Clicking this button should navigate to next activity i.e test2.xml.
4.) The code in mainActivity.java is as below
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity
{
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
btn = (Button)findViewById(R.id.click);
//Listening to button event
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), test2.class);
startActivity(nextScreen);
}
});
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
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;
}
}
}
5.) But when i did so the findViewById(R.id.click) showed error saying "click cannot be resolved or is not a field"
6.) Eclipse suggested me to create a field click in id which i did. It modified the R.java file but it did not help. Though the errors were gone but the emulator threw error saying "the application has stopped"
7.) My manifest.xml file is as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.test.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=".test2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.test2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
8.) Now my test2.java code is
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.test.R;
public class test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
TextView txtName = (TextView) findViewById(R.id.textView1);
txtName.setText("This is test2 activity");
}
}
Even here at setContentView(R.layout.test2) it shows the similar error as "test2 cannot be resolved or not a field" where as setContentView(R.layout.activity_main) did not show this error
9.) My fragment_main.xml code is as below
<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="com.example.test.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="65dp"
android:text="#string/button" />
</RelativeLayout>
10.) My test2.xml code is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="second page"
android:layout_gravity="center"
/>
</LinearLayout>
The error displayed on running the application is as below
a.) [2014-04-15 18:39:03 - test] W/ResourceType( 8160): ResXMLTree_node header size 0 is too small.
b.) [2014-04-15 18:39:03 - test] C:\Users\KC\Desktop\Android-budle\test\res\menu\main.xml:6: error: Error: No resource found that matches the given name (at 'title' with value '#string/action_settings').
I'm not able to understand what am I missing. Please provide me a solution for this issue, Thanks in advance.
Firstly Check your layout xml. I there is any error then correct it.After that press alt+p then select clean after that it will be build again.
In your activity_main.xml>Button you need to put the android:onClick="methodName". The in your main_activity you schould implement methodName. There you can handle the activities once the Button is clicked.
Another way is to implement the onClickListener to sepertate the XML and the Java files.
Can you add your layout XML files?
Write the onClickListener for your button in your MainActivity onCreate() method and inside it redirect to test2 activity as below:
btn = (Button)findViewById(R.id.click);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,test2.class);
startActivity(intent);
}
});
EDITED:
You need to set your view as fragment_main.xml besides activity_main as your Button is inside fragment_main layout.
Just change the
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
There is something wrong syntax wise with your xml files (test2 or activity_main) due to which the auto generated R file will no longer generate the id for the button view.
The main problem seems to be with activity_main as the button contained in it is no longer converted to its corresponding value in R file.
Check those files and if not able to figure out than put the code in order to get help.
Last but not the least check if you have the import statement like import android.R.
You don't need this to be there as it causes such errors if present.
UPDATE:
You are using -
1) setContentView(R.layout.activity_main);
2) btn = (Button)findViewById(R.id.click);
But your layout where you defined your button is setContentView(R.layout.fragment_main);
So btn is trying to find the view in activity_main which is not available.
Solution -
Set the content view to setContentView(R.layout.fragment_main).
Related
I've implemented a sliding menu android framework by following a tutorial video, the menu works well but I can't figure out how to make the buttons generate new "fresh", i.e. distinct, pages wherein I can place subsequent components/"activities".
Right now all the buttons do is "toggle" back and forth between having the menu exposed, and hiding the menu, making visible the complete "landing page".
This is how it looks:
In my layout file, I think this is responsible for assigning functionality to the buttons:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="Button 3" />
So as you can see the buttons evoke the method toggleMenu which looks like this:
public void toggleMenu() {
switch (this.menuCurrentState){
case CLOSED:
this.menuCurrentState = MenuState.OPENING;
this.menu.setVisibility(View.VISIBLE);
this.menuAnimatonScroller.startScroll(0, 0, this.getMenuWidth(),
0, menuAnimationDuration);
break;
case OPEN:
this.menuCurrentState = MenuState.CLOSING;
this.menuAnimatonScroller.startScroll(this.currentContentOffset,
0, -this.currentContentOffset, 0, menuAnimationDuration);
break;
default:
return;
}
this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable, menuAnimationPollingInterval);
this.invalidate();
}
I guess I need to make a new method for generating fresh pages and then assign that to the button push rather than toggle menu, I've tried this a few times using intent but I've not been able to quite figure it out.
What do I need to account for in such an operation?
How should such a function look?
The complete code can be found on my github page.
Thank you for your consideration.
dig this tutorial, it's one of the only helpful ones, in my opinion:
http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/
or check the pertinent info below"
XML Layouts
Create following two XML layout files in “res/layout/” folder :
res/layout/main.xml – Represent screen 1
res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
File : res/layout/main2.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
2. Activities
Create two activity classes :
AppActivity.java –> main.xml
App2Activity.java –> main2.xml
To navigate from one screen to another screen, use following code :
Intent intent = new Intent(context, anotherActivity.class);
startActivity(intent);
File : AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
File : App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
3. AndroidManifest.xml
Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".App2Activity" >
</activity>
</application>
I'm stuck when Add libraries in my project in Eclipse. I am following this link official android development website http://developer.android.com/tools/support-library/setup.html#libs-with-res and I do exactly what it says but I get errors for some reason.Here how it happends:
1.I download support libraries from SDK Manager.
2.I import them with existing Android Code into workspace and I press on the both .jar files Build Path>Add to Build Path.
3. Then on that project (made in step 2) I configure Build Path and I check both .jar files and uncheck the Android Dependencies and I click finish. Everything is okay for now.
But here comes the main problem=>
4. I press on my main project (myfirstapp) properties and click Add and select the libraries after that I press Apply and lots of errors raise up like R cannot be resolved to do variable and my R.id from /gen folder suddenly dissapear
I add now some screen shoots to make it better for you.
Sorry I have no reputations for posting images. Please copy links below
first image
second image
third image
fourth image
All my Codes: MainActivity.xml
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
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);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
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;
}
}
}
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:layout_height="match_parent"
android:orientation="horizontal">
<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">myfirstapp</string>
<string name="action_settings">Settings</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="title_activity_displaymessageactivity">Displaymessageactivity</string>
<string name="hello_world">Hello world!</string>
</resources>
Displaymessageactivity.xml
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Displaymessageactivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.displaymessageactivity, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
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_displaymessageactivity, container, false);
return rootView;
}
}
}
fragment_displaymessageactivity.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="com.example.myfirstapp.Displaymessageactivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Things I have tried so far:
1. Reinstall Eclipse and Java
2. Redownload libraries from SDK Manager
3. Clean the Project.
4. Make all my xml files starting with lowercase (but the MainActivity.xml and Displaymessageactivity.xml must start with uppercase)
I tried my best and I am stuck here forever. I am very frustated from Google :/
Every help is very appreciated and welcome!
Add the library to your application project:
In the Project Explorer, right-click your project and select Properties.
In the category panel on the left side of the dialog, select Android.
In the Library pane, click the Add button.
In step 3, notice that in the Library pane, you should see that the appcompat_v7 already be added (it's added automatically when you create your project follow MyFirstApp instruction, because you have already chosen minSdkVersion=8, not 11) (your second image).
So remove all the added libraries in the Library pane before adding your library (so in your third image it only has android-support-v7-appcompat in Library Pane), then the R.java will not disappear.
I am new to the Android development world and I've built a simple "Hello World" app. First, activity requests a text. When the "Go" button is clicked, the app launches the second activity displaying the input text.
If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left.
How do I avoid this?
I need my app to resume even if the launcher icon is clicked.
MainActivity.java,
package com.example.myfirstandroidapp;
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){
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txtName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayActivity.java,
package com.example.myfirstandroidapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// 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 onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public void onDestroy(){
super.onDestroy();
}
#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);
}
}
activity_main.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=".MainActivity" >
<EditText
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:onClick="sendMessage"
android:text="Go!" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtName"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Please input your name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
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:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstandroidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstandroidapp.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.myfirstandroidapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstandroidapp.MainActivity" />
</activity>
</application>
</manifest>
Problem:
I'm not qualified to say this a bug, but there is a behaviour with release builds when starting the application from the launcher. It seems that instead of resuming the previous Activity, it adds a new Activity on top. There is a related bug report on this topic here.
Solution:
I'm working around this this by closing the Launcher Activity if it's not the root of the task, as a result the previous Activity in that task will be resumed.
if (!isTaskRoot()) {
finish();
return;
}
Issue for me was whenever app in installed by an apk with the click on 'Open' option it used to relaunch every time when we minimize it.
resolved it by
SplashActivity.java:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isTaskRoot
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intent.action != null
&& intent.action.equals(Intent.ACTION_MAIN)) {
finish()
return
}
}
I've looked at the other threads on this topic, and none of them have helped. I'm using an appropriate minSdkVersion, my menu xml appears setup correctly, my Manifest is calling Them.Holo.Light, and all of the other "typically overlooked" problems (that I'm aware of from other threads, anyway).
I'm using Eclipse with the latest SDK and following the basic tutorial from the Android dev wiki. Eclipse isn't showing any errors.
As you'll see, I'm referencing main.xml and main_activity.xml. main.xml is showing the default view when the app loads, which is a simple header bar and a picture of two ponies. The idea is that the user can use the context menu to swap between the pony picture and one of Lionel Richie (hey, it's a test app).
I've tried running it on the emulator, as well as my Xoom tablet and a phone, with no luck.
Code:
/***** Contents of testIceCreamS Manifest *****/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.icecreams"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="11" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestIceCreamSActivity"
android:theme="#android:style/Theme.Holo.Light"
android:label="#string/app_name"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/***** Contents of TestIceCreamSActivity.java *****/
package test.icecreams;
import android.app.Activity;
import android.os.Bundle;
import test.icecreams.R;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
/***** Contents of ActionBarUsage.java *****/
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ActionBarUsage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
/***** Contents of main_activity.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/buttonone"
android:title="#string/showimage1"
android:icon="#drawable/helloicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttontwo"
android:title="#string/showimage2"
android:icon="#drawable/poniesicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonthree"
android:title="#string/showwhite"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfour"
android:title="#string/showblack"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfive"
android:title="#string/showalert"
android:showAsAction="ifRoom|withText"/>
</menu>
/***** Contents of main.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:orientation="vertical" >
<TextView
android:id="#+id/hellotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:debuggable="true"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/cd"
android:src="#drawable/ponies" />
</LinearLayout>
I also have the content for strings.xml, but I figured it was probably the least important of all of these.
Any suggestions?
In fact, I understand that you want to create an application running on ICS with the layout main.xml and a contextual menu like *main_activity.xml*. You do not have to create two activities, only the main one which will be launched at application will be started.
Please try to make all in one Activity in the same file TestIceCreamSActivity.java :
package test.icecreams;
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
Please also change your target SDK version to level 14 (ICS 4.0 -> 4.0.2) or 15 (4.0.3) in your manifest :
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="15" />
GET EDITTEXT INPUT TO TEXTVIEW IN ANOTHER CLASS USING A BUTTON
I am fairly new to android and I am trying to use an edittext to get user input on one screen (Activity), actually not just one edittext a few like a couple edit texts and maybe a spinner, kind of like a create a new user screen. But I know how to use a button to getText() and setText() from the edittext to the textview if they are in the same activity but can not find anywhere how to accomplish this. Here is something like what the first class's bare bones would be:
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Now I know the magic will be in the onClick(View v){} method, but what magic exactly do I use to 1-open a new Activity that houses the textview and 2- open the Activity?
Here is the second Activity for visual reference:
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
}
}
Again please if anyone can even chop up the code I will use just trying to get it to work right now. Hopefully everyone can rally and give their input as to help others out that may be stuck as well. Thanks ahead of time.
Here is what I have and if force closes on me:
Main Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UserInput extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText)
findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.mandam.ok.GETTEXT",
edit.getText().toString());
startActivity(intent);
}
});
}
}
Second Activity:
package com.mandam.ok;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GetText extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView view = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String name = intent.getStringExtra("com.mandam.ok.MAIN");
//edit.setText(view.getText());
}
}
I will even attach my xml:
<?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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Second xml:
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
And manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mandam.ok"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".OkActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".GetText" >
<intent-filter >
<action android:name="com.mandam.ok.GETTEXT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now I know I am missing something very simple, just don't know what. I appreciate the help so far. :)
Your second activity HAS a textview... saying that it IS a textview is wrong since Activities are not Views.
Here's how you can pass arguments to a new Activity... this code would be in your first Activity's onClick:
EditText edit = (EditText) findViewById(R.id.editText1);
Intent intent = new Intent(UserInput.this, GetText.class);
intent.putExtra("com.package.name.NAME", edit.getText().toString());
startActivity(intent);
where "com.package.name" is your package name. An intent is an abstraction that performs an operation. In this case, the intent tells android to create a new Activity. The putExtra method allows you to put extra information into the intent before telling Android to create a new Activity. When you put an extra variable into an intent, you need to give it a unique string identifier that preferably starts with the package name.
Once the other Activity is created, here's how you can retrieve the string:
Intent intent = getIntent();
String name = intent.getStringExtra("com.package.name.NAME");
// do whatever you want with name