how to know which button called a method? - android

I'm using a method : button_click(View view) to set a text on an editText , and I have a lot of buttons and each one of them should set text on a specific editText .
Is there any way to know which button called the method , so that I can set text of the correct editText ?
here is the code of the method :
public void button_click(View view) {
// Create the dialog
final Dialog mDateTimeDialog = new Dialog(view.getContext());
// Inflate the root layout
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final RelativeLayout mDateTimeDialogView = (RelativeLayout) inflater.inflate(R.layout.datepick, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
// Update demo edittext when the "OK" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.clearFocus();
// TODO Auto-generated method stub
String result_string = mDateTimePicker.getMonth()
+ "/"
+ String.valueOf(mDateTimePicker.getDay())
+ "/"
+ String.valueOf(mDateTimePicker.getYear())
+ " "
+ String.valueOf(mDateTimePicker.getHour())
+ ":"
+ String.valueOf(mDateTimePicker.getMinute());
edit_text1.setText(result_string);
mDateTimeDialog.dismiss();
}
});
// Cancel the dialog when the "Cancel" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimePicker.reset();
}
});
// Setup TimePicker
// No title on the dialog window
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}

The passed View is actually the button. All you need to do is switch the ID like so:
switch (view.getId()) {
case R.id.button1:
// Do something here related to button 1
break;
case R.id.button2:
// Do something here related to button 2
break;
}
Edit: Typo

the View you clicked upone is the argument of the onClick. If you need its id you can retrieve it through
int id = view.getId();
in this way you can switch upon the R.id.yourButtonId to understand which has been clicked

You're in luck - the View view that is passed to your handler is the actual view that was clicked. So the view itself is the button. Check out the docs.

In this method you have passed view which fired event (Button)v should give you reference
public void onClick(View v)

Related

Show hidden views of List item on click of button outside the list

I am developing an Android Application. In this app I am making a list of views using code given below. In each item of list there is delete button with visibility "Gone". Now there is another button Edit outside the list, on click of edit button I have to show delete button on each item of list. but using this code delete button shows only in the last item. Please help me to solve the problem. Thanks.
dynamicView();
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// lay.removeAllViews();
addnew.setVisibility(View.INVISIBLE);
btn_red.setVisibility(View.VISIBLE);
edit.setText("Done");
}
});
public void dynamicView() {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < templates.length; i++) {
final View customView = linflater.inflate(R.layout.order_template_item,
null);
btn_red=(ImageView)customView.findViewById(R.id.btn_negative);
btn_drag=(ImageView)customView.findViewById(R.id.button_drag);
btn_delete=(ImageView)customView.findViewById(R.id.button_delete);
final ImageView image = (ImageView)customView.findViewById(R.id.arrow);
final TextView text = (TextView)customView.findViewById(R.id.date);
final TextView sku = (TextView)customView.findViewById(R.id.time);
final TextView price = (TextView)customView.findViewById(R.id.last);
final TextView names =(TextView)customView.findViewById(R.id.name);
image.setId(i);
text.setId(i);
sku.setId(i);
price.setId(i);
names.setId(i);
btn_red.setId(i);
btn_red.setTag(i);
btn_delete.setId(i);
btn_drag.setId(i);
names.setText(templates[i]);
btn_red.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_delete.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(100,0 , 0, 0);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(1000);
btn_delete.setAnimation(anim);
}
});
btn_delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lay.removeView(customView);
}
});
lay.addView(customView);
}
You're holding reference only to the last one btn_red.
You can do something like this.
List<Button> buttons = new LinkedList<Button> ();
Then in your loop after findViewById on btn_red
buttons.add(btn_red);
And finally in your onClickListener
for (Button button: buttons) {
button.setVisibility(View.VISIBLE);
}
Do somethink like below to make visible all the button added to list items-
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addnew.setVisibility(View.INVISIBLE);
for (int i = 0; i < templates.length; i++) {
int id = btn_red.getId(i);
id.setVisibility(View.VISIBLE);
}
edit.setText("Done");
}
});

How do I add an clicklistener to a row in table Layout

