why parent view and child view are receiving onclickevent - android

I have a parent view LinearLayout i have added a child view TextView, But whenever i click on childview why parentview is also clicked. What i want is to differentiate whether only parent has been clicked and only childview is clicked.
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});

then try to implement View.OnClickListener in yout Activity first, then try to override
#Override
public void onClick(View v){
if(v.getId().equals(R.id.linearLayout1) {
//do ur stuffs
} else {
//do your stuffs
}
Hope it will help you. :)

In android, ViewGroups (i.e. all layouts by inheritance) use the property (and corresponding xml attribute ) : addStatesFromChildren
If set, the parent will just be set in the very same state as one of his children view.

try this code...
LinearLayout rlmain = (LinearLayout)findViewById(R.id.linearLayout1);
rlmain.setOnClickListener(this);
TextView tv = (TextView )findViewById(R.id.TextViewLayout1);
tv.setOnClickListener(this)
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.TextViewLayout1:
//do ur stuffs
break;
case R.id.linearLayout1:
//do ur stuffs
break;
}
}

Related

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
}
}
});

How to know which child view is clicked in a relativelayout

I have a relative layout to which child views are added and removed dynamically(any number can be added or removed)
My question is how to know which view was clicked so that i can add different onclicklisteners depending on the type of child views
Adding and retrieving the tag while click event can help. Here is the code.
For adding tags:
customView1.setTag(someTag);
customView1.setOnClickListener(myClickListner);
For retrieiving:
OnClickListener myClickListener = new onClickListener(){
#Override
public void onClick(View v) {
if(v.getTag() == someTag){
//do stuff
}else if(v.getTag() == otherTag){
//do something else
}
}
in your adapter class you need to write like this am sharing the sample code snippet
public static class ChatListItemsViewHolder extends
RecyclerView.ViewHolder {
public ChatListItemsViewHolder(View v) {
super(v);
// TODO Auto-generated constructor stub
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// write your code here
}
});
}
let me know if you need more clarity.

How to add onclicklistener to dynamically generated text views

I have dynamically created Textviews in my application. I want to give On click event to this textviews.. When I click on the textview, I need to get the id of the textview..
You try following code.
TextView text = new TextView(this);
text.setText("text here");
ll.addView(text);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "ID : "+arg0.getId(), Toast.LENGTH_SHORT).show();
}
});
in that "ll" is Layout which add textview and after adding put clickListener() for click event.
hope this is useful for you.
Have a look at setOnClickListener and getId.
TextView textView = new TextView(this);
// Set up your text view
textView.setId(1); // Any number is ok
textView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
int id = v.getId(); // This is the id you want
// Do whatever you want here
}
});
If you are creating TextViews dynamically than just implement OnClickListener:
import android.view.View;
import android.view.View.OnClickListener;
public class MyClickListener implements OnClickListener {
#Override
public void onClick(View v) {
int vId = v.getId();
}
}
and set it to dynamically created view like this:
private TextView createTextView(int vId) {
TextView textView = new TextView(this);
textView.setId(vId);
textView.setOnClickListener(myClickListener);
return textView;
}
P.S.: Don't forget to create & initilialize myClickListener in your Activity.

Android is there ClickListener for linearLayout using addView?

I'm trying to create pinterest like layout. I find a way to achieve this: Android heterogeneous gridview like pinterest?!
However the problem is: I want to view item details after clicking each picture. But as I am using LinearLayout.addView() to add all the ImageViews, I'm not sure how I can get it clickable?
Is there anyway to be able to click each item on the view?
You can do this pretty easily by adding tag information to your image view that can be displayed when clicked.
Adding your image view would look like:
ImageView iv = new ImageView(context);
iv.setOnClickListener(your_listener);
iv.setTag("Item information");
linearLayout.addView(iv);
Then in your click listener:
public void onClick(View v) {
if(v instanceof ImageView) {
String info = (String)v.getTag();
/* Show information here */
}
}
Use this :
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ADD your action here
}
});
or make your class implement the OnClickListner Interface and override the onClick() method
Use:
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do magic
}
});
And in your layout file mark the ImageView as clickable:
<ImageView
...
android:clickable="true">
...
</ImageView>
Building on the code that you linked, you can add a listener to each image view as you create it:
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);
for(int i=0;i<n;i++)
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.id.icon);
iv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Your click code
}
}
int j = count % 3; <----
if(j==0)
linear1.addView(iv);
else if(j==1)
linear2.addView(iv);
else
linear3.addView(iv);
}

how to make a click event on a textview of child of a custom listview in android

Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};

Categories

Resources