How to make a button open a new layout xml - android

I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.

Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);

You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions

Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)

Related

Need help finding why my application does not leave the home screen upon button click (which should take me to a second activity screen)

I have a main activity where the user has 3 list views and 3 buttons displayed. When the user clicks on 2 of the three buttons I want the user to be sent to another screen (another layout). I already have the two other layouts created and the two other classes created. I am not sure why this is not working. My application reports no errors, no warnings, and I have found that I cannot go through my code step by step because my debugger is not working.
I have an OnClickListner(I am currently just testing one button at a time to get the issue fixed) that is linked to the button in the Main Activity layout.xml android:OnClick. I have an Intent put in place within the Main Activity that (according to multiple sources and YouTube tutorials) is set up properly and the new activity that the user should be redirected to is linked to the appropriate layout. When I run the application the button click registers (I know this because I have a sound effect in place when the button is pressed) but nothing else happens. Also all of my activities are present in the AndroidManifest.xml file. And all of the activity files are at the same folder level, all layouts are under res/layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate button
Button btnClicked = (Button)findViewById(R.id.goal);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
btnClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ChangeGoal.class));
mp.start();
}
});
// method called when set goal button is clicked
public void setGoal(View view)
{
// intent to send user from main activity to the change goal
activity
Intent setGoal = new Intent(this, ChangeGoal.class);
startActivity(setGoal);
}
}
public class ChangeGoal extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_goal); // connect
class to the other layout
}
}
//button from activity_main.xml
<Button
android:id="#+id/goal"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="#string/Set"
android:onClick="setGoal"
android:textSize="20sp"
android:textColor="#000000"/>`
Again, I am expecting the button click from the main activity to send the user to a new activity where they need to make a selection and then return to the main activity. Thus far the button click registers (I can hear it since I included some audio already upon the button click) but the application does nothing, I am left on the same screen and the button can be pressed again and again.
In your Activity layout, you have declared this on your Button:
android:onClick="setGoal"
Then, in code, you have attached an onClickListener to the same Button. This is wrong. Do either the one, or the other, but not both.

Android Studio onClick and OnclickListener not working once changed to another Activity

I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition

Onclick text how can I redirect to another XML page in android?

I am new to android. Can I get to know Onclick text how can I redirect to another XML page in Android ?
You can redirect to another xml through a button click, But here I think you have asked how to do it clicking on some text, for a example if you want to click on this text "click to sign up" and redirect you to the signup page.
So here is how I did it.
1)for text click
Inside Main Activity class
you write the following code.
//Text redirect. signup textview should be created within mainActivity.xml
final TextView signup=(TextView)findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
Intent launchActivity1= new Intent(MainActivity.this,SignUp.class);
startActivity(launchActivity1);
}
});
In the AndroidManifest file, You create a New Activity
<!-- Sign Up activity -->
<activity
android:name=".SignUp"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.xxxx.SignUp"/>
<categeory android:name="android.intent.categeory.LAUNCHER"/>
</intent-filter>
</activity>
Sign Up Java class. You make signup.xml appear inside this class by accessing it through the R.id.layout
package com.example.xxxx;
import android.app.Activity;
import android.os.Bundle;
public class SignUp extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
}
}
.
2) for button click
You will do the same as for Text click.
In MainActivity.java class
final Button button=(Button)findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,Details.class);
startActivity(launchactivity);
Then you will create A new Activity in AndroidManifest.
You Have to have a Details.java class. Since you have mentioned to redirect to Details.class, according to this example.
Inside Details class like in previous access the relevant xml file.
"click here to signup" in the following image will redirect you to the signup.xml .
I'm guessing here that with 'new XML page' you refer to another view?
Most basic way to accomplish this is to make 2 activities with each a different layout (that XML file). On activity 1 add a button. In onclicklistener for that button start the other activity.
Use the visual tools in your IDE to create the activities. Use below code to switch to other activity:
public void startActivity2(View view) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
More info at http://developer.android.com/training/basics/firstapp/starting-activity.html
There so many ways to do it i can't even describe them all :)
Possibly you thing about starting new activity. So you need to create new activity class with proper layout (in xml) and then start it with intent.
Heres an example

Android layout loaded twice

On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});
I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.
When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.
You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

onClickListener in PreferenceActivity

I have this onClickListener in onCreate method of my PreferenceActivity, but it gives me error.
Here is the PrefereceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something.
}
});
}
it gives me this error:
unable to start activity componentinfo java.lang.nullpointerexception
any idea what am I doing wrong?
EDIT: My SettingsPreference opens Dialog that holds that "button2".
The Button is causing your NullPointerException because you haven't set a layout and therefore it is null. You shouldn't need to use buttons in a PreferenceActivity anyway.
There's a perfectly good example of using PreferenceActivity over at the Android developer site: http://developer.android.com/reference/android/preference/PreferenceActivity.html

Categories

Resources