I'm trying to make a simple math game with two modes, addition and subtraction. I figured out how to create a button that will link the "Addition button" to the addition activity but I can't seem to figure out how to create a second "Subtraction Button" that will link to the subtraction activity. Here's my broken code:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Your code just have some syntax error which is solved as per given code...
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
Your second instruction to add listener is inside the first OnClickListener.
So the listener is never added to the second button. You code should be like this :
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
You need to have different click listeners for different buttons. Right now, you're putting the click listener for the second button inside the click listener for the first button. Separate them like this..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
Some points before the solution (those will help you for further coding).
Declare context after buttons declaration : So after Button
button, button2, write :
final Context context;
Always keep in mind : Always initialize objects in onCreate()
method. So in OnCreate() after
setContentView(R.layout.activity_main_menu);, write :
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
//then define context
context = MainMenu.this;
//or context = getApplicationContext();
Now addListenerOnButton() function will be like this (just replace
addListenerOnButton() with following code):
public void addListenerOnButton(){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Now what is the wong in your code ?
Your onClick listener code is wrong...
enjoy the coding :) (and android :))...
you can even do like this
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}
Related
I am very new to android programming. I want to use a code that takes me to MainActivity from my current activity on a click of a Button.
Here is my current code:
package com.example.flashlightapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Whitelight extends Activity implements OnClickListener {
Button b1 = (Button) findViewById(R.id.b3);
Intent i = new Intent(this, MainActivity.class);
{
this.startActivity(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
What should I put in
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
At first you must check if you declared all your activities in the manifest.xml file.
and in your Java code try this:
package com.example.flashlightapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Whitelight extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Whitelight.this, MainActivity.class);
this.startActivity(i);
}
});
}
}
This tutorial explains how to use intents and listeners : http://goo.gl/phLWkx
Try this..
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
Button b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
Follow the links
http://developer.android.com/index.html
http://developer.android.com/training/index.html
http://www.mkyong.com/tutorials/android-tutorial/
Use following :
Button b1;
public class Whitelight extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelight);
b1 = (Button) findViewById(R.id.b3); // Initialization of Button
b1.setOnClickListener(this); // Initialization of ClickListener to the Button
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
}
in onClick(...) :
you can choose if your button is press then perform specific activity, in your case if you press b1 button then perform button specific activity :
so we can check view v==b1 button. if you want to more button then
if(v==b1){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
else if(v==b2)
{
// perform another action ;
}
I have created 2 buttons and i want to link both of them to 2 different html links,but i could link only one by using this below code....
package com.kk24.adding two buttons;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://........."));
startActivity(myWebLink);
}
});
}
Now i want to link button 2 to another link how do we link ????
Give me step by step details if there is something to import or creating a class or so.....
Thanks in advance.
Try this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://link1."));
startActivity(myWebLink);
}
});
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink2 = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink2.setData(Uri.parse("http://link2."));
startActivity(myWebLink2);
}
});
create new String stringUris then make the String stringUris equals the first link in the first button and in the second button equals the second link then start the activity
String stringUris;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
stringUris = "http://www.example1.com";
Intent Intent1 = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse(stringUris));
startActivity(myWebLink);
}
});
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
stringUris = "http://www.example2.com";
Intent Intent2 = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink2.setData(Uri.parse(stringUris));
startActivity(myWebLink2);
}
you can use a class for showing webview within the app if you want the class contact me.
On xml file in each button add two attributes android:tag with the url and android:onClick with the method name that handle the event
<Button android:id="#id/btSite1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="http://site_1.com"
android:onClick="openBrowser"/>
<Button android:id="#id/btSite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="http://site_2.com"
android:onClick="openBrowser"/>
On activity declare the method openBrowser to handle the click event:
public class Main extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void openBrowser(View view){
//Get url from tag
String url = (String)view.getTag();
if(url != null){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
//pass the url to intent data
intent.setData(Uri.parse(url));
startActivity(intent);
}
}
}
Now when an button is clicked the openBrowser method is called and the browser is open.
Hey I am trying to get different buttons to open up different pages in a android project but only on of the buttons is opening up a new page.
I am new to programming so my terminology may not be correct but I was following a youtube tutorial and it showed how to create a button and make it open up a new page. I tried to do this for multiple buttons but I think I am making the mistake in the main activity. Sorry if I haven't provided the write information to help me solve the problem.
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.question_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.owner_cost);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
you should only have one onCreate() method.. check android activity's life cycle to understand it
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
Button question = (Button) findViewById(R.id.question_button);
question.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
Button ownerCost = (Button) findViewById(R.id.owner_cost);
ownerCost.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
You are duplicating the onCreate() method... this method is called natively by Android and, thus, none of your other methods will get called (you'd have other problems if they were). To create more than one button, you need to add new buttons to your layout and then add them to onCreate().
There is another way to implement the onClick.
In your layout you can specify the function to call onClick
<ImageButton
android:id="#+id/imageButtonNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#color/back_color"
android:onClick="RegistraterCompanyOnClick"
android:src="#drawable/ic_next" />
Then in your activity you can filter which button by it's ID. See below.
public void RegistraterCompanyOnClick(View v){
switch(v.getId()){
case R.id.imageButtonNext1:
String cname = company.getText().toString();
if (cname.length()== 0){
message = getString(R.string.company_required);
ShowDialog(message);
}
else{
company_name = company.getText().toString();
VerifyClient(company_name);
}
break;
case R.id.imageButtonInfo1:
//message = getString(R.string.registration_info);
message = "Device ID:\n" + deviceID;
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonHelp1:
message = getString(R.string.registration_contact);
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonPrevious1:
Intent resultIntent = new Intent();
// TODO Auto-generated method stub
resultIntent.putExtra("company_name", company.getText().toString());
resultIntent.putExtra("company_id", companyID);
resultIntent.putExtra("location_name", location_name);
resultIntent.putExtra("location_id", locationID);
setResult(Activity.RESULT_CANCELED, resultIntent);
finish();
default:
break;
}
}
I have made all the buttons and all the links work just fine but the problem I am having is that i have to start from the top button and select it before proceeding to the subsequent buttons for each of those to work. In other words none one of the buttons work until I select each one in order. I'm sure I'm missing something easy here just don't know much at all about what I'm doing here. Any help appreciated, thanks!
package rainbow.cheetah.go.sms;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class RainbowCheetahGOSMSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.More);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=stealthychief&c=apps"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.match);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=rainbow+cheetah+stealthychief&c=apps"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.rate);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/apps/details?id=rainbow.cheetah.go.sms&feature=search_result#?t=W251bGwsMSwxLDEsInJhaW5ib3cuY2hlZXRhaC5nby5zbXMiXQ.."));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.twitter);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://twitter.com/#!/Stealthychief"));
startActivity(myWebLink);
Button btn = (Button) findViewById(R.id.google);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://plus.google.com/u/0/105194414710791941012/posts"));
startActivity(myWebLink); }
});
}
});
}
});
}
});
}});
}};
This is because you completely messed up your parentheses. I think you meant something like this:
public class RainbowCheetahGOSMSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.More);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(
android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri
.parse("https://play.google.com/store/search?q=stealthychief&c=apps"));
startActivity(myWebLink);
}
});
Button btn2 = (Button) findViewById(R.id.match);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/search?q=rainbow+cheetah+stealthychief&c=apps"));
startActivity(myWebLink);
}
});
Button btn3 = (Button) findViewById(R.id.rate);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://play.google.com/store/apps/details?id=rainbow.cheetah.go.sms&feature=search_result#?t=W251bGwsMSwxLDEsInJhaW5ib3cuY2hlZXRhaC5nby5zbXMiXQ.."));
startActivity(myWebLink);
}
});
Button btn4 = (Button) findViewById(R.id.twitter);
btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://twitter.com/#!/Stealthychief"));
startActivity(myWebLink);
}
});
Button btn5 = (Button) findViewById(R.id.google);
btn5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://plus.google.com/u/0/105194414710791941012/posts"));
startActivity(myWebLink);
}
});
}
}
I tried to make a signup page in android where I use a reset button that should clear all fields in the page. Please see the code below and correct it as my code is not working.
Button btnreset = (Button) findViewById(R.id.btnreset);
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
this is an signup page and fields are first name,last name,user id,password.when user click on reset button it clear all the fields who's filled the user.I'm give a complete source code to you please check this:
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class signup extends Activity{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.signup);
Button b = (Button) findViewById(R.id.btnClick2);
Button btnreset = (Button) findViewById(R.id.btnreset);
final EditText eText1 = (EditText)findViewById(R.id.firstname);
final EditText eText2 = (EditText)findViewById(R.id.lastname);
final EditText eText3 = (EditText)findViewById(R.id.userid);
final EditText eText4 = (EditText)findViewById(R.id.password);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
{
if(eText1.getText().toString().equals("") ||eText2.getText().toString().equals("") || eText3.getText().toString().equals("") ||eText4.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
Intent i = new Intent(signup.this,login.class);
startActivity(i);
}
}
}
});
}
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
public void onClick(View arg0) {
}
}
You've only defined the function; you're not calling it. You will need to execute restartActivity(signup.this) from the OnClickListener.
Also, your intent will likely not execute, because its parent has been finished. Perhaps rearranging the lines of code might help, but a better solution would be having the parent activity start it. Try replacing the click listener with:
btnreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Activity act = signup.this;
Intent intent = new Intent(act, act.getClass());
act.startActivity(intent);
act.finish();
}
}