How can i check that the LinearLayout is clicked or not - android

This is my xml file and as you can see it is nested many times by Linear layout.
What i want to acheive is when I click on the area of llOptionA(First
Linear Layout) i will get notified by a toast.
I have also put a toast on llOptionA.setonclickListener()
But when i click on the text it does nothing.
then I also set onclicklisteners on each of them giving me different toasts -> svTest ,layout_inner ,tvOptionA. and also i clicked everywhere to see which part is showing which toast.
<LinearLayout
android:id="#+id/llOptionA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff00ff"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="5dp" >
<HorizontalScrollView
android:id="#+id/svTest"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layout_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvOptionA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="A - Option A "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

First a LinearLayout does not listen for click events by default even when you set an OnClickListener. You also need to add this attribute to your LinearLayout llOptionA:
android:clickable="true"
Second a click starts at the highest level, TextView tvOptionA, and works it's way down until a View consumes this event. So before it reaches llOptionA your HorizontalScrollView intercepts the click in it's default OnTouchListener and doesn't pass it down to your llOptionA... You can listener for a click event with an OnTouchListener in your HorizontalScrollView to call the appropriate method.
Third perhaps this is a simplified layout, but the LinearLayout layout_inner only has one child and therefor is not necessary, you can simply use this:
<LinearLayout ...>
<HorizontalScrollView ...>
<TextView .../>
</HorizontalScrollView>
</LinearLayout>
Of course llOptionA only has one child so you could simplify it more:
<HorizontalScrollView ...>
<TextView .../>
</HorizontalScrollView>
Addition from comments
Here is how to put it all together:
public class Example extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llOptionA);
linearLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toast();
}
});
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.svTest);
hsv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
toast();
return false;
}
});
}
public void toast() {
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
}
}

Related

Clicking a scroll view

I have a scroll view that has a Linearlayout child and this linearlayout has a drawingview.
the problem : I tried almost all the available answers on this topic and none of them worked, I cant understand why click is not fired for (scrollview, linear layout and even drawingview).
note : I tried setting on click for each of the three and none worked.
the xml code:
<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#id/scrollview"
android:clickable="true"
>
<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/linear"
android:clickable="false">
<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/draw"
android:clickable="false"/>
</LinearLayout>
</ScrollView>
First your ScrollView ,layout_height is wrapped! and for children you gave match_parent i doubt your view won't even display. Isn't it?
Then no idea why you use setorientation and android:id="#id/draw" instead of android:orientation and android:id="#+id
If you want to check scrolling works use
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
Log.d("TAG", "onScrollChanged: ");
// DO SOMETHING WITH THE SCROLL COORDINATES
}
});
You are using unnecessary attributes check this example :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8768"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollIndicatorDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#823">
<LinearLayout
android:id="#+id/first_child"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#900"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#987"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
If you write an onClickListner to the id linear_layout it just work normally!
Answer :The child Views of your ScrollView are consuming the click events you do on the ScrollView.
The solution to this problem is to set the onClickLIstener to the immediate child of the ScrollView(It's child).
So according to my example if i want to find out the ScrollView onClick i write my listener to its child.
scrolChild =(LinearLayout) findViewById(R.id.first_child) ;
scrolChild .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivityName.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
}
});
Read here when you need androidclickable-true android:clickable="true" mean's that it's not clickable?
This is my activity
public class MainActivity extends AppCompatActivity {
private ScrollView scrollView;
private LinearLayout scroolChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
scrollView = (ScrollView) findViewById(R.id.scrollIndicatorDown);
scroolChild =(LinearLayout) findViewById(R.id.first_child) ;
scroolChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
}
});
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
Log.d("TAG", "GotAScrollChanged: ");
// DO SOMETHING WITH THE SCROLL COORDINATES
}
});
}
}
You have set android:clickable="false" for both make it true it will work for you
<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#id/scrollview"
android:clickable="true"
>
<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/linear"
android:clickable="true">
<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#id/draw"
android:clickable="true"/>
</LinearLayout>
</ScrollView>

How to finish activity when tapping outside an ImageView

