Let's say i have a button which inserts a number to the front of a linked list and a button that inserts a number to the end of the linked list. I have this AlertDaialog and I'm trying to use it with both buttons. How can i differentiate the buttons from one another so that when i press "OK" on the dialog, the onClick method know where to insert the number(front or end).
I've tried somthing like this but it doesn't work, only the default case gets activated.
ok.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String input = inputText.getText().toString().trim();
int numberInput = Integer.parseInt(input);
switch (v.getId()){
case R.id.btn_front: //insert to the beginning
break;
case R.id.btn_end: //insert to the end
break;
default: Log.e("DIALOG_ERROR", "Error!");
}
}
});
Use a class that extend dialog like:
DialogClass obj=new DialogClass(context,true);
obj.show();
class will be:
public DialogClass(final Context context,Boolean isAddToFront) {
super(context);
setContentView(R.layout.dialog_layout);
setTitle(Title);
TextView Ok = (TextView) findViewById(R.id.ok);
Ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isAddToFront){
/// Add to front
}else{///add to back}
});
}
Related
This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}
I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}
Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};
I have two editTextField, the same listener is attached to both of them. How would I figure out in the listener which one is clicked?
inputStart = (EditText)findViewById(R.id.editText3);
inputStart.setOnClickListener(TimePickerButtonOnClickListener);
inputEnd = (EditText)findViewById(R.id.editText4);
inputEnd.setOnClickListener(TimePickerButtonOnClickListener);
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { }
};
I have checked, one possible solution is in this question,
but I am looking for any property like EditText gotEditField = (EditText)v.targetEditField;
Does any such property exist?
Use switch case, in onClick() with parameter View v using v.getId()
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
switch(v.getId){
case R.id.editText3:
break;
.
.
.
}
};
Also you can get the object of EditText using Type Casting the View v parameter..
Like EditText editText = (EditText)v;
View v from the parameters in your onCLick method is the view that was clicked.
EditText gotEditField = (EditText) v;
public void onClick(View v)
{
int id= v.getId();
switch (id)
{
case R.id.editText3:
break;
case R.id.editText4:
break;
}
}
I have two button b1 and b2 .If button b1 is pressed then perform a query q1 else if button b2 is pressed then perform an another query q2.
if(b1_click)
{
mCursor=//query
}
else if(b2_click)
{
mCursor=//query
}
Please tell me how can i implement this.How to implement b1_click method or any inbuilt method which tell that button is pressed.I tried
Cursor c;
c=//querys
if(b1.isPressed())
{
next.setOnClickListener
(
new View.OnClickListener()
{
#Override public void onClick(View v) {
c=db.getData1(); (getData1 method return cursor)
}
}
);
}
tv.append(c.getString(column_number) (tv=TextView)
"Same as above for b2"
It is saying that cursor (c) should be final
Help?
First of all I would recommend going through some tutorials Hello Android and you could also follow the code and samples in Common Tasks and How to do them in Android.
If you would read this carefully you would know that is very simple, something like this:
public class MyActiviy extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.myLayout);
findViewById(R.id.Button1).setOnClickListener(this);
findViewById(R.id.Button2).setOnClickListener(this);
//more code...
}
public void onClick(View v){
switch(v.getId()){
case R.id.Button1:
//Button1 pressedd...do stuff
break;
case R.id.Button2:
//Button2 pressed...do some other stuff
break;
default:
break;
}
}
}
Try creating your OnClickListeners as private classes:
private class Button1ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 1 is clicked.
}
}
private class Button2ClickedListener implements OnClickListener {
public void onClick( View v ) {
//Do what needs to be done when button 2 is clicked.
}
}
Then you set the onClick-listeners:
b1.setOnClickListener( new Button1ClickedListener() );
b2.setOnClickListener( new Button1ClickedListener() );
If you need the OnClickListeners to be able to use the cursor, you just need to declare it as a private field in the parent class of the OnClickListeners.