how can I order a button in an activity to open another activity not by writing codes and only by using properties menu or right-clicking on the button(pointed in the picture)image.
also I create a new activity(.java file) in src file.
Is it possible or I must write codes?
For calling other activity you need to write code i.e. Intent like this you can call another activity
Intent intent = new Intent(this,anotherActivity.class);
startActivity(intent);
You must. But its 1 line of code only. Add this to your button's onClick
startActivity(new Intent(this, ActivityToOpen.class));
In your layout xml file inside button write attribute android:onClick
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyButton"
android:onClick="YourMethodName" />
and inside Activity
public void YourMethodName(View v) {
// ButtonClickAction
}
and this will call the method when button is clicked
how ever i would recommend to use Intent
You have to write code, there is no escaping that. For opening another activity after clicking a button add the following on the OnClickListener of the button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
Remember to add the next activity within your AndroidManifest.xml file as follows or the activity won't run:
<activity android:name=".ActivityB"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar.BlackActionBar" />
Related
I have the first activity and a button on it , when I click the button , I go to the second activity. And when I moved / am on the second activity I would like the first activity not to be destroyed but to pause(FirstActivity.Pause). Tell me if there are ways to implement this ?
I want to implement this so that the data from the first activity is not deleted.
Thanks!
Your activity is supposed to pause.
You should add this line of code to your on create method:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondaryActivity.class));
}
});
Your Main activity is going to be paused whilst you are on your secondaryActivity.
its the defualt behavior to pause first activty you didn't call finish() on it .
your Intent should be like this
Intent i= new Intent(context, secondActivity.class);
startActivity(i);
please share your code to find issue .
I have a really beginner question about activities, I have just started a new project and I added a button. I would like to know how I can create a second activity (in eclipse) and THEN how do I link the first activity to the second with a button.
To open another activity from an activity you should use Intents.
Tutorial from android doc: http://developer.android.com/guide/components/intents-filters.html
Example:
// The context, The activity to open
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
Intent constructor, startActivity
It will open NewActivity activity, you should replace NewActivity.class with the Class name to open.
And remember you should add the Activity in the AndroidManifest.xml
Since you asked, to open an Activity at button click you need to use the OnClickListener of the Button, setOnClickListener will be used to set the listener.
// i get the reference to the button from the XML
Button button = (Button)findViewById(R.id.button);
// now i set the listener
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Here you should add the code you want to execute when the button is clicked
// In our case we want to open the activity
Intent intent = new Intent(this, NewActivity.class);
// It will open the activity
startActivity(intent);
// ... and stop.
}
});
new View.OnClickListener() this line creates an anonymous class which implements interface View.OnClickListener... Read more here if you want to know.
I have come across a strange error when i press a button that i supposed to return to teh main activity i get an activity not found error. But when i dod it from another activity, it works fine.
Working call:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Broken Call:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Campaign.this, Menu.class);
i.putExtra("Token", player.tokens);
// intent.putExtra("Round", player.round);
// intent.putExtra("Rank", player.rank);
// intent.putExtra("Score", player.score);
// intent.putExtra("Sec", player.secondsTapped);
// intent.putExtra("Min", player.minutesTapped);
// intent.putExtra("Hour", player.hoursTapped);
// intent.putExtra("Day", player.daysTapped);
// intent.putExtra("LifeTap", player.tapsInLife);
// intent.putExtra("SecTap", player.tapsPerSec);
// intent.putExtra("TapRo", player.tapps);
startActivity(i);
}
});
They are pretty much identical, but somehow the second one does not work. The first one has a bridging class before it. I looked through my xml file and nothing is wrong there since it works in one of my activities. The exact error i get is:
Unable to fin explicit activity class {com.tap.tapalot/android.view.Menu};
Thank You for Your help :)
I think you may have used wrong imports, so that Menu class is not the Menu-class you expect.
Try to specify the Menu.class in your intent with full package-path.
Check you seem to be importing. You are importing android.view.Menu in the second call. This is not your Activity class.
You are importing the wrong android.view.Menu class. You should import your Menu Activity class.
make sure you define 'Menu Class' in your mainfest file? like this:
<activity
android:name="pakgae.name.your.Menu"
android:label="#string/title_activity_menu" >
</activity>
I'm new to Android Dev, so please help me out.
I'm trying to start a new activity after i press a button but nothing seems to work.
Here's my code:
public class viewInfo extends Activity {
private Button btn;
public TextView txt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
btn=(Button)findViewById(R.id.buy);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent myIntent = new Intent(viewInfo.this, buyNow.class);
startActivity(myIntent);
}
});
}
I've also added this new activity in the Manifest but it keeps crushing after I press the button.
What am I doing wrong?
Misread the question initially (original answer below for completeness sake).
Make sure you have the activity you are calling defined in your manifest file:
Something like
<activity android:name=".buyNow" android:label="#string/app_name"></activity>
within the application tags would suffice.
Here's the original answer.
Assuming that you have the correct button ID - try this in your onclick:
Intent myIntent = new Intent(getApplicationContext(), buyNow.class);
startActivity(myIntent);
You could add a log message inside your onClick too, to make sure it is actually being called. You can see the log from logcat (run via adb logcat on the command line)
Try to make it:
startActivity(new Intent("[Here is your package name.what you declared in Manifest file]"));
For this you need to write to your manifest:
Hope it helps.
There may be a problem with your buyNow activity that is causing the error.
You really need to use logcat to trace the error. You can enable this by clicking the menu item:
Window -> Show View -> Other...
the selecting "LogCat" from the Android folder
You can simply do this:
startActivity(new Intent(getBaseContext(),Activity.class));
Afther you registred your activity in manifest:
<activity
android:name="com.example.ActivityName"
android:label="#string/app_name" >
</activity>
I want to know how to move from one class to other class in android.
I have one main class i.e Addition.java(Main Activity) and i created other subactivity(Form.java).
I want to how to make my move from one class(ie.Activity)to other.
I tried this,but not working out,just trying to figure out
Intent intent = new Intent();
intent.setClass(this.getParent(),Form.class);
startActivity(intent);
here Form.class is the subactivity, this.getParent I hope it represents main activity. And I created one activity in manifest.xml file and named it as .Form
Am i working right?
The below code works perfectly.
Try it:
#Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(v.getContext(),Form.class);
startActivity(intent);
}
make sure that activity is declared in Android.manifest.