Reputation: 90
I am new to android and i want to create a workout application for my own use and for that my idea was-
1) The main activity will display an add button to add number of exercises. Lets suppose i need 5 exercises so 5 buttons will be create dynamically.
2) Now buttons are created what i need is when i click on any button the corresponding textToSpeech is called because this textToSpeech class will speak the name of exercise to be started. As each button is associated with a different exercise name and time.
Activity images-- Main Activity , Add number of Buttons
I don't Know how to do this as there are lots of button and how to call different textToSpeech for each buttons.
Upvotes: 1
Views: 2927
Reputation: 397
just add onCLickListener in your activity
View.OnClickListener onClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
// put condition as per id of view
}
};
// here while creating dynamically add click listner
Button button=new Button(ButtonActivity.this);
button.setText("Button"+i);
button.setId(1000+i);
button.setOnCLickListener(onClickListener);
I hope i gave you solution.
Upvotes: 1
Reputation: 487
SecondActivity(Button display) No need to create Activity Create Java file and add the file in manifest
GridLayout gridLayout=new GridLayout(this);
ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(GridLayout.LayoutParams.MATCH_PARENT,GridLayout.LayoutParams.WRAP_CONTENT);
gridLayout.setLayoutParams(layoutParams);
gridLayout.setColumnCount(2);
setContentView(gridLayout);
if(getIntent()!=null){
int buttoncount=getIntent().getIntExtra("value",0);
for(int i=0;i<buttoncount;i++){
Button button=new Button(ButtonActivity.this);
button.setText("Button"+i);
button.setId(1000+i);
button.setBackgroundResource(R.drawable.button_back);
button.setPadding(10,10,10,10);
gridLayout.addView(button);
}
}
Xml file for background (create this file inside of drawable)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#66ff0000" />
<size
android:width="120dp"
android:height="120dp"/>
</shape>
Any confusion then use this is full code of Java file
public class ButtonActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridLayout gridLayout=new GridLayout(this);
ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(GridLayout.LayoutParams.MATCH_PARENT,GridLayout.LayoutParams.WRAP_CONTENT);
gridLayout.setLayoutParams(layoutParams);
gridLayout.setColumnCount(2);
setContentView(gridLayout);
if(getIntent()!=null){
int buttoncount=getIntent().getIntExtra("value",0);
for(int i=0;i<buttoncount;i++){
Button button=new Button(ButtonActivity.this);
button.setText("Button"+i);
button.setId(1000+i);
button.setBackgroundResource(R.drawable.button_back);
button.setPadding(10,10,10,10);
gridLayout.addView(button);
}
}
}
}
Manifest file
<activity android:name=".ButtonActivity"></activity>
and use the background file above...
Upvotes: 0
Reputation: 199
Add clicklistener while inflating new button/view.
private void addNewExcercise(String exercise) {
Button button = new Button(ActivityMain.this);
button.setText(exercise);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do ur task here
}
});
parentLinearLayout.addView(button);
}
Upvotes: 0
Reputation: 367
If I understand your problem in right manner you need to assign Ids to button while creating the buttons start loop for five buttons
for(i=1;i<=5;i++){
Button button=new Button(Activity.this);
button.setId(R.id.existingButtonId+i);
button.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
button.offsetTopAndBottom(40);
button.setHint("hint x ");
yourLayout.addView(button);
}
after this implement onClickListener on your activity and use
public void onClick(View v) {
switch (v.getId()) {
case R.id.ex1:
//call textToSpeech for exercise one
break;
case R.id.ex2:
//call textToSpeech for exercise two
break;
default:
break;
}
}
Upvotes: 0
Reputation: 8282
Implement OnClickListener() on activity
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.oneButton:
// do your code
break;
case R.id.twoButton:
// do your code
break;
case R.id.threeButton:
// do your code
break;
default:
break;
}
}
I hope this will help you,All the best.
Upvotes: 0