How can I fire Onclick event programmatically? - android

I have a custom view with 2 linear layouts: the first is the view's header and the second is the the details view.
In the custom view, the OnClickListener of the header Linearlayout is already defined: when it fires, it collapses/expandes the second linearlayout.
What I want to do is to add more functionalities to my header's OnClickListener event (i.e.: collapse/expand the second layout and show a Toast).
I can't modify the source code of the custom view. I tried to set a new OnClickListener but it hides the initial event (collapse/expand).
How should I implement this?
The source code of My Custom View:
public class ExpandoLayout extends ViewGroup
{
/* some declarations */
private Linearlayout header;
private linearlayout footer;
/* some code */
#override
protected void onFinishInflate() {
header= new LinearLayout(context);
header.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toggleExpand();
}
});
}
}
What I want to do is to add some code to the already defined OnClickListener event in my activity.
Something like that:
public class myActivity extends Activity {
private Linearlayout myCustomView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rsdetail);
myCustomView= (MyCustomView) findViewById(R.id.expanded);
myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v instanceof LinearLayout)
{
v.performClick();
Toast.makeText(getActivity(), "ExpandoOnClickListener", 2000).show();
}
}
});
}

You can programmatically raise click event on a View to call it's OnClickListener as below:
view.performClick();
Now if you call this on your second layout under first layout's OnClickListener then i hope it should do the magic

Simple solution would be to get original OnClickListener and then fire it in new one:
final OnClickListener preDefinedListener = myCustomView.getChildAt(0).getOnClickListner();
myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v instanceof LinearLayout)
{
preDefinedListener.onClick(v); // calls default (defined by MyCustomView)
Toast.makeText(getActivity(), "ExpandoOnClickListener", 2000).show();
}
}
});
Sadly, View does not have getOnClickListner(), but I guess you can use reflection to get it. It is stored in the field mOnClickListener (source).
This is how you can get OnClickListener defined for your layout:
OnClickListener tmpOnClickListener = null;
try {
Class<View> cls = (Class<View>) Class.forName("android.view.View");
Field fld = cls.getDeclaredField("mOnClickListener");
fld.setAccessible(true); // because it is protected
tmpOnClickListener = (OnClickListener) fld.get(myCustomView.getChildAt(0));
fld.setAccessible(false); // restore it's original property
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
final OnClickListener preDefinedListener = tmpOnClickListener;
if (preDefinedListener != null) {
myCustomView.getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
preDefinedListener.onClick(paramView);
Toast.makeText(getActivity(), "ExpandoOnClickListener", Toast.LENGTH_LONG).show();
}
});
I didn't really bother to handle all exception correctly, but it's enough to get the idea.
It might look messy, but it's actually just 5 lines of new code to solve your problem.

Since SDK 15 you can just call method:
view.callOnClick()

If you cannot modify the code of CustomView then you have the below option.
Extend the CustomView.
Override the onClick() method and bind this as a Listener to the first layout.
Inside onClick() first call super.onClick() then add your additional functionality below.
This way you are keeping the existing code and adding additional functionality as well.

Related

How can I know which button is clicked inside my CardView if I have a dynamic RecyclerView/CardView?

I have two imageviews with in my card view, now how can I setOnClickListeners so that I can know which button in which cardview is selected. im1 and im2 are my clickable ImageViews
This is my code :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ConnIfInfo dataModel = ifList.get(position);
Log.d("Name", "if list name: "+dataModel.getName());
holder.name.setText(dataModel.getName());
holder.appName.setText(dataModel.getApp().toString());
if(String.valueOf(dataModel.getPreferredModeMode().toString()) .equals( String.valueOf(ProjectionTypes.OperationMode.AOA_AA)))
{
holder.im1.setImageResource(R.drawable.auto1);
holder.im2.setImageResource(R.drawable.carlife);
holder.im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(1,"Nexus 5");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(2,"Nexus 6");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
#Override
public int getItemCount() {
return ifList.size();
}
You can set the tag to the view. The tag can be any object.
holder.im2.setTag(position);
you can set many tags also, with the key, and the key should be unique resId. you can dump the ids in ids.xml. Refer this
holder.im2.setTag(<res_id>, position);
and you can get the tag as
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag()
//or if you set with key then
v.getTag(<res_id>)
// please check for null value
}
});
instead of setting the onClickListener inside the onBindViewHolder you can do that in ViewHolder class itself
You can also do this by using Interface also
For more details please check out This link contains the brief about the how to handle click event by using interface.

Disable click after clicking the textview

