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);
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)
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.
I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.
I want to make a one button goes to different activities, I have uploaded an image to show how I planned to do.
The idea is to come from Activity 1 or Activity 2 and find the same Layout and Button and go through it to Activity 7 or Activity 8 according to the Activity I was on.
I hope that I have explained my idea with a good way to be understood.
You need to pass an identifier or key with intent from your 1st part of activities as shown in the image and based on that key identify where the activity came from and where is it supposed to go. You can use if statements inside your button click function
Example
in xml for all your 3 button
<Button
android:name = "#id/button1"
android:onClick = "button1">
define other button and their properties
in java file
public void button1(View v)
{
Intent i = getIntent()
String key = i.getStringExtra("key");
if(key == "activity5")
{
Intent i = new Intent(this, Activity5.class);
startActivity(i);
}
else if (key == "activity6")
{
Intent i = new Intent(this, Activity6.class);
startActivity(i);
}
}
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.