Android pass onClick to child - java

Hello my goal is to make my custom view (ViewCircleOfInterest.java) receiving onClick. Now every time I click onClickListener on ViewCircleOfInterest.java is not called.
My interesting part of layout looks like that:
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
I have FrameLayout on my layout and I added view (SurfaceView - CameraPreview.java) to it like that
MyActivity.java
rootView = findViewById(android.R.id.content).getRootView();
viewCircleOfInterest = rootView.findViewById(R.id.view_circle_of_interest);
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(NewWayMeasurementActivity.this, "circle a", Toast.LENGTH_SHORT).show();
}
});
(...)
((FrameLayout) rootView.findViewById(R.id.camera_view)).addView(cameraPreview);
(...)
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
On CameraPreview.java in my init() method I do like that:
(...)
viewCircleOfInterest = ((NewWayMeasurementActivity)activity).viewCircleOfInterest;
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "circle aa", Toast.LENGTH_SHORT).show();
}
});
I also set onClickListener on my ViewCircleOfInterest.java
public ViewCircleOfInterest(SurfaceView surfaceView) {
super(surfaceView.getContext());
this.surfaceView = surfaceView;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(surfaceView.getContext(), "circle aaa", Toast.LENGTH_SHORT).show();
}
But unfortunately non of this method works, I still cannot receive onclicks from my custom view. How to solve that? I hope my question and code is understandable.

Make these changes in your code and check if it works.
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content"/>
</FrameLayout>
MyActivity.java
((FrameLayout)rootView.findViewById(R.id.camera_view)).addView(cameraPreview,0);
Here we are adding cameraPreview at 0 index so your custom view is on top now and that should receive clicks.

Related

BottomSheetBehaviour is dissmised when user clicks on back

I have a BottomSheetBehaviour in my layout, and it works absolutely fine. The problem is, that whenever user clicks on back button, it becomes invisible(or gone). How can I prevent it to be non-dissmissable?
create a layout for your bottom sheet as:
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
app:behavior_peekHeight="260dp"//change according to your need
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//your design here
</LinearLayout>
</androidx.core.widget.NestedScrollView>
include this layout in your main layout file
<include layout="#layout/layout_name" />
now you can use your bottom sheet in your activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
View bottomSheet = findViewById(R.id.bottomSheet);
//add behaviour as
BottomSheetBehavior<View> bottomSheetBehavior =BottomSheetBehavior.from(bottomSheet);
//access your views inside bottomSheet as
TextView textView = bottomSheet.findViewById(R.id.textViewId);
}
Just override onCancel() or onDismiss().
#Override
public void onCancel(DialogInterface dialog) {
// super.onCancel(dialog); // comment this out
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}
Or override onBackPressed in onCreateDialog function when you're crating the dialog
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()){
#Override
public void onBackPressed() {
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}
};
}

Why does my app keep crashing when I have 2 setOnclickListener?

My app keeps crashing when I set 2 setOnclickListener. The interesting thing is, when I only have 1 button intent, it doesn't crash. When I have 2 buttons, the new button does not bring me to new screen, and the old one just crashes the whole app.
Here's my code for the "old" button:
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
If that's the only thing, then everything works. But when I add following, the new one doesn't work and old one crashes:
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Here's the Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
at com.hoversfw.notes.LoginActivity.onCreate(LoginActivity.java:96)
And LoginActivity.java Line 96 is the beginning of Support.setOnclickListener.
Also, I have everything declared like:
Support=findViewById(R.id.Support);
SignUp=findViewById(R.id.textView);
Here's my XML file code:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Already have an account? Sign in!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/Support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="29dp"
android:text="Support"
app:layout_constraintEnd_toStartOf="#+id/about"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
I also have all ID matched, Android Studio detects no error. I wonder what's causing the problem.
UPDATE
I have everything matched, like java file and xml, no ID is same, everything declared. And I don't think there is any problem with XML.
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
/*Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});*/
If it's like above, then SignUp it works. If it's like
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Both of them doesn't work. (The difference is last one I commented Support.setOnClickListener..... but this one I made it as code.)
I can't confirm directly your error because you don't show your xml, but maybe you can do it.
you need check id view in xml same with you call in activiy with findViewById()
findViewById must above in your listener
If you are using 2 buttons, then you need to link them as button for both of them.
UPDATED
Since, you are using button and textview. I updated the answer.
This code at xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<TextView
android:id="#+id/txtViewSupport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Support" />
<Button
android:id="#+id/btnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp" />
</LinearLayout>
This code at Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button buttonSignUp = findViewById(R.id.btnSignUp);
TextView textViewSupport = findViewById(R.id.txtViewSupport);
buttonSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
textViewSupport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}

