I found this code very usefull to get from one activity to the other but the problem is, that I don't see where the destination is mentioned. I would really appreciate it if some one pointed out how to change the destination.
Here is the code:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
The respective part in the xml:
<Button
android:id="#+id/GetTaxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="GetTaxi" >
</Button>
Thank you very much in advance!
Actually, the code that executes on the Button's click states that your Activity (which is a sub-activity here by the way) has done its job and now finishes with the result code RESULT_OK. It means that there was another Activity that has started this actual Activity for some kind of result. So, when you'll click the Button your Activity will finish. To start some other Activity on the Button's click you should create an Intent, specifying explicitly the Activity you want to start, or just the action you want to perform over some data, letting the Android resolve the final Activity for you. Then you should call startActivity(), passing in the Intent you've created. Hope this helps.
You can use something like this:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Intent intent = new Intent();
intent.setClass(this, GetTaxiActivity.class);
startActivity(intent);
//call finish() if the current activity is something like a loading page
}
});
The code piece above which you mentioned is a sub-activity which is called using
StartActivityForResult();
Related
So, I am basically building an expense tracker app. I have successfully implemented Recycleview which contains Transactions' history. Now, what I am trying to achieve is to use an arrow imageview in the recycleview which will forward me to another activity when it is clicked. Any help will be appreciated on it.
in your adapter class inside onBindViewHolder method add
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, OtherActivity.class);
context.startActivity(intent);
}
});
I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?
A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="#string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});
Using an OnClickListener
Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.
Button yourButton = (Button) findViewById(R.id.your_buttons_id);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
});
This is probably most developers preferred method. However, there is a common alternative.
Using onClick in XML
Alternatively you can use the android:onClick="yourMethodName" to declare the method name in your Activity which is called when you click your Button, and then declare your method like so;
public void yourMethodName(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
Also, don't forget to declare your new Activity in your manifest.xml. I hope this helps.
References;
Starting Another Activity (Official API Guide)
If you declared your button in the xml file similar to this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity"
android:onClick="goToActivity2"
/>
then you can use it to change the activity by putting this at the java file:
public void goToActivity2 (View view){
Intent intent = new Intent (this, Main2Activity.class);
startActivity(intent);
}
Note that my second activity is called "Main2Activity"
Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
startActivity(i);
}
});
Write code on xml file.
<Button android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/button"
android:text="Click"/>
Write Code in your java file
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Secondclass.class));
/* if you want to finish the first activity then just call
finish(); */
}
});
use the following code to have a button, in android studio, open an already existing activity.
Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);
StartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
}
});
I did the same that user9876226 answered.
The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"
open is the function, that is bound to the button.
Then just create the function open() in your activity class. When you click on the button, this function will be called :)
Also, I think this way is more confortable than using the listener.
Apply the following steps:
insert new layout xml in folder layout
rename window2
add new button and add this line: android:onClick="window2"
mainactivity.java
public void openWindow2(View v) {
//call window2
setContentView(R.layout.window2);
}
}
Use following steps to add the new activity (Manifest file will be automatically update)
File > New > Activity > Empty Activity
In your MainActivity.java file add the following code inside protected void onCreate(Bundle savedInstanceState).
Make sure you call finish(); function at the end. So when you tap the Back button, it will not go back to the previous activity.
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
finish();
}
});
I need to link the button with the page (not the main page) like when I click on the button to go to the page for example (location page)?
private void setupLocationPageButton() {
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener()
If your goal is to open another activity this is what you are going to want to do.
In your xml file you are going to want something like this
...
<Button
android:id="#+id/activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="#string/text_of_button" />
...
Then in your activity you want
public void onButtonClicked(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
what i think you trying to do: is when you click on button it's change the MainActivity
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener({
public void onClick(View _view) {
Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
startActivity(i);
}
}));
but first you have to creat activity and class inherit Activity like MainActivity
initialize the class in AndroidMainfest.xml
i hope this help you
You can use fragment, each fragment contains a specific page, see this for Android fragment.
The first activity contains a Fragment, and there's a button inside the fragment, and when I click this button, I wish to jump to a new Activity with different animation. So I do it like this in the Fragment:
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), NewActivity.class);
getActivity().startActivity(intent);
getActivity().overridePendingTransition(
R.anim.forward_activity_move_in,
R.anim.forward_activity_move_out);
});
However the new animation does not take effect when moves to the new Activity, so I wonder maybe I do it wrong. I hope someone can help me, thx a lot:)
Thanks, I solved this problem, You can find solutions at Change activity transition when inside a TabHost
Well the problem is not the case of Fragment, instead the TabActivity causes the problem.
public class SuperActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button registerButton = (Button) findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Register.class);
startActivity(myIntent);
}
});
Button loginButton = (Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(SuperActivity.this, Login.class);
startActivity(myIntent);
}
});
}
}
My register buttons works but not the login button. Is something wrong with my code?
The code posted looks fine. What exactly doesn't work? Do you get an error?
Since you mention it works for 'register' but not for 'login', you may want to double check if you didn't forget to add Login to your manifest.
Is button with id login_button declared in main layout? If I had to guess this button is not declared in main layout is declared somewhere else.
double check if button with id login_button declared in main layout
Since you are not posting your logcat view,it's very difficult to see where the problem this.I'm suggesting you few things which you should make sure that are inplace as said by me.If not then please correct them and let me know if that solved your problem or not.
Firstly,you should make sure that you have declared a button with same id in your main.xml file.It will look something like this:
<button android:id="#+id/login_button" >
</button>
Secondly you should make sure that you have declared your Login.class in your AndroidManifest.xml file.It will look something like this:
<application>
<activity android:name=".Login"></activity>
</application>
Check if these things are in place or not.
Have you declared all the activities? ;)