I have an activity with custom popup window (quickaction style). There are some buttons leading to other activities. I want to close popup after pressing button (about or email button) in this popup (now when I go back popup appears again).
public class FirstActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code
Button quickButton = (Button) findViewById(R.id.button_quickaction);
quickButton.setOnClickListener(this);
final ActionItem about = new ActionItem();
final ActionItem email = new ActionItem();
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
QuickAction qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
}
}
Please, help.
Added:
I need something like this:
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qa.dismiss();
}
});
But qa cannot be resolved. Even if I add final to QuickAction qa = new QuickAction(v);.
try finish() on button's onClick method.
updated:
QuickAction qa;
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
you can also put private QuickAction qa; at your activity.
Related
I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
I am new to android. I have five buttons on an activity and on every button click i want to save data shown/written on button say "10",on other button "20" etc.into arraylist or list and than add(+) them every time any button is clicked and than store them on single value and when sublit button is clicked than the total single value is saved into firebase database as signle child data.
Hope this will working for you.
ArrayList<Integer> total = new ArrayList();
int totalCount = 0;
button10.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(10);
}
});
button20.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(20);
}
});
button50.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(50);
}
});
button100.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(100);
}
});
button500.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(500);
}
});
button1000.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
total.add(1000);
}
});
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
for(int i = 0; i < total.size();i++){
totalCount = totalCount + total.get(i); //total value or Output
}
}
});
I want to do different things for one button.
But I am changing instructions after the button was clicked every time.
First I am creating two buttons, after I clicked the button I want it to change its text. For each text I want to create different instruction.
I created an if/else for this. But I am not sure why my isn’t not working.
I only do instructions in the first statement.
Here is my code.
package com.example.iqbal.destini;
public class MAINPAGE extends AppCompatActivity {
Button mAnswer_1_Button;
Button mAnswer_2_Button;
TextView mTextbody;
int counter1 = 0;
int counter2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
mAnswer_1_Button = (Button) findViewById(R.id.answer_1);
mAnswer_2_Button = (Button) findViewById(R.id.answer_2);
mTextbody = (TextView) findViewById(R.id.text_body);
if(mTextbody.getText().toString().equals(getResources().getString(R.string.T1_Story)) && mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans1)) ){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T2_Story);
mAnswer_1_Button.setText(R.string.T2_Ans1);
mAnswer_2_Button.setText(R.string.T2_Ans2);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T2_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T4_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T3_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T6_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
**mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T5_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
}
}**
mTextbody.getText() always is empty where it's currently located (unless you have a default string in the TextView)
Move the if conditions into the onClick method body.
Conditionally perform the action. Don't conditionally add the listener.
And you only need one OnClickListener per button
In android,
for example
i want to perform a text.settext("something dynamic") until the buTTon is pressed. how to do it?
Try this:
android.os.Handler mHandler = new Handler();
Runnable rUpdateTextView = new Runnable() {
#Override
public void run () {
yourTextView.setText(returndate());
// Update your TextView every 200ms
mHandler.postDelayed(this, 200);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
mHandler.post(rUpdateTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mHandler.removeCallbacks(rUpdateTextView);
}
});
}
//click event for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time = returndate();
//set your text to textView
test.setText(time);
}
});
}
Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {