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();
Related
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>
I have an EditText contained in a LinearLayout. For My project I have set an OnclickListener on the LinearLayout. When I launch application nothing happened on click to layout. Maybe It due to the EditText but need that OnCliclistener to be work on LenearLayout.
MyCode:
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/lnlabelnameuniteholder"
android:id="#+id/choix_decategorie"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
CODE
maCategorie = (LinearLayout) findViewById(R.id.choix_decategorie);
maCategorie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
MyDB db = new MyDB(MyActivityCategorie.this);
ArrayList<Category> categoryList = db.getCategory();
ChoiceCategoryDialog categoryDialog = new ChoiceCategoryDialog(MyActivityCategorie.this, R.string.add_category, mCategoryTextView.getText().toString(), categoryList, MyActivityCategorie.this, true);
categoryDialog.show();
}
});
Thanks
In your code, you are referring to R.id.choix_decategorie (which is not shown in your code, is this a LinearLayout you want to set onClickListener on?
For a LinearLayout that has child elements (like yours does) - may need to prevent the child elements from receiving focus - you can set android:descendantFocusability="blocksDescendants" like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
For a good measure you may also set android:clickable="false" on each of the child elements. So your layout would look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/choix_decategorie"
android:id="#+id/lnpriceholderlabel"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="10dp" >
<EditText
android:text="#string/choix_de_categorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:clickable="false"
android:textSize="18sp"
android:textColor="#000" />
</LinearLayout>
Give it a try and let us know if this works.
Try this example:
LinearLayout yourLayout = (LinearLayout) findViewById (R.id.yourID);
yourLayout.setOnClickListener(new View.OnClickListener() { // remember to use "View."
#Override
public void onClick(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
}
});
set LinearLayout attribute android:clickable="true"
if you have button or textview in layout set android:clickable="false" for all of them
You must have to set the LinearLayout clickable="true"
<LineartLayout....
android:clickable="true"...>
This will work
And you need to put some margins in EditText, because EditText comes upside on LinearLayout and space doesn't remain for LinearLayout to be clickable.
put
<EditText
android:layout_margin="5dp".../>
your click listener will work but your edit text hide the linear layout , if you remove the edit text it will work or you can set height and width not equal to linear layout.
As a workaround you could do something like this (kotlin code):
editText.inputType = InputType.TYPE_NULL
editText.setOnClickListener { linearLayout.performClick() }
In that case the editText won't pop up the keyboard and the linearLayout will receive the a click event.
I have an activity with the following layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button ....
<EditText ....
//..........
I want to place a group of controls into the 2nd LinearLayout so that I'll be able to hide them all at once when I click the button. I suppose I should wrap them in one more LinearLayout and hide/show it when I click the button.
I've tried that and got an error saying basically it wasn't allowed. Did I do anything wrong with that? If not, what should I use instead?
As you need to hide/show your layout containing some views, on button click:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(ll.getVisibility() == View.VISIBLE)
{
ll.setVisibility(View.INVISIBLE); //use View.GONE, if required
}
else
{
ll.setVisibility(View.VISIBLE);
}
}
}
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();
}
}
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>