I have a TextView and I put a OnClickListener on this TextView. I use this action to load custom view onto a LinearLayout.
But when I click on this TextView twice, custom view is repeating on the LinearLayout. I clear all custom views on this LinearLayout before I load new custom views on to this LinearLaout.
This is my OnClickListener on TextView,
TextView rejectedTitleTextView = (TextView) findViewById(R.id.roster_menu_rejected_title);
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rejectedTitleTextView.setBackgroundColor(getResources().getColor(R.color.acceptedPurpleColour));
newTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
acceptedTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
locationLinearLayout.removeAllViews();
rosterBottomLayout.setVisibility(View.GONE);
Log.d("CHECK_ACTION"," REJECTED_TEXT_VIEW ");
InternetConnectivity internetConnectivity = new InternetConnectivity();
final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);
if(isConnectedToInternet==true) {
try {
Thread.sleep(1300);
} catch (Exception e) {
e.printStackTrace();
}
getDataFromServer("REJECTED");
}else{
Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
getDataFromServer("REJECTED");
is the method which I used to load custom view onto this LinearLayout.
How can I prevent this issue ?
Have any ideas ?
Inside onclickListener put
rejectedTitleTextView.setClickable(false);
and once finish your functionality make it as true because u need to click for next time .
rejectedTitleTextView.setClickable(true);
Inside setOnclickListener try below code:-
textView.setClickable(false);
Try this
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSpinner.setEnabled(false);
mSpinner.postDelayed(new Runnable() { #Override public
void run() {
mSpinner.setEnabled(true); }
}
// do your stuff here
});
You can maintain boolean value like this
boolean isClick=false;
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isClick)
{
//do your Stuff on onCLick
isClick=true;
}else
{
//leave it blank if you do not want to do anything second time
}
}
});

which Onclicklistener gives best performance and less memory leak on listView

I'm working with very long lists that can have 4+ views with clicks for row.
(ListView)
Method 1:
in listview public View getView()
vh.pprofile.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
JSONObject miid;
try {
miid = listArray.getJSONObject(position);
MyApp.goProfile(activity, false, miid.getString("user_id"), 1);
} catch (JSONException e) { }
}
});
Method 2:
in ListView public View getView()
vh.pprofile.setTag(position);
vh.pprofile.setOnClickListener(onClickListener);
In ListView class
MyOnClickListener onClickListener = new MyOnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == vh.pprofile.getId()) {
JSONObject miid;
try {
miid = listArray.getJSONObject(Integer.valueOf(v.getTag().toString()));
MyApp.goProfile(activity, false, miid.getString("user_id"), 1);
} catch (JSONException e) { }
}
}
};
New Class:
public class MyOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {}
}
None really. What you are comparing has no difference in performance. Neither option will lead to memory leaks. A difference in performance only appears as soon as you compare creating several anonymous click listeners (option 1 multiple times) to reusing a specific click listener for different views (option 2 with switch-statement). See details in my explanation here.

OnClickListener of ImageView is not Working

image123(ImageView) works normal but when it comes to ONclickListener it does not work even after using different methods of Calling the listener.
I put func(),which contain findviewbyid of image123 (ImageView) just after SetContentView() and it works fine .OnclickListener of image123 does not works when func() is in OnClicklistener of Reset Button as Shown in the Code.
Problem:
How can i call the func() in reset button onclicklistener as well as in start at Oncreate() Activity?
It catch an exception shown in Log cat
- Exception at Onclick Listener" ,"java.lang.NullPointerException
public class MainActivity extends Activity {
ImageView image123;
int[] resultid=new int[25];
Button buttonExit;
Button buttonreset;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonreset=(Button) findViewById(R.id.button_reset);
buttonExit=(Button) findViewById(R.id.button_exit);
v1=(TextView)findViewById(R.id.name);
v2=(TextView)findViewById(R.id.fruit_count);
//--------------------------Exit Button---------------------------
buttonExit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//*******************Exception comes here*********************
try{
image123.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d("Listner Called", "Listner Called");
}
});
}
catch(Exception e)
{
Log.d("Exception at onclickListener", e.toString());
}
buttonreset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
func();
}});
}
void func()
{
String as=null;
int resultid;
SecureRandom random=new SecureRandom();
image123=new ImageView(this);
as=("img"+1).toString();
resultid=getResources().getIdentifier(as, "id","com.example.selectiongame" );
Log.d(as, ""+resultid);
image123=(ImageView) findViewById(resultid);
as="drawable"+(random.nextInt(3)+1);
resultid = getResources().getIdentifier(as, "drawable", "com.example.selectiongame");
Log.d(as, ""+resultid);
image123.setImageResource(resultid);
}
Try creating an array of ResIDs and an array of ImageViews. Then do something like this instead of initializing them with findViewById()...
for(int i = 0; i < res_arr.length; i++)
{
images[i] = new ImageView(this);
images[i].setImageResource(res_arr[i]);
images[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Button_Clicked", "Button Clicked");
}
});
}
You must not findviewbyid() of an ImageView in some other function other than Oncreate(Bundle b) or Some Listener(e.g OnclickListener ,onselectionchange etc) . if you want to findviewbyid(...) of a view in a function other than Oncreate ,Atleast Call that function once after SetContentView() and other prerequisite Findviewbyid(...).
2.
In order to Refresh your Activity ,when you can not use function containing findviewbyid(...) of ImageView,like in my case, in body some button(e.g Refresh ,Reset) OnclickListner .You must write the following code
buttonreset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Bundle newBundle=new Bundle();
onCreate(newBundle);
//func(); >>We can not use this func() here if we are using it in Oncreate(...) in case of Array of ImageView
}});
}
For views other than Imageview ,Maybe Regular Line of Code work ...But in case of ImageView or Array of ImageView ....These things must be kept in mind and Simply Oncreate() must be used to Refresh.

