Button onClick, Cycle through Colors/Backgrounds - android

I am using Android Studio
I'm wanting to change the color of my button, with each click.
To cycle it through around 10 colors and start over again in a continuous loop.
For example I'm using setBackgroundResource(#drawable/oval)
oval = blue circle button
oval2 = red circle button
oval3 = green circle button and so on.
So far I have it that button1 starts as oval(blue) and onClick turns into oval2(red)
So my question is, how to add another click to change it to oval3(green) and then cycle it back to the start oval(blue)?
MainActivity.java
package com.example.shadowz.buttononclick;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
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);
// Code Break
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button1) {
button1.setBackgroundResource(R.drawable.oval2);
}
}
});
// Code Break
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button2) {
button2.setBackgroundResource(R.drawable.oval3);
}
}
});
// Code Break
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button3) {
button3.setBackgroundResource(R.drawable.oval4);
}
}
});
// Code Break
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button4) {
button4.setBackgroundResource(R.drawable.oval5);
}
}
});
// Code Break
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button5) {
button5.setBackgroundResource(R.drawable.oval6);
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shadowz.buttononclick.MainActivity"
android:id="#+id/backgroundLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/button1"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button1"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button2"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button2"
android:background="#drawable/oval"
android:layout_alignTop="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button3"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button3"
android:background="#drawable/oval"
android:singleLine="false"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button4"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button4"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:id="#+id/button5"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button5"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp" />
</RelativeLayout>

You can do this by following:
Create integer-array in resources:
<integer-array name="backgrounds">
<item>#drawable/oval1</item>
<item>#drawable/oval2</item>
<item>#drawable/oval3</item>
<item>#drawable/oval4</item>
<item>#drawable/oval5</item>
</integer-array>
Then create special OnClickListener class inside your class code
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
And then set that listener to each button:
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
This should result in the following MainActivity code:
package com.example.shadowz.buttononclick;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
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);
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
}
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
}

Related

Not Performing Any action On Button Click : Android

I need a button in android button.I need to show that when button was click it show the character 1 in the TextView field textView.
So here is my java code.
package com.example.kartikeya;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(){
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<Button
android:text="#string/button"
android:id="#+id/button"
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="#string/click1"
android:contextClickable="true" />
<Button
You forget to call your method. buttonOnClick();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOnClick();
}
Try this way
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
You have missed the text view in your activity.xml file.
you never called this fuction... buttonOnClick()
In MainActivity write following code.
public class MainActivity extends AppCompatActivity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.clickBtn);
TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/textView"
android:textSize="30sp"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/clickBtn"
android:layout_centerInParent="true"
android:text = "click me"/>
</RelativeLayout>

Android Event Handling - Multiple Buttons

Can we create a centralized event handler for multiple buttons in Android?
If so, how can we do that?
Yonatan Nir's solution is the easiest but if you're already using Butterknife you can do it like this:
#OnClick({ R.id.button1, R.id.button2, R.id.button3})
public void onButtonClicked(View theViewThatIsClicked) {
Toast.makeText(this, "Clicked!", LENGTH_SHORT).show();
}
You can do it by using the View parameter you are given:
#Override
public void onClick(final View v)
{
switch (v.getId())
{
case R.id.Button1: ....
case R.id.Button2: ...
}
}
For example:
class MyActivity extends Activity implements View.OnClickListener
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
Button button1 = (Button)findViewById(R.id.Button1);
Button button2 = (Button)findViewById(R.id.Button2);
button1 .setOnClickListener(this);
button2 .setOnClickListener(this);
}
#Override
public void onClick(final View v)
{
switch (v.getId())
{
case R.id.Button1: ....
case R.id.Button2: ...
}
}
}
Here is a pseudo code for this:
class FooActivity extends Activity implements View.OnClickListener{
//inside onCreate(), find buttons with their ids, For ex;
Button btn1 = (Button)findViewById(R.id.btn1);
Buttn btn2 = (Button)findViewById(R.id.btn2)
// and register listeners for appropriate Buttons
btn1.setOnClickListener(this);
btn2.setOnClickListener(this)
//then implement the listener interface method
#Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.btn1 || id==R.id.btn2){
doSameForBothButtons();
}
}
}
}
Here is how I centralize my Click handlers.
Note: It's useful for different types of Views as well, not only Buttons.
Even mixed types (i.e.: you can handle an ImageButton, a TextView and a Button using the same method for all).
Example layout (note the android:onClick="clickHandler" attribute on each Button):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f000"
>
<Button
android:id="#+id/Button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="1"
android:onClick="clickHandler"
/>
<Button
android:id="#+id/Button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="2"
android:onClick="clickHandler"
/>
<Button
android:id="#+id/Button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:text="3"
android:onClick="clickHandler"
/>
</LinearLayout>
Accompanying code (note the clickHandler method):
package com.dergolem.abc_2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MultiClick
extends Activity
{
Button btn1 = null;
Button btn2 = null;
Button btn3 = null;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.multi_click);
btn1 = (Button) findViewById(R.id.Button1);
btn2 = (Button) findViewById(R.id.Button2);
btn3 = (Button) findViewById(R.id.Button3);
}
public void clickHandler(final View v)
{
String str = "";
switch (v.getId())
{
case R.id.Button1:
{
str = "1";
break;
}
case R.id.Button2:
{
str = "2";
break;
}
case R.id.Button3:
{
str = "3";
break;
}
}
Toast.makeText(this, "Button " + str, Toast.LENGTH_SHORT).show();
}
}

