Android development - CurrentActivity and LatestActivity classes not recognized by Eclipse - android

I ran the HelloWorld android app and now I moved on to making buttons and stuff like that. I am able to create the buttons in the layout xml and all that, but I ran into some confusion over Eclipse not recognizing my Intent declarations.
Here is a snippet of code:
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
The CurrentActivity and NextActivity classes do not seem to be recognized and Eclipse and it doesn't give me the option to automatically create the import statements for it.
What is the package that these classes are in? Is it an issue of some things not recognized? Or some package that needs to be installed/downloaded? Whats the best practice way to handle such a situation?
Also, do I need to add listeners if I already added the buttons to the layout?
Thanks!

I believe CurrentActivity and NextActivity are just being used as example names for classes for launching an activity in whatever code snippet you were looking at.
CurrentActivity should be the name of whatever the Activity class is that you're launching the new activity from, and NextActivity would be the name of some new Activity class that you want to navigate to next.

It seems you are trying a tutorial. In your project, you should create your own classes extends Activity, named CurrentActivityand NextActivity, so Eclipse will know what they are.
2.If you just declare a button in the layout xml file, the app only show it, but doesn't know how to handle the click event on it, so you still have to register the listener for it. You can:
a. Set the android:onClick attribute for the button in the layout file, and then implement the method to handle the click event. I.e. android:onClick="click" in the xml, and add a function with that name in your code:
public void click(View v){
//Process click event here
}
b. register the listener fully in code:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Process click event here
}
});

Related

how to make a button go from MainActivity page to another (Android)

I noticed that even though I made a new xml named drink, there is no ".java" file with it. Do I need to make one or do i add it on to MainActivity.java?
This code is in my MainActivity.java and the Intent line is saying that drink cannot be resolved to a type.
public void calculate(View view){
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, drink.class);
startActivity(i);
}
This is in my manifest:
<activity
android:name=".drink">
</activity>
This is a post before I knew android. I didn't realize you have to make the java file and xml file.
This code is in my MainActivity.java and the Intent line is saying
that drink cannot be resolved to a type.
This means that you don't have a "drink" class. you have to create a new class called "drink" that extends Activityin the application package.
Without any further code to go on (including knowing what your drink class and your manifest.xml looks like), I would check to make sure that the package name specified in the manifest tag of your manifest file matches the package name of your drink class.
I am not sure I understand you correctly, but to go to a different 'page', which is in your case an Activity, you have have to create one yourself.
If you do not see a .java file representing your Activity you should make one like this (note the uppercase Drink for naming conventions):
public class Drink extends Activity{
protected void onCreate(Bundle savedInstance){
this.setContentView(R.layout.drink.xml)
}
}
I am assuming you already made a xml file for the layout of the Activity. Please read the helpfull documentation of Android.
This means that you did not create the class yet. Right - click on your package name in Package Explorer and select Class. Then, create your activity with your desired class name. Also, check if you declared your android:onClick = calculate in your XML file if you get a yellow attention sign that says that your method is not used.

Android Manifest Editor not working properly

Ubuntu 11.10 ADT 16.01 Java 6 Eclipse Java EE Indigo SR1 - All latest release following android dev requirements.
I am just starting to learn coding for android and got to a tutorial on switching Activities. Using the New project android template, I made another test activity class identical to the first except for name.
package com.test.SwitchActivity;
import android.app.Activity;
import android.os.Bundle;
public class SwitchActivityActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The problem is that when I use the Menifest editor edit the AndroidManifest.xml file - add button for the Application Node, the listbox is empty. All the tutorials I have found are populated with xml tag choices. Now, I can add the xml data manually - it is no problem. What is driving me nuts is not being able to figure out why it does not work for me as it is supposed to work. I don't find other people having the issue and I have re-installed everything at least once before coming here to post another question.
I just want to finish the tutorial and switch between two activities.
Here is a screenshot of what I describe. Anyone have any ideas as to why it does not work?
http://i.stack.imgur.com/Fwfbz.png
To create another activity
Create a new class "myactivity.java" that extends the Activity
Create android xml file with your UI items in it lets call it exampleactivity.xml (it should be in small letters with .xml extension and should be saved in res/layout folder).
In myactivity.java add the following method (same as above except exampleactivity replaced main)
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.exampleactivity);
}
}
create a button changeactivity in SwitchActivityActivity and initialize it in onCreate method of SwitchActivityActivity via
changeactivity = (Button) findViewById(R.id.newActivityButton);
create the following in onCreate method of SwitchActivityActivity
changeactivity.setOnClickListener(new OnClickListener() {
Intent i;
public void onClick(View v) {
i = new Intent(Intent.ACTION_DEFAULT);
i.setClass(SwitchActivityActivity.this, myactivity.class);
startActivity(i);
}
});
In the main.xml create a button and give the button id by
android:id="#+id/newActivityButton"
Finally add the following in AndroidManifest file after the SwitchActivityActivity activity tag
compile and run it.

