Reputation: 99
I am trying to develop in Android Studio a program which works well in Eclipse. But the novel complexity of buttons, and listeners and methods impedes me: I have a collection of similar buttons, with similar actions. They only differ by an index value. I've tried to create a class of objects, named bouton, extending button and having an instance method with a parameter j. But it doesn't work. So far what works is to repeats the code for each button, as shown below with two buttons. The code is partially ready for a for loop but my attempts get me nowhere.
package com....;
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 {
// public static Button boutons[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBouton1();
actionBouton2();
}
private void actionBouton1() {
String buttonID = "bouton" + 1;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
final Button btnGenerique = (Button) findViewById(resID);
final TextView monMessage = (TextView) findViewById(R.id.affichage);
View.OnClickListener monEcouteur = new View.OnClickListener(){
@Override
public void onClick(View v){
monMessage.setText("you clicked button "+1);
}
};
btnGenerique.setOnClickListener(monEcouteur);
}
private void actionBouton2() {
String buttonID = "bouton" + 2;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
final Button btnGenerique = (Button) findViewById(resID);
final TextView monMessage = (TextView) findViewById(R.id.affichage);
View.OnClickListener monEcouteur = new View.OnClickListener(){
@Override
public void onClick(View v){
monMessage.setText("you clicked button "+2);
}
};
btnGenerique.setOnClickListener(monEcouteur);
}
}// fin class MainActivity
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn 1"
android:id="@+id/bouton1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn 2"
android:id="@+id/bouton2"
android:layout_alignTop="@+id/bouton1"
android:layout_toRightOf="@+id/bouton1"
android:layout_toEndOf="@+id/bouton1"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/affichage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="200dp" />
</RelativeLayout>
Upvotes: 0
Views: 471
Reputation: 1374
There are a couple of best practices for adding events to several buttons.
First, I'd like to comment you don't need to create a specific method in the Activity for each individual button and button event. You can just create one method called, buttonEvents, or whatever, and attach all events there.
First suggested method
Just attach and define the events all at once:
Button button = (Button)findViewById(R.id.buttonId)
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
So basically those are "two lines" of code per button and event attaching.
Second suggested method
You can implement OnClickListener on the Activity class and @Override the onClick method like this:
public class SomeClassOfMine extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//... some other code here to init the layout
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
break;
case R.id.button2:
break;
}
The catch in this last is in the onClick method. There you see in the switch you do v.getId(). That method gets you the very specific button resource ID. So in the switch you can discriminate the several button events.
Have a Button's ArrayList
If you have a button's ArrayList you can use a Java foreach instead of a for like this:
// Say you have this below initialized somewhere ...
ArrayList<Button> myButtons;
// The foreach or for..in construction
for (Button but : myButtons){
but.setOnClickListener(new View.OnClickListener(){
//@Override
public void onClck(View v){
// Do something for this button
}
});
Upvotes: 1