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.
Related
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);
}
}
I was wondering if you guys can help me on my app. It’s something really easy for you guys that I’m missing. I’m trying to link buttons on one layout to navigate to other layouts. These six buttons should go to their six different layouts…
Button 7 should go to the layout Number7
I already did Button 1 and it works to number1.
Here’s a screenshot of the layout and here is my code to my main.java: http://imgur.com/zLJ3cdE
package com.example.isthisyourluckyday;
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 Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this, Number1.class));
}
});
}
}
Also….. Here is number7.java which should link to the number7 layout
package com.example.isthisyourluckyday;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Number7 extends Activity {
Button button7;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.number7);
}}
If you guys can please help me out I’d really appreciate it.
If the first button works, where is the problem with the others ?
Below all the code :
public class Main extends Activity implements View.OnClickListener {
private final Button button1;
private final Button button2;
private final Button button3;
private final Button button4;
private final Button button5;
private final Button button6;
private final Button button7;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.equals(button1))
startActivity(new Intent(Main.this, Number1.class));
else if(v.equals(button2))
startActivity(new Intent(Main.this, Number2.class));
else if(v.equals(button3))
startActivity(new Intent(Main.this, Number3.class));
else if(v.equals(button4))
startActivity(new Intent(Main.this, Number4.class));
else if(v.equals(button5))
startActivity(new Intent(Main.this, Number5.class));
else if(v.equals(button6))
startActivity(new Intent(Main.this, Number6.class));
else if(v.equals(button7))
startActivity(new Intent(Main.this, Number7.class));
}
}
The Main class can implement OnClickListener to have only one onClick() function.
I did not test this properly, but you get the idea. It is similar to the other answer, but the code is a bit cleaner and has less repetition in my opinion.
package com.example.isthisyourluckyday;
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 Main extends Activity implements View.OnClickListener {
final Button[] buttons = new Button[7];
final Class[] classes = { Number1.class, Number3.class, Number3.class,
Number4.class, Number5.class, Number6.class, Number7.class };
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttons[0] = (Button) findViewById(R.id.button1);
buttons[1] = (Button) findViewById(R.id.button2);
buttons[2] = (Button) findViewById(R.id.button3);
buttons[3] = (Button) findViewById(R.id.button4);
buttons[4] = (Button) findViewById(R.id.button5);
buttons[5] = (Button) findViewById(R.id.button6);
buttons[6] = (Button) findViewById(R.id.button7);
for(Button b : buttons){
b.setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
for(int i=0; i<buttons.length; i++){
if(buttons[i].equals(v)){
Intent intent = new Intent(Main.this, classes[i]);
startActivity(intent);
}
}
}
}
When you start an activity like this
startActivity(new Intent(Main.this, Number1.class));
you are telling Android to start the Number1 activity. So if you want to start another activity you just do the same thing you did:
final Button b7 = (Button) findViewById(R.id.button7);
b7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Number7.class));
}
});
This is not elegant, but will do the trick.
You could also define a "tag" on each button like
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:onClick="choose"/>
then in your activity add a method
public void choose(View view) {
String number = view.getTag().toString();
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.example.isthisyourluckyday", "com.example.isthisyourluckyday.Number"+number));
startActivity(intent);
}
package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_main);
this.btn = (Button)this.findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
}
}
I don't get any errors however my program also dont run as I want. I am new in android and I want to change the screen after the button is clicked for that I am using two classes so in one class my program should invoke another one onclick. How can I do this ? My code is as above.
First remove this.setContentView(R.layout.activity_main); because you declared it twice . Then declare
btn = (Button)this.findViewById(R.id.button);
after
setContentView(R.layout.activity_main);
Declare your profile activity in manifest file.Check my code below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
1) your oncreate method should look like below one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
2) Add profile Activity into Manifest.
3) add logs in onClick method so you should know if it is getting called on not.
Happy coding!!
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();
}
}