Is it possible to finish or dismiss activity when tapping outside a certain component like an ImageView or TextView ? And if it is ... how to do that ?
code of my layout
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
tools:ignore="PxUsage,UselessParent" >
<RelativeLayout
android:id="#+id/contents"
android:layout_width="720px"
android:layout_height="1280px"
android:layout_gravity="center"
android:clickable="true">
<ImageView
android:id="#+id/shortcutImageView"
android:layout_width="533px"
android:layout_height="727px"
android:layout_alignParentRight="true"
android:layout_marginRight="15px"
android:layout_marginTop="165px"
android:background="#drawable/shortcut_bg"
android:contentDescription="#string/imageDesc" />
</RelativeLayout>
</FrameLayout>
I want to finish activity whenever tapped outside of this ImageView
First code your xml like that:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
</LinearLayout>
And then in your activity:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent ev)
{
finish();
return false;
}
});
Hope this work for you...
Simply attach onClickListener to root of your layout (most likely you got there LinearLayout or so) and then do regular finish() when that element gets tapped.
You have to set RelativeLayout attribute android:clickable="true" in the xml layout file OR setClickable(true) in the java activity code. Simply call finish(); to finish the current activity in onClickListener.
If you happen to be using a FragmentActivity or another subclass of the Activity class you can set it to finish itself when touched outside the window's bounds by calling setFinishOnTouchOutside when creating the activity
setFinishOnTouchOutside(true);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.contents);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
}
});
this.initContent();

How to let click event pass to container in android?

I have a table of FrameLayout where each frame contains either an ImageView or a TextView. Regardless of content in the frame I want the onClick event to be detected by the OnClickListener set on the frame.
How can I achieve this?
This is some of my layout, I got a total of 5 rows (Here's only shown 1).
As described above I have 5 FrameLayouts in each row, each containing a TextView. I am not able to put my OnClickListener on the TextView since this may be changed to an ImageView at runtime. Therefore i want the OnClickListener on the FrameLayout.
It seems that the content of the FrameLayout is preventing it from detecting the click event.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gateContainer"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door1"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door2" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView5"
android:layout_gravity="center" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/door3" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView4"
android:layout_gravity="center"/>
</FrameLayout>
</TableLayout>
Here is an example of how i set the OnClickListeners:
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v){
// do something
}
};
TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer);
// Go through all TableRows
for (int i = 0; i < tableLayout.getChildCount(); i++){
TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
// Set listener for each FrameView
for (int j = 0; j < tableRow.getChildCount(); j++){
FrameLayout frame = (FrameLayout) tableRow.getChildAt(j);
frame.setOnClickListener(clickListener);
}
}
You could also set android:clickable="false" to the TextView/ImageView layouts and android:clickable="true" on the background, that way the background view always catches the clicks.
Additionally, the following answer might be helpful for people considering this issue:
SO: Android: How to propagate click event to LinearLayout childs and change their drawable
Just implement, OnTouchListener over ImageView and TextView components and duck them, in the sense pass them.
<your_view>.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
});
this will make sure, your container handles that.
You can define the click event on your TextView or ImageView as below:
<yourView>.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic here.
}
});

Android RelativeLayout onclicklistener doesn't get fired

This is my XML layout - it's an info window.
i want that when the user clicks the #+id/fragment_info_relativelayout_content layout - the info window will be closed.
This is my XML:
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_content"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#drawable/background_info_window"
android:clickable="true" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_margin="5dp" >
<!-- The info textView -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_info"
style="#style/text_shadow_black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
android:text="Some Text "
android:textColor="#android:color/white"
app:isBold="true" />
</ScrollView>
<!-- Tap to close -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_tap_to_close"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingRight="5dp"
android:text="#string/button_tap_to_close"
android:textColor="#android:color/white"
app:isBold="true" />
</RelativeLayout>
This is my code:
public InfoWindow(ViewGroup rootView)
{
this.container = rootView;
infoWindowView = LayoutInflater.from(rootView.getContext()).inflate(INFO_WINDOW_LAYOUT_ID, null);
//find info window's view
contentLayout = infoWindowView.findViewById(R.id.fragment_info_relativelayout_content);
contentLayout.setClickable(true);
contentLayout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//close the info window
close();
}
my problem is that the onClick method isn't being fired on every click on the layout...
it's weird since it does fire after clicking some clicks, or click at the bottom right on the textview...
any ideas ??
My suggestion, is that the ScrollView in the RelativeLayout handles all touch events, and don't let the RelativeLayout handle any touch events.
In this case, you should extend the ScrollView and make the onTouchEvent return false
#Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return false;
}
And the same to onInterceptTouchEvent
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
return false;
}
Just try it out and tell me what you got =)

Button onClick not detected with a ListView and ListActivity

I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>

Categories

Resources