How do I make multiple buttons that have links? - android

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);
}
});
}
}

Related

Reload Activity in Android Studio

Can you please help me about this game that I created, I want to reload the same activity, like restart game in quiz game. I hope you guys can help me, this is the code:
package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.ImageViewCompat;
import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class stagenumbereasy1 extends AppCompatActivity {
Button congratsback,congratshome,congratsforward,reload;
ImageView congrats,tryagain;
Animation fromsmall,fromnothing;
MediaPlayer click,congrats1,die;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_stagenumbereasy1);
click = MediaPlayer.create(this, R.raw.click);
congrats1 = MediaPlayer.create(this, R.raw.congrats);
die = MediaPlayer.create(this, R.raw.die);
final Button butt8=(Button)findViewById(R.id.button8);
final Button butt9=(Button)findViewById(R.id.button9);
final Button butt10=(Button)findViewById(R.id.button10);
final Button butt11=(Button)findViewById(R.id.button11);
final Button back=(Button)findViewById(R.id.button12);
final LinearLayout overbox=(LinearLayout)findViewById(R.id.overbox);
final LinearLayout congratsbox=(LinearLayout)findViewById(R.id.congratsbox);
final LinearLayout tryagainbox = (LinearLayout)findViewById(R.id.tryagainbox);
final LinearLayout trybox=(LinearLayout)findViewById(R.id.trybox);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
congratsbox.setAlpha(0);
overbox.setAlpha(0);
tryagainbox.setAlpha(0);
trybox.setAlpha(0);
congratsback=(Button)findViewById(R.id.congratsback);
congratshome=(Button)findViewById(R.id.congratshome);
congratsforward=(Button)findViewById(R.id.congratsforward);
congrats=(ImageView)findViewById(R.id.congrats);
reload=(Button)findViewById(R.id.reload);
tryagain=(ImageView)findViewById(R.id.try1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent back=new Intent (stagenumbereasy1.this,stagenumber.class);
click.start();
startActivity(back);
}
});
butt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
congrats1.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
overbox.setAlpha(1);
overbox.startAnimation(fromnothing);
congratsbox.setAlpha(1);
congratsbox.startAnimation(fromsmall);
}
});
congratshome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent home = new Intent(stagenumbereasy1.this, Home.class);
click.start();
startActivity(home);
}
});
congratsforward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent easy2 = new Intent(stagenumbereasy1.this, stagenumbereasy2.class);
click.start();
startActivity(easy2);
}
});
butt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent refresh = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(refresh);
finish();
}
});
}
});
}
}
Image of my game
I actually create another activity to make it happens but when I click the reload button it is going back to home. This is my thesis game.
I don't know if exists a recommended answer in Android docs. But, I think you can call startActivity() and finish(), like:
// Java
startActivity(new Intent(MyActivity.class));
finish();
// Kotlin
startActivity(Intent(MyActivity::java.class));
finish();
This will create a new instance of the activity and destroy the current.
For better performance, you should make tests with Fragments instead resetting an Activity.
You could try recreate(); which would recreate the activity
You could also try Intent intent = new Intent(MyActivity.this, MyActivity.class);
startActivity(intent);
Just simply use intent flags one is INTENT_FLAGS_NEW_TASK
it restart activity
Intent.setflags()
In your current activity recall it
Intent(main.this,main.class)

Connecting Two buttons in one activity to two other activities

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);
}
}

How do I link these buttons to their layouts?

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);
}

How to link 2 buttons to 2 different html links in eclpse?

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.

reset button in signup page is not working in android

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();
}
}

Categories

Resources