Animation freeze with swipelayout

after adding 2 new features (swipelayout + animation) my ui freezes.
I have a recyclerview which includes movie objects. If i add/remove/modify a movie i call my moviemanager because there's a notify concept (all views which want to use the data have to be registered on this manager).
This all works fine before i added the new features.
I use this swipelayout:
https://github.com/daimajia/AndroidSwipeLayout
Currently the items looks like this:
After swiping:
The freeze is always reproducible if i have 4 items. I swipe on the second item and delete it. Then it will dissapear like i want. After im swiping the "new" second item there's a freeze for like 10-15 seconds.
The layout xml of the item:
<com.daimajia.swipe.SwipeLayout
android:id="#+id/item_movie_swipe_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/item_movie_swipe_bottom_wrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ff0000">
<TextView
android:id="#+id/item_movie_bottom_delete_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_delete_white_24dp"
android:text="#string/delete"
android:textColor="#fff"
android:layout_gravity="center"
android:padding="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/item_movie_list_poster"
style="#style/image_border_style"
android:layout_width="68dp"
android:layout_height="100dp"
android:layout_marginRight="3dp"
android:background="#android:color/black"/>
<TextView
android:id="#+id/item_movie_list_name_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="3dp"
android:layout_toEndOf="#+id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:gravity="center|left"
android:padding="3dp" android:text="name"/>
<TextView
android:id="#+id/item_movie_list_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/item_movie_list_name_year"
android:layout_marginBottom="3dp"
android:layout_toEndOf="#+id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:padding="3dp"
android:text="Drama - Komödie - Action" android:textColor="#color/item_textyear"
android:textSize="12dp"/>
<TextView
android:id="#+id/item_movie_list_viewed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/item_movie_list_poster"
android:layout_toRightOf="#+id/item_movie_list_poster"
android:paddingLeft="3dp" android:text="Gesehen: "
android:textColor="#color/accept"
android:textSize="12dp"/>
</RelativeLayout>
</com.daimajia.swipe.SwipeLayout>
Relevant code of the adapter:
#Override
public void onBindViewHolder(final CDMMoviesAdapter.IC_ViewHolder p_Holder, final int p_iPosition) {
// more unimportant code...
p_Holder.m_SwipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
// Drag From Right
p_Holder.m_SwipeLayout.addDrag(SwipeLayout.DragEdge.Right, p_Holder.m_SwipeLayout.findViewById(R.id.item_movie_swipe_bottom_wrapper));
// Handling different events when swiping
p_Holder.m_SwipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
#Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
#Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
#Override
public void onStartOpen(SwipeLayout layout) {
}
#Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
#Override
public void onStartClose(SwipeLayout layout) {
}
#Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
p_Holder.m_SwipeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
if((((SwipeLayout) p_View).getOpenStatus() == SwipeLayout.Status.Close)) {
Intent l_Intent = new Intent();
Bundle l_Bundle = new Bundle();
l_Bundle.putLong(CDMMovieDetailsActivity.ARG_MOVIE, l_MovieID);
l_Intent.putExtras(l_Bundle);
l_Intent.setClass(l_Context, CDMMovieDetailsActivity.class);
l_Context.startActivity(l_Intent);
}
}
});
p_Holder.m_tvDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
Animation l_DeleteAnimation = AnimationUtils.loadAnimation(l_Context, R.anim.slide_out);
l_DeleteAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mItemManger.removeShownLayouts(p_Holder.m_SwipeLayout);
CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition));
mItemManger.closeAllItems();
}
});
p_Holder.m_View.startAnimation(l_DeleteAnimation);
}
});
// mItemManger is member in RecyclerSwipeAdapter Class
mItemManger.bindView(p_Holder.itemView, p_iPosition);
// more unimportant code...
}
/**
* This class is the viewholder. For internal use only.
*/
public static class IC_ViewHolder extends RecyclerView.ViewHolder {
/**
* Constructor
*
* #param p_View the view
*/
public IC_ViewHolder(View p_View) {
super(p_View);
m_View = p_View;
m_SwipeLayout = (SwipeLayout) p_View.findViewById(R.id.item_movie_swipe_layout);
m_tvNameYear = (TextView) p_View.findViewById(R.id.item_movie_list_name_year);
m_ivPoster = (ImageView) p_View.findViewById(R.id.item_movie_list_poster);
m_tvGenre = (TextView) p_View.findViewById(R.id.item_movie_list_genre);
m_tvViewed = (TextView) p_View.findViewById(R.id.item_movie_list_viewed);
m_tvDelete = (TextView) p_View.findViewById(R.id.item_movie_bottom_delete_desc);
}
public View m_View;
public SwipeLayout m_SwipeLayout;
public TextView m_tvNameYear;
public ImageView m_ivPoster;
public TextView m_tvGenre;
public TextView m_tvViewed;
public TextView m_tvDelete;
}
In CDMMovieManager.getInstance().removeMovieID(m_List, m_MovieIDs.get(p_iPosition)); the movie will be removed and all registered views will be notified. In this case the fragment of this adapter will call m_Adapter.notifyDataSetChanged();
The animation file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromXDelta="0%"
android:toXDelta="-100%"/>
</set>
I tried things like View.clearAnimation() or Animation.setAnimationListener(null) but i always get this problem.
Plase ask if you need more informations.
Update:
Problem only occures if i try to swipe the next item. (For example delete 2nd item. After that try to swipe the 3rd item which is the new second item -> freeze on the item i tried to swipe). The freeze can only be removed on swiping on any other item.
I solved my problem. This answer helped me: How to animate RecyclerView items when they appear
I added this code to my adapter:
#Override
public void onViewDetachedFromWindow(IC_ViewHolder p_Holder) {
p_Holder.m_View.clearAnimation();
}

