Writing onClick event for textviews inside linear layout - android

I have two linear layouts present inside relativelayout. Each linear layout contains three text view. I want to write onclick event for all text views present in both linear layouts in general. Please advice.

in all TextView add the following attribute
android:onClick="onClick"
dont forget to set id to all TextView too
Then from your code
public void onClick(View v){
switch (v.getId()) {
case R.id.tv1:
// do somethong
break;
default:
break
}

Have a common click listener for all the text views. From the common click listener handle the click events of all the text views by the ID of the text view.
Sample FYR.
findViewById(R.id.textview1_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview2_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview3_id).setOnClickListener(commonClickListener);
private OnClickListener commonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int selectedItemId = v.getId();
switch (selectedItemId) {
case R.id.textview1_id:
// implement your code here.
break;
case R.id.textview2_id:
// implement your code here.
break;
case textview3_id:
// implement your code here.
break;
}
}`

Related

How to get id of the item clicked from layout action in android?

I am validating the EditText value by clicking anywhere inside the window. But for some buttons, I want to do something different from the validation functionality. How can I get the id of the item from the layout action. Here my layout is ConstraintLayout. Tried the below code but it's not working.
window_layout.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tech:
setButtonsVisibility();
break;
}
}
});
Please suggest a better way to implement that.
What you're doing right now is adding the listener to the parent view. You need to set the listener to the individual views.
You can add a listener variable and add it to the different button views.
You can also iterate your constraint layout child views and setting the listener individually.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tech:
setButtonsVisibility();
break;
}
}
}
for(int i = 0; i <= windowLay.getChildCount(); i++) {
rec.getChildAt(i).setOnClickListener(listener);
}

Is there a way to write a same onclick function for multiple controls?

I have came across a problem where i have multiple text view that should have the same onclick function. I can obviously write multiple onclick but i want an optimized code.
For example i have textviews of color red and othe textviews of color blue and i want all red to have same onclick and all blue to have a same onclick function different than red.
Is there a way to achieve this in android java?
declare your click event in a variable from type View.OnClickListener and then assign it to your textviews like this:
View.OnClickListener eventVar = new View.OnClickListener() {
#Override
public void onClick(View view) {
//do what you need
}
};
textview1.setOnClickListener(eventVar);
textview2.setOnClickListener(eventVar);
textview3.setOnClickListener(eventVar);
textview4.setOnClickListener(eventVar);
Assign an id for red colour and another id for blue. And write a switch statement, if as below
switch (id)
case 0:
runAmethod();
break;
case 1:
runAnotherMethod():
break;
default:
Write a common function for your all the color textview like
private void clickOfRedTextView(){
// your code.
}
Now implement your activity or fragment with OnClickListener listener and pass the on click
textviewred1.setOnClickListener(this);
textviewred2.setOnClickListener(this);
// so on ...
In the override method of onclick you can have a switch case with view id as follow.
public void onClick(View v) {
switch (v.getId()) {
case R.id.textviewred1:
case R.id.textviewred2:
so on....
// your function you want to call will come here.
clickOfRedTextView();
break;
}
}
You can use this simplest way:
Define your onClick in your xml file for your red type textviews and blue type textviews like this.
<TextView
//other attributes...
android:onClick="redTextViewMethod"/>
<TextView
//other attributes...
android:onClick="blueTextViewMethod"/>
And then define the redTextViewMethod method in your corresponding Java file like this
private void redTextViewMethod() {
//your code
}
private void blueTextViewMethod() {
//your code
}
Solution:
First, create common public method for getting TextView click,
public void onTextViewClick(View v) {
switch (v.getId()) {
case R.id.VIEW_ID:
case R.id.VIEW_ID:
case R.id.VIEW_ID:
// Performs action for Blue Text View
break;
case R.id.VIEW_ID:
case R.id.VIEW_ID:
// Performs action for Red Text View
break;
}
}
Then, in your xml write TextView onClick like this,
<TextView
...
android:onClick="onTextViewClick"/>

How can I add touch listener for each element of an recyclerview item.

My case :
- I have a RecyclerView with complex item (many ImageView, texts .. )
- I use 4 ImageView as 4 buttons (Like, Comment, Share, Dislike)
- I want to use TouchListener for each button, because I want to control animation, also click event. ( I know how to use ClickListener for each item so please focus on TouchEvent ) Example : Facebook Comment button of each Story on NewsFeed, when you touch onto the button, animation scale icon. Click event just go on after release the button.
Problem is : I just can get Parent View of each Item (ex : LinearLayout contain other components of each item) by using recyclerView.findChildViewUnder(x,y).
So how to know which button was clicked like when we use clickListener :
- button1.setClickListener(this);
- button2.setClickListener(this); (on View Holder)
And after that catch event in Activity or Fragment :
- switch(view.getId()) {
case R.id.button1: .. do something; break;
case R.id.button2: .. do something; break;
....
I google this problem all day but all sample and answer just use ClickListener like I said above.
Thank you!
inside recyclerview adapter use like this
#Override
public void onBindViewHolder(final RecyclerViewAdapterA.ViewHolder_a holder, final int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You get parent view by by
View parent = recyclerView.findChildViewUnder(x,y);
Now you can easily get its corresponding views e.g.
Button button = (Button)parent.findViewById(R.id.button);
Hope you understand!!!

How is it possible to know which View is clicked in a layout OnClickListener?

I have a layout which contains some views. I want to set some actions when a user clicks anywhere on the layout, so I have set an OnClickListener for the layout which works as it should. But how can I know which view was clicked?
I want to assign different actions depending on the view which was clicked; or maybe no view was clicked and it was only the layout itself.
So the problem is if I set OnClickListener for one of the views, both OnCLickListeners related to the layout and the view will get activated, but I want only the view action. Thanks in advance.
Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).
Furthermore, if you set the listener for the layout the View argument received by the onClick method will always be the layout.
You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.
A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.
private final String TAG = "MainActivity";
List<View> views = new ArrayList<View>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv3 = (TextView) findViewById(R.id.tv3);
views.add(tv1);
views.add(tv2);
views.add(tv3);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isView = false;
for (View view : views) {
if (v.equals(view)) {
isView = true;
break;
}
}
if (isView) {
Log.e(TAG, "Click on view");
} else {
Log.e(TAG, "Click on layout");
}
}
};
tv1.setOnClickListener(clickListener);
tv2.setOnClickListener(clickListener);
tv3.setOnClickListener(clickListener);
rLayout.setOnClickListener(clickListener);
}
If you want different actions for different views, just create a listener for each of them.
For example Layout has two views View1 and View2. I assume that you have already inflated them. So you have to set OnClickListiner for each of them
Layout.setOnClickListener(this);
View1.setOnClickListener(this);
View2.setOnClickListener(this);
Then you have to override method:
public void onClick(View v)
{
switch(v.getId())
{
case R.id.id_for_layout:
// do what you want when user click layout
break;
case R.id.id_for_first_view:
// do what you want when user click first view
break;
case R.id.id_for_second_view:
// do what you want when user click second view
break;
}
}
Remember that the method OnClick have one parameter that indicates which View is clicked!
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
// first button
break;
case R.id.btn2:
// second button
break;
}
}
Assign the id to each of your views by android:id="#+id/v1"
then in
onClick(View v)
check for the view like
if(v == findViewById(R.id.v1))
{
// v1 specific action
}
else if(v == findViewById(R.id.v2))
{
// v2 specific action
}
Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener. So no need for a whole bunch of classes. One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):
public void onClick(View v) {
switch(v.getId()) {
case R.id.layout:
layout actions here
break;
case R.id.textview:
textview actions here
break;
this onClickListener is for all of your views ;)
There is View v parameter in onClickListner(View v) method :
#Override
protected void onCreate(Bundle savedInstanceState) {
LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
layout.setOnclickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.linearLayout){
//your logic
}
}
for anyone interested you can also log the currently clicked view:
getActivity()
.getWindow()
.getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.v("[onClick]", v.getClass().getCanonicalName());
}
return false;
});

NullPointerException at Radio Group on dialogbox

I have two radiogroup on FormVisitMapping.java, i put one of them on dialogbox which is trigger by button onClick(). but i get an error nullpointerexception only on my radiogroup where in dialog. even i have a similiar code on both of them. i have no idea why this happenned. i've read a question with similiar issue, but nothing solve my problem. here is my code :
btninvoice= (Button) findViewById(R.id.btninvoice);
btninvoice.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
final Dialog dialog3 = new Dialog(FormVisitMapping.this);
dialog3.setContentView(R.layout.penagihan);
dialog3.setTitle("Isi Data Penagihan:");
dialog3.show();
//other stuff
RadioGroup tes=(RadioGroup) findViewById(R.id.penagihan);
switch (tes.getCheckedRadioButtonId()) {
case R.id.rbtandaterima:
systemofpayment = "Tanda Terima";
break;
case R.id.rbtagihlangsung:
systemofpayment="Tagih Langsung";
break;
default:
break;
}
}
});
i've got NullPointerException on this line :
switch (tes.getCheckedRadioButtonId()) {
every help will be apriciated.thank you
Your using findViewById() on a wrong view. You haven't included your XML but I'm guessing that you want to find it in dialog's layout.
Change
RadioGroup tes=(RadioGroup) findViewById(R.id.penagihan);
to
RadioGroup tes=(RadioGroup) dialog3.findViewById(R.id.penagihan);

Categories

Resources