How To Set Different Actions To Dynamiclly Created Button On Android (specifically - Auto Scroll To A Textview Using A Dynamic Button)
i created a layout containing multiple text views. i saved the text view's ids in an ArrayList which is a class variable called _resultId. now i want to create buttons which suppos
Solution 1:
after learning more on Runnable interface i Found an answer so for those who will struggle with something similar: you need to create a new class altogether that implements both runnable and the OnClickListener.
this class will contain extra data and a constructor to match your needs. when you set the onClick method of the button you create a new object of that class. for instance in my case the new method looked like:
publicclassButtonHandlerimplementsOnClickListener,Runnable
{
privateString _data;//data to be passed for the buttonprivateArrayList<Integer> _resultId;//the id's of the text viewsprivateActivity _activity;
publicButtonHandler(String data,ArrayList<Integer> resultId,Activity activity)
{
_data=data;
_resultId=resultId;
_activity=activity;
}
@Overridepublicvoidrun()
{
View currentView=_activity.findViewById(_resultId.get(Integer.valueOf(_data)));
ScrollView scrollView=(ScrollView) _activity.findViewById(R.id.resultScroll);
scrollView.scrollTo(0, currentView.getTop());
}
@OverridepublicvoidonClick(View v)
{
newHandler().post(this);
}
}
now to call it from the main activity i use:
privatevoidaddNavigationView(ViewGroup navigationLayout, ArrayList<Perek> mishnayot)
{
for (int i=0;i<mishnayot.size();i++)
{
_counter=i;
String currentOt=mishnayot.get(i).getOt();
Button button = new Button(getBaseContext());
button.setText(currentOt);
if (_resultId==null)
thrownew IllegalAccessError("missing result id link cannot be created");
button.setOnClickListener(new ButtonHandler(i+"",_resultId,this));
navigationLayout.addView(button);//add the button to panel
}
navigationLayout.setVisibility(View.VISIBLE);
}
Post a Comment for "How To Set Different Actions To Dynamiclly Created Button On Android (specifically - Auto Scroll To A Textview Using A Dynamic Button)"