How do I link these buttons to their layouts? - android

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

Related

I want my button to be click once and i want to disable double click

package com.example.shery.tictactoe;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
TextView tv1,tv2;
String field1 = tv1.getText().toString();
#SuppressLint("CutPasteId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn8);
tv1 = (TextView)findViewById(R.id.tv1);
if (field1.equals("Turn X")){
tv1.setTextColor(Color.GREEN);
}else if (field1.equals("Turn O")){
tv1.setTextColor(Color.GREEN);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(field1.equals("Turn X")){
btn1.setText("X");
tv1.setText("Turn O");
} else {
btn1.setText("O");
tv1.setText("Turn X");
}
}
});
I want my button to be click once and I want to disable double click so what will be method of doing this and how to do that in my code and want to stop double clicking this is my code below you can check. I want my button to be click once and I want to disable double click :
Just call view.setEnabled(false); in your onClickListener to disable the view after the first click.
From what I understand, you want to disable de double click on the button. This is not possible because Android queues the clicks, but there's a trick I always use: Storing the last time it was clicked:
private long btn1ClickTime= 0; //local variable declaration
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - btn1ClickTime< 1000){
return;//do nothing
}
btn1ClickTime= SystemClock.elapsedRealtime();
//write your code here
}
}

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

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

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.

Android SDK Change text when Button clicked

package com.purelymean.earnings;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button);
TextView tx = (TextView) findViewById(R.id.textView1);
}
}
What do I need to put so that when the button is clicked it will change the textview to whatever i put?
You should add an OnClickListener to your button and override onClick function to set your desire text in your TextView
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
Add an OnClickListener to your button and thats all you need.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tx.setText("It Works!");
}
});

Categories

Resources