I'm a newbie and I'm working on a Unit Converter.
I would like to open the same UI regardless of which button I click(Weight or Length)
main_activity.xml UI
Below is the UI I want to open: activity_conversion.xml UI
And I would like each button in the main_activity to run on a different java class.
So, (minus the main_activity.java and it's .xml file)
I have 1 xml file(activity_conversion.xml) and 2 java files one for each button of the main_activity.xml
activity_main.xml Weight button
android:onClick="weightPage"
activity_main.xml Length button
android:onClick="lengthPage"
MainActivity.java
public void weightPage(View view){
Intent intent = new Intent(this, WeightActivity.class);
startActivity(intent);
}
public void lengthPage(View view) {
Intent intent2 = new Intent(this, LengthActivity.class);
startActivity(intent2);
}
Length_Activity.java code for Length button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversion);
}
setContentView() method doesn't work for me:(
Thanks in advance!
I would like to open the same UI regardless of which button I click(Weight or Length)
You can do that by creating an activity and its layout XML file. And then start that activity via explicit intent like this:
//Place this code inside the onClick method
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent);
And I would like each button in the main_activity to run on a different java class.
No, you cannot. All UI elements on a screen are always in the same activity; they cannot run on different java classes. (Unless you are using fragments of which you need not worry about as you are a newbie)
Apparently, you want the two buttons in your main activity to open the same activity. Which you can achieve using intents using the code snippet mentioned above.
Related
What I want, when I click on the login button of the first activity the yellow part slides down and the next activity opens. when I click on the signup button of the second screen(login screen) the yellow part of the second screen slides up and the first activity (sign up Activity)opens. I have used slide-down animation on the linear layout on the first screen it works but not working smoothly. Any help??
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mSignUpButton = findViewById(R.id.btnSigUp);
linearLayout=findViewById(R.id.linearLayout1);
mGotoLoginActivityButton=findViewById(R.id.btnLoginSignUpActivity);
slideDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
//listener for Login button
mGotoLoginActivityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//setValidation();
Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
startActivity(intent);
linearLayout.startAnimation(slideDown);
}
});
}
you shouldn't use two separated Activities for this purpose, use one and login and create account views should be packed into Fragments. this way will be way easier to animate between two views/fragments in on Activity, but if you really must use Activity then use Transitions (probably with shared elements), not animations, as these are working in one Activity during its runtime when visible (and you are currently running new Activity, which cover old one)
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 5 years ago.
My app runs well and shows no errors, but my button is not working ( is unClickable in device)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new Intent(MainActivity.this, Main2Activity.class);
}
});
}
}
and XML file :
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:layout_marginTop="200dp"
android:background="#color/colorPrimary"
android:text="صفحه اصلی"
android:onClick="onClick"/>/>
On the real device when I click on this button nothing happens !
try this way
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
EDIT: I wanted to add a bit of an explanation to help
Hi MRAK,
First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set "onClick" in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called "onClick." I would prefer to call it "startAcitivty2" or so on so you are not confused later. I stuck with your method name for now.
See below for corrected code:
public void onClick(View v){
// note in the below line i'm just using "this"
Intent myIntent = new Intent(this, Main2Activity.class)
// Secondly, you need to end the current activity
finish();
// Third, you need to start your new activity...
// Creating an Intent does not the activity alone
startActivity(myIntent);
}
Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway :)
In an activity, I have created a AsyncTask after hiding the activity:
this.moveTaskToBack(true);
(new MyTask(this)).execute();
To show a dialog in the task (in onPostExcecute), I want to bring the activity to front:
alertDialog.show();
Intent intent = new Intent(mainActivity, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.getBaseContext().startActivity(intent);
But a new instance of the main activity is created and shown on top of the dialog, although the application was still running (the activity has also a dialog style Theme.Dialog). How should I fix this?
Edit: According to javadoc, this code always recreates the activity and doesn't bring its previous instance to front, since startActivity is called from outside of an Activity Context.
How about adding a new piece of information to that intent, and catching it in onCreate()?
What I mean is something like this:
public class MainActivity extends Activity {
public static final String WANT_DIALOG_EXTRA = "WANT_DIALOG_EXTRA";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().hasExtra(WANT_DIALOG_EXTRA)) {
// create and show dialog
}
}
}
Then when you create your intent, add one more line like this:
intent.putExtra(MainActivity.WANT_DIALOG_EXTRA, true);
I'm creating my first mobile app and I'm trying to bind a button to go straight to another xml file. It seems like such an easy answer but I can't find the solution anywhere. I'm using Eclipse as my IDE and using the Android ADT bundle, if that's at all relevant.
Put this in onCreate:
findViewById(R.id.my_button).setOnClickListener(new View.OnClickListener {
#Override
public void onClick() {
startActivity(new Intent(MainActivity.this, OtherActivity.class))
}
});
Replace my_button with the id of your button and MainActivity.this with the name of your main activity class.this and OtherActivity with the name of your other activity.class.
Are you asking how to create a layout with button that brings different layout? If so, you can drag button on your layout and put method in the "On Click" property of the button, something like myClick and then in your code add method
public void myClick(View v) {
startActivity(new Intent(this, MyNewActivity.class));
}
Make sure to declare new activity in AndroidManifest
Hope it helps
In your xml file, add this line of code to your button:
<Button>
//other button properties here
android:onClick='onNextPage'
</Button>
and in the activity/java file for that page, do this:
private void onNextPage(View view){
Intent intent = new Intent(this, nextActivity.java);
startActivity(intent);
}
when you click the button, you'll go to nextActivity.java or whatever you named your second page to be.
I'm trying to load a new layout when I click a button, this layout has only a webView in it so my goal is, when the button is clicked that it opens the webView and directs the user to a pre-determined page. Right now I have
Button codesBtn = (Button)findViewById(R.id.imagebutton1);
codesBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.codes);
}
});
This is inside my onCreate() method in my main activity. I have a couple concerns:
1) is this the correct place to put this block of code into?
2) Do I need to create a separate activity for the webView and what I want the button to do?
3) If so, what is the basic structure of the activity needed?
Thanks in advance!
In general, rather than changing the layout in the current activity it is easier to launch a new activity with the new layout.
If you want to direct the user to a website, you could use an intent to ask the browser to open (example taken from this question)
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Or, you could create an Activity that just has a WebView and launch that by saying;
Intent i = new Intent(this, MyWebViewActivity.class);
i.putExtra("destination", myDestination);
startActivity(i);