Reputation:
I am trying to make a quiz app in Android Studio 1.5 and my title screen "Begin" Button does not work.
It is supposed to lead from the MainActivity to the second Activity named questionone.
Here is the Button from the layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Begin"
android:id="@+id/begin"
android:layout_marginTop="45dp"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:clickable="true"
android:background="#ffffff"
android:onClick="toDo" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:id="@+id/exit"
android:layout_marginTop="27dp"
android:layout_below="@+id/begin"
android:layout_alignLeft="@+id/begin"
android:layout_alignStart="@+id/begin"
android:clickable="true"
android:onClick="toDo"
android:background="#ffffff" />
Here is the code from my MainActivity Java file:
package com.example.noot_a_normal_pc.kmtomiles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.widget.Toast.makeText;
public class MainActivity extends AppCompatActivity {
Button buttonBegin, buttonExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBegin = (Button) findViewById(R.id.begin);
buttonExit = (Button) findViewById(R.id.exit);
}
public void buttonBegin (View view){
Intent intent = new Intent (this, question.class);
startActivity(intent);
}
public void toDo(View v) {
if (v.equals(buttonExit)) {
Toast.makeText(getApplicationContext(), "Why would you want to exit such a great app?", Toast.LENGTH_LONG).show();
}
}
}
Here is also my Android Manifest file:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".questionone">
</activity>
</application>
</manifest>
Upvotes: 0
Views: 67
Reputation: 483
In your MainActivity
, I would recommend implementing an OnClickListener
...
package com.example.noot_a_normal_pc.kmtomiles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.widget.Toast.makeText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button buttonBegin, buttonExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBegin = (Button) findViewById(R.id.begin);
buttonBegin.setOnClickListener(this);
buttonExit = (Button) findViewById(R.id.exit);
buttonExit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.equals(buttonExit)) {
Toast.makeText(getApplicationContext(), "Why would you want to exit such a great app?", Toast.LENGTH_LONG).show();
}
else if (v.equals(buttonBegin)) {
//run your app!
}
}
}
Upvotes: 1
Reputation: 191681
Your problem is that you have put android:onClick="toDo"
twice, and so buttonBegin
is never used.
As an alternative, I would recommend this after you remove any android:onClick
from the XML
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.begin).setOnClickListener(this);
findViewById(R.id.exit).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.begin:
// handle begin
break;
case R.id.exit:
// handle exit
break;
}
}
}
Also, based on the Activity name "questionone", I would strongly suggest you avoid the thought that you need one new Activity per question.
You only need one generic Activity to display any question.
Upvotes: 1
Reputation: 164
Your button will newer call "buttonBegin(View v)" because in his XML you set "toDo(View v)" in onClick attribute.
So you should make it look like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Begin"
android:id="@+id/begin"
android:layout_marginTop="45dp"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:clickable="true"
android:background="#ffffff"
android:onClick="buttonBegin" />
But better and cleaner idea to accomplish what you want is setting onClickListener on your button. Something like this:
buttonBegin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent (this, question.class);
startActivity(intent);
}
});
If user clicks on you button then code inside onClick(View v){ //code } will run.
Upvotes: 0
Reputation: 2065
your Button xml is wrong, change it like below:(onClick field is changed from toDo to buttonBegin)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Begin"
android:id="@+id/begin"
android:layout_marginTop="45dp"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:clickable="true"
android:background="#ffffff"
android:onClick="buttonBegin" />
Upvotes: 0