How to configure a image pop up for android using eclipce?

I'm working with a list of ImageViews, while I would like to each one be clicked on, an image appear as a pop up.
Currently got a good result, but a lot of work, I created a new xml file for each image, so using onclicklistener, every time I click on ImageView, open another layout xml, but that I would like just open a pop up image.
Can anyone help me?
Here is may xml:
res/layout/main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="353dp"
android:layout_marginTop="150dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup2" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup3" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup4" />
<Button
android:id="#+id/button5"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup5" />
</LinearLayout>
</ScrollView>
</FrameLayout>
res/layout/aperitivos
<ImageView
android:id="#+id/ivcarnes"
android:layout_width="364dp"
android:layout_height="410dp"
android:layout_gravity="center"
android:contentDescription="#string/panecestino"
android:src="#drawable/panecestinoim" />
<Button
android:id="#+id/btclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="85dp"
android:layout_marginTop="150dp"
android:text="#string/close"
android:textStyle="bold"/>
</FrameLayout>
Here is my code:
Main.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class Main extends Activity {
Button button1, button2, button3, button4, button5;
#Override
protected void onCreate(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);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Arg0) {
Intent testegrafico = new
Intent(Main.this,Aperitivos.class);
Main.this.startActivity(testegrafico);
Main.this.finish();
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
Aperitivos.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class Aperitivos extends Activity {
Button btclose;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aperitivos);
btclose = (Button)
findViewById(R.id.btclose);
btclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Arg0) {
Intent testegrafico = new
Intent(Aperitivos.this,Main.class);
Aperitivos.this.startActivity(testegrafico);
Aperitivos.this.finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package br.com.example.locandaristorante;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Main extends Activity {
Button button1, button2, button3, button4, button5;
#Override
protected void onCreate(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);
button1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
View popupView = new View(Main.this);
final PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Drawable drawable = getResources().getDrawable(R.drawable.panecestinoim);
popupWindow.setBackgroundDrawable(drawable);
popupWindow.setHeight(600);
popupWindow.setWidth(400);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
popupWindow.dismiss();
return false;
}
});
popupWindow.showAtLocation(findViewById(R.id.button1), Gravity.CENTER, 0, 0);
}
});
}}

Submit button access denied cannot get any result

I have a problem with my android project. I cannot get access to my button. I have attach my class code==http://pastebin.com/A5ZTBkhd. Please anyone help me.
Here is my code -
package com.droid.androiddoctor;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidDoctorMainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
String e="hello";
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
break;
}
}
}
.Here is my code.when i push submit nothing happens. and the xml file is====
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSubmit"
androi:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
You did not set the listener for your widgets at all. Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
button1.setOnClickListener(this)
}
I would suggest you to go through Android's UI Guide.
I wouldn't implement OnClickListener like that, try to add it like this in your class:
public class AndroidDoctorMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
Button button2 = (Button) findViewById(R.id.btnExit);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button
//or if you just want to exit from activity, just call:
finish();
}});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
}

Get a text value of the button dynamically

My View
<Button android:id="#+id/button1" android:text="Text 1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button2" android:text="Text 2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button3" android:text="Text 3"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
<Button android:id="#+id/button4" android:text="Text 4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></Button>
I have an undefined amount of buttons and need get you text in a touch event.
How i do it?
Tnks a lot.
EDIT:
Thanks for all, this is result of the your Answers => https://play.google.com/store/apps/details?id=br.com.redrails.torpedos
My App :D
If you name all of your buttons "button1, button2, button3..." you can do this:
for(int i = 1; i <= buttonCount; i++) {
int id = getResources().getIdentifier("button" + i, "id", getPackageName() );
findViewById(id).setOnClickListener(this);
}
Where buttonCount is the amount of buttons you have. This method would be placed in onCreate().
Then for your onClick:
public void onClick(View v) {
if (v instanceof TextView) {
CharSequence text = ((TextView) v).getText();
// Do fun stuff with text
}
}
Your Activity will need to implement OnClickListener
You can get the text of a button by using the getText() method.
You can do :
Button button4= (Button) findViewById(R.id.button4);
text = button4.getText();
Here is a complete example:
import android.os.Bundle;
import android.widget.Toast;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
private Button btn1, btn2, btn3, btn4; // You can add more if you like
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewByID(R.id.button1);
btn2 = (Button) findViewByID(R.id.button2);
btn3 = (Button) findViewByID(R.id.button3);
btn4 = (Button) findViewByID(R.id.button4);
// declare the other buttons if any
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
// register the other buttons as well if any
}
#Override
public void onClick(View v)
{
String text = ((Button) v).getText();
Toast.makeText(this, "You clicked " + text , Toast.LENGTH_SHORT).show();
}
}

Categories

Resources