Double Activity window when using intent for achartengine graph

Development Environment: Eclipse 3.7.0
Developing: Android 3.2 application for Market Place
Using: aChartEngine 0.7.0
I'm new to the development scene but have done a bit of coding in the past various languages, I've created the ZopaStats(on Marketplace) app, but I'm now trying to convert a text based stats page to be displayed in a bar graph using achartengine.
I can get the graph to display from an activity via another activity i.e.:
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
but this gives me an additional activity screen i.e.:
Main Screen -> 1st Activity (Original Text Stats View) -> 2nd Activity (Graph)
Therefore, with I hit back on the graph screen, I get the blank 1st activity screen.
I hope I'm making sense here.
So what I tried to do was launch the activity from the Main Screen (i.e. my Main class) e.g.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
}
});
But eclipse gives me the following error in the code:
The method execute(Context) in the type TemperatureChart is not applicable for the arguments new (View.OnClickListener(){}}
I've tried letting Eclipse change the method but this then causes other problems, so I think what I'm really looking for (in a round about way) is to find out what the difference is when I can try to start the activity from another Activity class rather than starting it from the main class.
I apologise for the misuse of terms etc, as I say I'm new. I've been looking at this for a few days now but the Intent and Activity documentation doesn't help me much so I just need a few pointers.
Thanks,
In your example, the this reference that you're passing to execute() is your annonymous inner subclass of OnClickListener. This is not a context object, which is what eclipse is complaining about.
Rather, you want to pass in the activity instance. Assuming the code snippet you posted lives in a class named MyExampleActivity, then you can use MyExampleActivity.this from inside the inner class to access the instance of the containing class. You should be able to pass that to TemperatureChart.execute()
There is no difference in starting an activity from the main activity or from any other activity. You just create an intent, and call startActivity on it.
For completeness, the new code is this:
N.B. My original class is called ZopaStats.class
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new MarketZopaGraph().execute(ZopaStats.this);
startActivity(achartIntent);
}
});
This works great, only a single Activity windows, once again many thanks for the quick response, in record time ;)
when we use achartengine to draw graph in includes it own activity...i.e.org.achartengine.GraphicalActivity..
when we press back it shows own activity which is used to show graph..to hide these activity call finish() method on onPause() method.

Create new "screens" dynamically

I have an application that gets information from a database and it creates ImageButtons dynamically. What I want is, when I click a dynamically created ImageButton, I want it to make a search in my database (which I know how to do) and then create a new activity (or screen) with new ImageButtons created dynamically, too.
How can I do this?
You cannot create activities without declaring them in the manifest file. You can either call setContentView after creating the new layout(I do not recomend this.).
Or use the viewflipper and add the the different layouts as childs to it. So you can use the viewflipper to switch between layouts.
Best option is to read the android developer documentation on how Intents can be used to launch a New Activity
An Intent object is passed to Context.startActivity() or
Activity.startActivityForResult() to launch an activity or
get an existing activity to do something new.
(It can also be passed to Activity.setResult() to return
information to the activity that called startActivityForResult().)
ImageButton imagebutton=new ImageButton(this);
imagebutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getDataFromDataBase();//In this method you get data from database
Intent intent=new Intent(currentactivity.this,newactivity.class);//start new screen.. in this you can create imagebutton dynamically
startActivity(intent);
finish();
}
});
just execute the code that you have inside your onCreate (wrap it in an init(Params param) method), and call that inside your onClickListener. of course exclude the setContentView and findViewById's.

Is it possible to chain event listeners in Android?

I have an activity which contains QuickContactBadges. I'm looking for a way to either chain event listeners on the QuickContactBadge, or to call the default listener from within an override.
Specifically, what I am looking to do is have the QuickContactBadge, when clicked to show the QuickContact card, and then to setResult and finish, to close my activity.
So either I want to add a second listener to the badge in addition to the default one, or implement something like the following:
bdg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
QuickContactBadge bdg = (QuickContactBadge) view;
bdg.base.onClick(); // PSEUDO-CODE LINE
setResult(RESULT_CANCELED, null);
finish();
}
});
Are either of these methods possible, or is there some other way I should be doing this?
Well, the answer to what I was trying to do was not actually in an event listener at all.
The key to getting my activity to close when the QuickBadge is clicked was to add android:noHistory="true" to the activity definition in the application manifest file.
Though, it would still be interesting to know yes/no if there is a way to chain event listeners.

Categories

Resources