i want to make a table layout in which there are 2 rows.
In both rows there are one Label and a Text Field when i press the 1st row or 2nd row the prompt Dialog will open and u enter any value it will set on Text field of selected row.
please guide me how to make a click listener on row with example and how to call a dialog when the row is selected.
TableRow row1 = findViewById(R.id.row2);
row1.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
// do any thing
}
});
// Method 2 ********************
TableRow row1 = findViewById(R.id.row2);
row1.setonClickListner(this);
public void onClicl(View v)
{
switch (v.getId())
{
case R.id.row1:
break;
}
}
Simply give each TableRow element a unique id and define an onClick
method:
<TableRow
android:id="#+id/one"
android:onClick="rowClick">
Find the row by id from layout and then add following in java class
tableRow= (TableRow) findViewById(R.id.one);
tableRow.setClickable(true);
tableRow.setOnClickListener(onClickListener);
private OnClickListener onClickListener= new OnClickListener() {
public void onClick(View v) {
show_dialog();
}
};
Then Call Following Method
public void show_dialog() {
final Dialog dialog = new Dialog(getApplicationContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow();
dialog.setContentView(R.layout.yourlayout);
dialog.setTitle("yor title");
dialog.setCancelable(false);
final Button btnOkDialog = (Button) dialog.findViewById(R.id.ResetOkBtn);
btnOkDialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
try {
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
First you have to give your TableRow an id on your xml file
<TableRow
android:id="#+id/row1"
...
>
Set the children of your row(in your case a TextView and an EditText probably) to NOT clickable
android:clickable="false"
Now on your java file find your tablerow with the id and add onClickListener
TableRow row1 = findViewById(R.id.row1);
d.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Now to open a dialog read this
Just a guess.
Make the EditText clickable. It might be what you are really looking for
//if we want to applay listener on dynamic tablerow then use this
//sure that perfect
TablRowe tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
tr.setOnClickListener(this);
#Override
public void onClick(View v)
{
switch (v.getId())
{
case 100:
Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();
break;
case 101:
Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();
break;
}

Android Trouble with Edit Text Views

In my application,on press of a button "Add contact" phone book gets open and then user selects a contact that get displayed in Edittext View and when another button "Add More Contacts" is pressed, a another Edit-Text View gets displayed on the top for which i can again select the contact from phone book. But the problem that i am facing is that i want that user can only add up to 5 Edit Text .
I am using the following code, but its force crashing the application. Please help.
And also i want the Edit Text Views to be non editable, for which i tried editable=false but its working only for the first Edit Text View not on the other Views, that user adds afterwards.
int id = 1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(OnClick());
EditText editview = new EditText(this);
editview.setText("Add more");
editview.setEnabled(false);
editview.setFocusable(false);
}
// implementing OnClickListener OnClick() method for "btn_addmore_cntct"
// button
private OnClickListener OnClick() {
// TODO Auto-generated method stub
// changing return type "null" to "new OnClickListner"
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(layoutLinear.getChildCount()>5){
}else{
final EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setId(id);
id++;
layoutLinear.addView(tab,0);
tab.requestFocus();
}
Just check number of layoutLinear child
if(layoutLinear.getChildCount()>5){
//nothing to do
}else{
//create new EditText and add to layoutLinear
}
user this code to disable EditText
edittext.setEnabled(false);
edittext.setFocusable(false);
You can use following way.
int temp;
public void onCreate(Bundle savedinstance) {
super.onCreate(savedinstance);
setContentView(R.layout.yourlayout);
temp=1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(temp<=5){
EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setEnabled(false);
tab.setFocusable(false);
layoutLinear.addView(tab);
temp++;
}else{
//Print message to user Cant add more editText.
}
}
});
}
Let me know its working or not.

Button location in listview android

I have a button on the listview when click on that will show popup. I have implemented that successfully. But the issue is that i want to display popup on the clicked button position.
View layout = inflater.inflate(R.layout.popup_layout,null);
pwindo = new PopupWindow(layout, 300, 250, true);
pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, 30, 40);
pwindo.setOutsideTouchable(true);
pwindo.setTouchable(true);
pwindo.setBackgroundDrawable(new BitmapDrawable());
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
pwindo.dismiss();
return false;
}
});
Button code
vi = inflater.inflate(R.layout.list_row, null);
vi.findViewById(R.id.statusImage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showPopup();//to display popup thats given above
}
});
You need to set buttons tag as its position, and onClick you need to get it:
button.setTag(position);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int position = (Integer) v.getTag();
// Do whatever you like with position
}
});
when the button is clicked you can get the position of the item in the list. Use
list.getFirstVisiblePosition();
to get the buttin postion. substract the FirstVisiblePosition from the position. you will get the click postion. and you can show the pop up in that postion.
You can get position for button click in listview by following code.
(vi.findViewById(R.id.statusImage)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(Classname.this, "POSITION"+ position,Toast.LENGTH_SHORT).show();
showPopup();
}
});
Use QuickAction. Examples :
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
http://www.edumobile.org/android/android-apps/quick-action-demo/
https://github.com/alhneiti/Android-QuickAction

How to have data from dialog box to listview in android 2.1?

all
i have created listview dynamically.now i want to change the name of listview/listitems or i want to retrieve content below each row but the content is coming from dialogbox.is it possible to have data from dialogbox in listview?How to achieve this??can any one guide or give some sample code of the same?
Thanks in Advance--
public class Tdate extends Activity
{
private ListView lView;
private String lv_items[] = { "Birth_Date", "Anniversary_Date", "Joining_Date","Meeting_Date","Appraisal_Date","Anniversary_Date", "Joining_Date","Meeting_Date","Appraisal_Date"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tdate);
Button customdate = (Button)findViewById(R.id.customdate);
lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
showDateTimeDialog();
}
});
}
private void showDateTimeDialog()
{
// Create the dialog
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
// Check is system is set to use 24h time (this doesn't seem to work as expected though)
final String timeS = android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.TIME_12_24);
final boolean is24h = !(timeS == null || timeS.equals("12"));
// Update demo TextViews when the "OK" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.clearFocus();
((TextView) findViewById(R.id.Date)).setText(mDateTimePicker.get(Calendar.YEAR) + "/" + (mDateTimePicker.get(Calendar.MONTH)+1) + "/"
+ mDateTimePicker.get(Calendar.DAY_OF_MONTH));
if (mDateTimePicker.is24HourView()) {
((TextView) findViewById(R.id.Time)).setText(mDateTimePicker.get(Calendar.HOUR_OF_DAY) + ":" + mDateTimePicker.get(Calendar.MINUTE));
} else {
((TextView) findViewById(R.id.Time)).setText(mDateTimePicker.get(Calendar.HOUR) + ":" + mDateTimePicker.get(Calendar.MINUTE) + " "
+ (mDateTimePicker.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM"));
}
mDateTimeDialog.dismiss();
}
});
// Cancel the dialog when the "Cancel" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog)).setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime)).setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
mDateTimePicker.reset();
}
});
// Setup TimePicker
mDateTimePicker.setIs24HourView(is24h);
// No title on the dialog window
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}
}

Categories

Resources