Android Manifest Editor not working properly - android

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.

Related

Navigate through different Android xml activities

I am new to android development and am using android studio in ubuntu to write a simple Android app.
Right now the main activity activity_go is an xml file which describes the first set of objects being displayed. By adding a click listener to a button object described on activities_go.xml
I am able to change the activity to another set of objects, 'two' which corresponds with two.xml
The Following is the java code I am currently using to switch between xml files
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go);
Button nxtButton = (Button) findViewById(R.id.nextButton);
nxtButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
setContentView(R.layout.two);
}
});
}
Upon running this I am able to display two.xml once the button is clicked.
How Can I do this again, upon clicking a button on two.xml change to three.xml ?
or is there a better method to shift between the layout xml for android applications?
I guess, you need to view trainings by Google. This article is about your case.

Android XML Graphical Layout Not Updating

When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is my Graphical layout on eclipse: activity_main.xml
http://s16.postimg.org/wusm4qrp1/image.png
second.xml
http://s29.postimg.org/qft17p5on/image.png
Running the emulator:
http://s28.postimg.org/f9dn32ku5/image.png
After clicking the button (in which case the text I edit does not show):
http://s16.postimg.org/75r62dodx/image.png
Because you didn't post your code, I'll try to explain it from the beginning.
I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.
Both of the activities have their distinct layouts set in the following way, right?
public class MyFirstActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
...
public class MySecondActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
...
Then, in your first activity, define onClickListener of your button
...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(intent);
}
});
That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.

In android eclipse, how can i run a second class to execute some commands?

For instance, the mainActivity.java file is really clustered and to keep it clean i created a second .java(class) where i will execute some code upon a button press. I cannot figure out how to do it at all. And i am not sure what search terms to use either so i apologize if this has been covered.
Heres what i have in my "test" application.
I have a main activity with a single button on it.
package com.test.secondclass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button startButton;
final Intent second = new Intent(getApplicationContext(), testClass.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)findViewById(R.id.button1);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(second);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Now here is the "second" class that i made, now remember this is very short i am just using this for an exercise program before implementing it into my actual program.
package com.test.secondclass;
import android.app.Activity;
import android.widget.Toast;
public class testClass extends Activity{
public void onCreate(){
Toast.makeText(getApplicationContext(), "Second class thinger started", Toast.LENGTH_LONG).show();
}
}
And if i try this i get a force close immediately. If i comment out the "intent" part at the very beginning of my main activity then the program runs. But it doesnt do what i want. obviously. Thanks everyone
Add an OnClickListener to send whatever information to testClass, as shown below (untested):
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent secondIntent = new Intent();
secondIntent.setClassName(myPackageName, "testClass");
startActivityForResult(secondIntent, REQUEST_CODE);
});
References here and here.
Here's my viewpoint: you are receiving a forced close due the fact that you is trying to open user interface methods (such the Toast) with no context.
IMPORTANT: I'm assuming that you already defined both classes in the manifest.xml file!
Before explaining, I'll make a brief:
Toast: this class opens a quick message, receiving as main arguments the context, the message and the time-to-show;
The context: it is the "environment" where to show. Something like a visual scope, that defines the resources you have. In most cases, you can setup it with setContentView method.
Issue Explanation, in my opinion: The "crash" occurs because you opens the Toast message with no context. An activity is a UI control very similar to a viewpage. If you call a new activity, its very like to call a new page, and so, a new context. In the seccond activity, I haven't see any context. I think that you was assuming that the context is preserved from the first activity, but it ins't because its a new activity.
How to fix:
In the seccond class, define a layout view with setContentView, or...
Reimplement your seccond class as a Service, and call it through startService, or...
Define an AIDL mechams (similar to previous fix, but more sophisticated and complex, as it enables async method calls).
Hope it has helped in some way.
Thank you ALL for the answers!! I was actually able to do what i wanted by using the "stopSelf()" command after i displayed the Toast message. I implemented a service class and when i press the button the testClass.java class gets called and runs the "toast" message then immediately exits by the "stopSelf()" command. I made sure of this by including an "onDestroy()" method which also displayed a simple toast message confirming that the service was stopping :). I usually do stuff like this using threads but it was making the main activity really messy no matter how much formatting i did. So i wanted to have a seperate class i could use.
And to the commenter EfEs, i come from programming in C# language for windows. Android is a new playground for me and im still learning. i think im doing quite well but wasnt sure how to do what i asked. But i figured it out then. And thanks for clearing up that an Activity in android is like a "WindowsForm" in C# where it is completely new GUI for the user. I didnt know that. But thanks to all for helping me with your posts!

Android development - CurrentActivity and LatestActivity classes not recognized by Eclipse

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
}
});

please explain this block of code

I found this on android developers when i was trying to program a button:android developers blog: UI framework changes in Android 1.6
With Android 1.6, none of this is necessary. All you have to do is declare a public method in your Activity to handle the click (the method must have one View argument):
class MyActivity extends Activity { public void myClickHandler(View target){
// Do stuff
}
}
And then reference this method from your XML layout:
<Button android:onClick="myClickHandler" />
can smeone please explain this code to me? I am a beginner in programming, and I don't know what to put in the //do stuff space? I need to reference another activity so i can open another screen. And do i still need to have an activity and put a block of program in the class? this is the code i am using in the class at the moment. Please tell me if i need to update it to use this method:
package com.duncan.hello.world;
import com.duncan.hello.world.R;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}
You put what you want to happen when the button is clicked in the // do stuff part.
You only need to update your code if you're using a button click handler in this fashion.
You might want to start with something a bit simpler, and perhaps not target Android 1.6.

Categories

Resources