Android: Create a new intent with a button from a GridView (GridView with Navigation buttons)

I have created a class of type BaseAdapter that is populated with buttons - when you click on a button I want to load a new intent. This has proven difficult on two levels:
You cannot associate the event with the button (one that creates a new intent) inside the Adapter. This is why I send the Buttons as an array to my Adapter (this solution works, but it is messy)
Even though my buttons are created inside the same Activity - they cannot create a new intent from that Activity. The exeption is so great that I have not even gotten a try...catch statement to work.
I have tried reflection, creating the buttons inside the activity and passing them through, passing the context (to call context.startIntent(...))
My question: can someone show me how to create a ButtonAdapter where each button creates a new Intent - even of the same type as the original Activity?
UPDATE: Here is the code because I am getting answers from people who think I am struggling with onClickListeners:
public class ButtonAdapter extends BaseAdapter
{
private Context _context;
private Button[] _button;
public ButtonAdapter(Context c, Button[] buttons)
{
_context = c;
_button = buttons;
}
// Total number of things contained within the adapter
public int getCount()
{
return _button.length;
}
// Require for structure, not really used in my code.
public Object getItem(int position)
{
return _button[position];
}
// Require for structure, not really used in my code. Can
// be used to get the id of an item in the adapter for
// manual control.
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
_button[position].setId(position);
return _button[position];
}
}
---------------
The Activity:
public class MainActivity extends Activity
{
private GridView _gv;
private TextView _heading;
private ButtonAdapter _adapter;
public void LoadActivity(String heading)
{
try
{
Itent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Level", "NextPage");
intent.putExtra("Heading", heading);
startActivity(intent);
}
catch (Exception ex)
{
Toast.makeText(getApplicationContext(), String.format("Error LoadActivity: %s", ex.getMessage()), Toast.LENGTH_SHORT).show();
}
}
private void createButtonsAdapter(Button _button[])
{
_buttonadapter = new ButtonAdapter(getApplicationContext(), _button);
_gv.setAdapter(_adapter);
}
private void setupButtons()
{
Button[] _buttons = new Button[2];
String names[] = new String[]{"Button1","Button2"};
for (int i = 0; i < 2; i++)
{
_buttons[i] = new Button(this);
_buttons[i].setText(names[i]);
_buttons[i].setTag(names[i]);
_buttons[i].setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
try
{
LoadActivity(((Button)arg0).getTag().toString());
}
catch(Exception ex)
{
Toast.makeText(getApplicationContext(), String.format("Error button.onClick: %s", ex.getMessage()), Toast.LENGTH_SHORT).show();
}
}
});
}
createButtonsAdapter(_buttons);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_gv = (GridView)findViewById(R.id.gridview);
_heading = (TextView)findViewById(R.id.tv_heading);
Bundle params = getIntent().getExtras();
if (params == null)
{
setupButtons();
}
else if (params.containsKey("Level"))
{
_heading.setText(params.getString("Heading"));
if (params.getString("Level").equals("NextPage"))
{
//code not here yet
}
else if (params.getString("Level").equals("Chapters"))
{
//future code
}
}
}
}
Excuse the bold and caps but I have had enough silly answers to warrent this:
I HAVE TRIED PUTTING THE ONCLICKLISTENER INSIDE THE GRIDVIEW AND IT DOES NOT WORK EITHER
You cannout load an activity from a class outside that activity, even if you pass the context as a parameter. That is why I have resorted to this method, which completely bombs android, even though I have try catch statements.
Please try give me a solution in the form of a correction to my code, other code, or a tutorial that achieves what I want here. I know how to do a button adapter properly, it is the act of loading an Intent that has forced me to implement it this way.
I suggest the following,
Using a common onClick listner for all the buttons in your grid view
Set tag for all the butons in the getView func. of adapter.
Use the tag Object to decide on the intent to fire from the onClick listener.
I hope it helps..
I guess you can easily manipulate your buttons created in your class extending Base adapter. In the getView method .... if you have button b.. then do it as follows
b.setOnClickListener(new OnClickListener()
{
public void onClick()
{
// Do your stuff here ...
}
});
and if you want to start another activity on Click of this button then you need to pass the calling context to this adapter.
Once again I am answering my own question:
http://bottlecapnet.blogspot.com/2012/01/android-buttons-have-no-place-inside.html
I will just have to style TextViews as I want to see them.

Categories

Resources