How to detect Circle Menu's animation has finished in android? - android

Im using CircleMenu
Layout
<com.hitomi.cmlibrary.CircleMenu
android:id="#+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Activity
circleMenu = (CircleMenu) findViewById(R.id.circle_menu);
circleMenu.setMainMenu(Color.parseColor("#CDCDCD"), R.mipmap.icon_menu, R.mipmap.icon_cancel)
.addSubMenu(Color.parseColor("#258CFF"), R.mipmap.icon_home)
.addSubMenu(Color.parseColor("#30A400"), R.mipmap.icon_search)
.addSubMenu(Color.parseColor("#FF4B32"), R.mipmap.icon_notify)
.addSubMenu(Color.parseColor("#8A39FF"), R.mipmap.icon_setting)
.addSubMenu(Color.parseColor("#FF6A00"), R.mipmap.icon_gps)
.setOnMenuSelectedListener(new OnMenuSelectedListener() {
#Override
public void onMenuSelected(int index) {
switch (i){
case 0:
((SomeScreenSlidePagerActivity)getActivity()).refresh();
break;
case 1:
((SomeScreenSlidePagerActivity)getActivity()).logout();
break;
case 2:
((SomeScreenSlidePagerActivity)getActivity()).showNotifications();
break;
default:
break;
}
}
});
When i press on a subMenu button animation starts but it finishes, OnMenuSelected option executes so it looks very bad. i want animation to finish then start executing OnMenuSelected option. How to do it ?

You can use addListener() of Animation of the class CircleMenu.java

Related

switch case using variables, repetitive code