Can I open a seperate XML layout with a OnLongClick

I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.
This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?
XML main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.how_to_use.MainActivity"
android:id="#+id/main_view">
<TextView
android:id="#+id/long_press_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long Press To Open \n'Drag n Drop'"/>
</LinearLayout>
XML 2nd page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/swirl_img"
android:src="#drawable/Swirl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private LinearLayout mainView;
private TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.long_press_textView);
addListenerLongPressBtn();
addListenerOnClickBtn();
}
public void addListenerOnClickBtn() {
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(this, DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
}
I found a solution to my own problem.
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(view.getContext(), DragDropActivity.class);
startActivity(intent);
return true;
}
});
}

create one button and method [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am new in java or android just create one demo.
I found good example on google: https://github.com/rciovati/Android-KeyboardView-Example
Now I want to create one button and method in Java and on them I click the button and the method is invoked and will show a hello world message.
I need good help on this because I don't know where I want to change and where to put code for create button.
I am not sure about your problem but I assume you want to define click listener with method which prints Hello World message.
Include android:onClick attribute in your Button inside XML layout.
For example: android:onClick="btnHelloWorldClick"
Define method in Activity class with the same name given in android:onClick attribute.
For example:
public void btnHelloWorldClick(View v)
{
System.out.println("Hello world");
// or Toast.makeText(this, "Hello world", Toast.LENGTH_LONG).show();
}
Your code,
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Hello World", Toast.LENGTH_LONG).show();
}
});
and your layout,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="232dp"
android:text="Button" />
</RelativeLayout>
You can do:
First add attribute "android:onClick="btnHelloWorldClick"" in Button controller in xml layout.
For Example:
Define button in xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
MainActivity.java
setContentView(R.id.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// do something or call a function defined.
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG).show();
}
});
Define onclick attribute for button in xml
activtiy_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:onClick="btnClick" // method
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
MainActivity.java
public void btnClick(View v)
{
// do something
Toast.makeText(MainActivity.this, "Hello world", Toast.LENGTH_LONG).show();
}
To create a button programatically
MainActivity
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativelayout);
// Define relative layout in xml layout file.
Button b = new Button(MainActivity.this);
rl.addView(b);
// button click using annonymous inner class
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// do something or call a function defined.
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG);
}
});
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/relativelayout"
android:layout_height="match_parent"
tools:context=".MainActivity" >
Your class can also implement OnClickListener. In that case
public class MainActivity extends Activity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1)
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) //returns int
{
case R.id.button1 :
// do something or call a function defined.
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG);
break;
}
}
}
Well this question is most likely to get closed but here's what you need to know
To handle the clicks for any view let it be a Relative layout,button,textview ,etc you need to implement the onClickListener and perform function inside the onclick function of the interface
there are many ways you can set onclick listeners:
1 Let you activity implement onclickListener it will ask you to implement onClick function and you can run your function there
public class HomeScreenActivity extends Activity implements OnClickListener {
public void onClick(View v) {
int clickedId = v.getId();
switch (clickedId) {
case R.id.nearbyButtonHome:
nearbyButtonAction();
break;
case R.id.cgExButtonHome:
cgExButtonAction();
break;
case R.id.styleButtonHome:
styleButtonAction();
break;
}
}
}
2 you can set the onclick listeners on button object directly
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
3 you can add a attribute to button tag in layout file
android:onClick="func1"
and in your activity create this function.
this is all about the button clicks

Categories