I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button1:
if(button_list.get(1).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
is_clicked= (String)button_list.get(1).getTag();
if ("0".equals(is_clicked)){
button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
button_list.get(1).setTag("1");
}else{
button_list.get(1).setBackgroundResource(R.drawable.button_normal);
button_list.get(1).setTag("0");
}
break;
case button2:
if(button_list.get(2).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
is_clicked= (String)button_list.get(2).getTag();
if ("0".equals(is_clicked)){
button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
button_list.get(2).setTag("1");
}else{
button_list.get(2).setBackgroundResource(R.drawable.button_normal);
button_list.get(2).setTag("0");
}
break;
case button3:
if(button_list.get(3).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
is_clicked= (String)button_list.get(3).getTag();
if ("0".equals(is_clicked)){
button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
button_list.get(3).setTag("1");
}else{
button_list.get(3).setBackgroundResource(R.drawable.button_normal);
button_list.get(3).setTag("0");
}
break;
case button4:
if(button_list.get(4).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
is_clicked= (String)button_list.get(4).getTag();
if ("0".equals(is_clicked)){
button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
button_list.get(4).setTag("1");
}else{
button_list.get(4).setBackgroundResource(R.drawable.button_normal);
button_list.get(4).setTag("0");
}
break;
case button5:
if(button_list.get(5).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
is_clicked= (String)button_list.get(5).getTag();
if ("0".equals(is_clicked)){
button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
button_list.get(5).setTag("1");
}else{
button_list.get(5).setBackgroundResource(R.drawable.button_normal);
button_list.get(5).setTag("0");
}
break;
case button6:
if(button_list.get(6).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
is_clicked= (String)button_list.get(6).getTag();
if ("0".equals(is_clicked)){
button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
button_list.get(6).setTag("1");
}else{
button_list.get(6).setBackgroundResource(R.drawable.button_normal);
button_list.get(6).setTag("0");
}
break;
case button7:
if(button_list.get(7).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
is_clicked= (String)button_list.get(7).getTag();
if ("0".equals(is_clicked)){
button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
button_list.get(7).setTag("1");
}else{
button_list.get(7).setBackgroundResource(R.drawable.button_normal);
button_list.get(7).setTag("0");
}
break;
case button8:
if(button_list.get(8).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
is_clicked= (String)button_list.get(8).getTag();
if ("0".equals(is_clicked)){
button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
button_list.get(8).setTag("1");
}else{
button_list.get(8).setBackgroundResource(R.drawable.button_normal);
button_list.get(8).setTag("0");
}
break;
case button9:
if(button_list.get(9).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
is_clicked= (String)button_list.get(9).getTag();
if ("0".equals(is_clicked)){
button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
button_list.get(9).setTag("1");
}else{
button_list.get(9).setBackgroundResource(R.drawable.button_normal);
button_list.get(9).setTag("0");
}
break;
case button10:
if(button_list.get(10).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
is_clicked= (String)button_list.get(10).getTag();
if ("0".equals(is_clicked)){
button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
button_list.get(10).setTag("1");
}else{
button_list.get(10).setBackgroundResource(R.drawable.button_normal);
button_list.get(10).setTag("0");
}
break;
Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button[i]
if(button_list.get(i).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
is_clicked= (String)button_list.get(i).getTag();
if ("0".equals(is_clicked)){
button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
button_list.get(i).setTag("1");
}else{
button_list.get(i).setBackgroundResource(R.drawable.button_normal);
button_list.get(i).setTag("0");
}
break;
Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?
I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:
public void ButtonOnClick(View v) {
Button btn = (Button) v;
if (btn.getTag().equals("0")) {
enter_txt.setText(enter_txt.getText()+btn.getText().toString());
}
is_clicked= (String) btn.getTag();
if ("0".equals(is_clicked)){
btn.setBackgroundResource(R.drawable.button_pressed);
btn.setTag("1");
}else{
btn.setBackgroundResource(R.drawable.button_normal);
btn.setTag("0");
}
}
As an aside, you might consider using Boolean objects instead of String objects as the button tags.
First you should never copy paste code, try to extract your code in another method.
If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o)
So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :
public void ButtonOnClick(View v) {
if(button_list.contains(v))
{
setButtonBackground(button_list.indexOf(v));
}
}
private setButtonBackground(int buttonNumber)
{
Button myButton = (Button) button_list.get(buttonNumber);
if(myButton.getTag().equals("0"))
{
enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
}
is_clicked = (String) myButton.getTag();
if ("0".equals(is_clicked))
{
myButton.setBackgroundResource(R.drawable.button_pressed);
myButton.setTag("1");
}
else
{
myButton.setBackgroundResource(R.drawable.button_normal);
myButton.setTag("0");
}
}
When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.
[...]
case button1:
setButtonBackground(1);
break;
case button2:
setButtonBackground(2);
break;
[...]
Let me know if it works for you

Android - Unexpected layout

i haven't many experience in android development. this is my code:
public void onClick(View v) {
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
}
myAvatar is an ImageView.
when i click in one of the four imageButton , the result is always the same: Case R.id.categoriaA4.
Why??
You need to add break to each case. like
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
break;
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
break;
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
break;
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
break;
}

onClick event buttons

I have 5 buttons,each of them open another activity. How to make clickeble only one of them at current moment?When i click 2 different buttons simultaneusly onClick event calls 2 times and 2 different activities opens. How can i resolve my problem. Thanks.My code
public void onClick(View v)
{
switch (v.getId())
{
case R.id.play_button:
onPlayClick();
setButtonsEnable(false);
break;
case R.id.difficalty_button:
onDifficultyClick();
setButtonsEnable(false);
break;
case R.id.hight_scores_button:
onHighScoresClick();
setButtonsEnable(false);
break;
case R.id.share_button:
setButtonsEnable(false);
break;
case R.id.turn_off_button:
onLeaderboardClick();
setButtonsEnable(false);
break;
default:
break;
}
}
After return to main activity i make my buttons clickable
protected void onResume() {
super.onResume();
setButtonsEnable(true);
}
public void setButtonsEnable(boolean config)
{
playBtn.setClickable(config);
difficultyBtn.setClickable(config);
hScoreBtn.setClickable(config);
shareBtn.setClickable(config);
turnOffButton.setClickable(config);
}
setOnClickListener(null)
would do the trick or you can make
setClickable(false)
Maybe what you really need is RadioGroup? It allows out of the box to select click/select only one button at the time.
Doc reference
Tutorial
Create a variable clicked in Activity:
boolean clicked=false;
Set this value in onClick code:
public void onClick(View v)
{
if(!clicked){
clicked=true;
switch (v.getId())
{
case R.id.play_button:
onPlayClick();
setButtonsEnable(false);
break;
case R.id.difficalty_button:
onDifficultyClick();
setButtonsEnable(false);
break;
case R.id.hight_scores_button:
onHighScoresClick();
setButtonsEnable(false);
break;
case R.id.share_button:
setButtonsEnable(false);
break;
case R.id.turn_off_button:
onLeaderboardClick();
setButtonsEnable(false);
break;
default:
break;
}}
}
If you want to open an activity on each click, use a onclicklistener for each button or add a method call in the xml layout via android:onClick="someMethod", when you click a button and call another activity the UI should block itself...have you tried it this way?

Many switch cases but 1 activity

First of all there are few buttons.
I am set a switch case for all buttons in onClick method.
I want to do different task for all buttons like e.g
For add button I want to display questions of Addition , same for subtraction etc.
But I want to start same activity on the click of the buttons, I mean I have a counter class which should start before the actual questions starts getting displayed.
So on the click on any of the buttons I want to start that counter activity an after that display the questions.
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
startActivity(Counter);
switch (v.getId()) {
case R.id.BAddition:
break;
case R.id.BSubtraction:
break;
case R.id.BMultiplication:
break;
case R.id.BDivision:
break;
case R.id.BAll:
break;
}
}
I have done this , is this correct or not, I dont think so, so please guide me.
Edit:
I want when the user clicks on any button the the timer of 3 2 1 starts and when it finishes then the activity with questions starts.
Thanks
From what I think you are saying, you want to display the question after the timer expires. You have several options. One is that you can use
Intent Counter = new Intent(this, TYS_Counter.class);
startActivityForResult(Counter); // notice the change here
Then when your timer Activity finishes, it can use setResult() and return a value to onActivityResult() you can then start the Activity with the questions you want depending on what is sent back from the timer Activity.
If you want to start a new Activity when the counter expires then you could change your code like this
#Override
public void onClick(View v) {
Intent Counter = new Intent(this, TYS_Counter.class);
switch (v.getId()) {
case R.id.BAddition:
counter.putExtra("key", "addition");
break;
case R.id.BSubtraction:
counter.putExtra("key", "subtraction");
break;
case R.id.BMultiplication:
counter.putExtra("key", "multiplication");
break;
case R.id.BDivision:
counter.putExtra("key", "division");
break;
case R.id.BAll:
counter.putExtra("key", "whateverThisIs");
break;
}
startActivity(Counter);
}
Then when your counter Activity finishes you can use the extra sent to tell it which Activity to start

Passing parameter from a button to android:onClick method

Hi I have something like this (3 buttons) in my activity xml pointing to same method:
<Button
android:id="#+id/Button_1"
android:onClick="printNo"
android:text="#string/Button_1" />
<Button
android:id="#+id/Button_2"
android:onClick="printNo"
android:text="#string/Button_2" />
<Button
android:id="#+id/Button_3"
android:onClick="printNo"
android:text="#string/Button_3" />
Is there any way I could determine which button was pressed while in the printNo method ?
public void printNo( View v ) {
switch (v.getId()) {
case (R.id.Button_1):
//stuff
break;
case (R.id.Button_2):
//stuff
break;
case (R.id.Button_3):
//stuff
break;
}
As #user1106018 said - you can use tag in xml like that:
<Button android:onClick="f" android:tag="0"/>
Then it is really simple to get this tag in this way:
public void f(View v) {
String value = v.getTag();
}
Simply switch over the ID:
public void printNo(View v){
switch (v.getId()){
case R.id.Button_1:
break;
case R.id.Button_2:
break;
case R.id.Button_3:
break;
}
Working in my end
public void printNo(View v) {
switch (v.getId()) {
case R.id.Button_1:
break;
case R.id.Button_2:
break;
case R.id.Button_3:
break;
}
In xml add tag, np with name of button.
public void printNo(View V){
view.getTag();
// now you can recognize view with getTag()
}
Other answers seems also good;)

Categories

Resources