I've got a ListView with several rows. Every row consists of 7 TextViews which react to onClick() events.
This all works perfectly fine but when the user clicks on the margins of the row, where no TextView catches the onClick() event, the root view - a LinearLayout - get's highlighted.
This is normal behaviour of course but as nothing happens by clicking there I don't want the Linear Layout to be highlighted.
Is there any way of disabling this behaviour but keep catching the onClick() events on the TextViews?
(The onClick listener is set inside the adapters getView() method)
Here some extract of the xml file. As one can see I've tried some things but they don't work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:longClickable="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="#+id/listelement_weekoverview_tv_mo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#drawable/weeklylist_rndrectangle"
android:gravity="center_horizontal"
android:singleLine="false"
android:textColor="#color/appcolor_red_base" />
...
</LinearLayout>
unclicked version
clicked version
I've found a solution.
Simply overwrite the isEnabled (int position) method in your custom Adapter like this:
#Override
public boolean isEnabled (int position) {
return false;
}
I need a way of turning a TableRow into a Button in android. I have tried to set up an onCLickListener() and I have tried nesting a TableRow inside a Button but that just crashes the app.
Edit:
I deleted the android:onCLick="onClick" like you said and that got rid of the crashing but nothing happens when I click the table row.
My code:
tableRow1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openInfoTR1 = new Intent("android.intent.action.MENU");
fromTableRow = 1;
startActivity(openInfoTR1);
System.out.println("Confirmed click");
}
});
<TableLayout
android:id="#+id/tlDisplayTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/trTableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:visibility="invisible"
android:clickable="true">
<TextView
android:id="#+id/tvDisplayedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="5"/>
</TableRow>
</TableLayout>
Every element that inherits from View can have attached an OnClickListener. No need to wrap it inside a button.
However, you'll have to look at how events are propagated through your layout. E.g. if you have clickable elements within your TableRow, the click events will normally be consumed by that elements and will not reach your OnClickListener. There are different ways to intercept or modify that behaviour, but you'd have to post your code to get more specific help.
EDIT:
The exception in your app comes from the line android:onClick="onClick" in your layout file. As you set the onClick listener programmatically, you do not need this. android:onClick="onClick" is a shortcut that would expect a method void onClick(View view) directly within your Activity (not, as you have it, as part of the OnClickListener implementation).
I have a RelativeLayout inside of a ScrollView that contains a Button and some TextViews and EditTexts.
In my xml layout file, I am defining android:onClick but it always takes two clicks of the button to fire the event. The button always gets the focus on the first click and fires the onClick event on the second click. I have tried setting focusable and focusableInTouchMode both to false but the behavior doesn't change.
Here is my layout file:
<ScrollView 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:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".DensityActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
...
<TextView
...
<TextView
...
<EditText
...
<Button
android:id="#+id/ad_button_calculate"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_below="#id/ad_edit_obs_temp"
android:layout_alignParentRight="true"
android:layout_marginTop="20pt"
android:paddingLeft="6pt"
android:onClick="onClick"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/button_calculate" />
<TextView
...
</RelativeLayout>
</ScrollView>
Any ideas or suggestions as to why the focusable and focusableInTouchMode don't seem to do anything?
I thought it might be my onClick() method not doing what it should so I reduced it to something simple just to see and it behaves the same. Here is my simplified onClick():
public void onClick(View view) {
new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();
}
OK, I found it. It was, of course, my own mistake.
At the end of my onCreate method I was doing this:
// Set the focus to the calculate button so the keyboard won't show up automatically
Button calcButton = (Button)findViewById( R.id.ac_button_calculate );
calcButton.setFocusable( true );
calcButton.setFocusableInTouchMode( true );
calcButton.requestFocus();
So of course, no matter what I did in my xml file, I was overriding it in my code.
Instead, I used this to hide the keyboard:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN );
Which works great.
I am working on an app in which when user click on a button then a new activity layout
will be opened.This new layout contains various views.
One of them is zip-code EditText .I want that this zip-code EditText automatically fetch the zip-code of user through GPS and fill it in.For this I want to work like this:
User click on a button.
A new layout loads.
All views load perfectly and in front of zip-code EditText a loading button will be shown
and it shows automatically by GPS.
How do I work on 3rd point i.e, showing loading until user zip code not fetched.
Consider the following pieces of code as an example. You will have to fill in some blanks though.
This is how I show a ProgressBar in one of my app's Activities. It has a ListView, which you will have to replace with your own content.
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbHeaderProgress"
style="#style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center"
android:padding="2dp"
android:text="Loading...."
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling"
android:scrollbars="none" >
</ListView>
Now, in your Java code where you map this layout to your Activity, cast the LinearLayout linlaHeaderProgress. Please note that in the XML itself, the android:visibility="gone" attribute is set to "gone".
If you use an AsyncTask to fetch the ZIP code, then while the process happens in the doInBackground(), show the ProgressBar by toggling the visibility of the linlaHeaderProgress in the onPreExecute()like this:
#Override
protected void onPreExecute() {
// SHOW THE PROGRESS BAR (SPINNER) WHILE FETCHING POST DETAILS
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
And in the onPostExecute(), again toggle the visibility like this:
#Override
protected void onPostExecute(Void result) {
// HIDE THE PROGRESS BAR (SPINNER) AFTER FETCHING THE POST DETAILS
linlaHeaderProgress.setVisibility(View.GONE);
}
If you are not using an AsyncTask, but are instead doing the processing in a Method, use this at the start of the method: linlaHeaderProgress.setVisibility(View.VISIBLE);. And use this at the end of the method once you have displayed the ZIP code in the EditText: linlaHeaderProgress.setVisibility(View.GONE);
I declare the LinearLayout linlaHeaderProgress globally so I can reuse it wherever required. Let me know if this helps, or if you have any further questions for me.
This question already has answers here:
How to stop EditText from gaining focus when an activity starts in Android?
(54 answers)
Closed 7 years ago.
I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?
You can make cursor and focus disappear by
edittext.clearFocus();
But detect when the key board hide is a hard work.
You can add this to onCreate and it will hide the keyboard every time the Activity starts.
You can also programmatically change the focus to another item.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Add LinearLayout before EditText in your XML.
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
Or you can do this same thing by adding these lines to view before your 'EditText'.
<Button
android:id="#+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="#+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="#color/blue"
android:textSize="13sp"
android:textStyle="bold" />
Remove focus but remain focusable:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText will lose focus, but can gain it again on a new touch event.
Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)
android:focusable="true"
android:focusableInTouchMode="true"
It will do the trick :)
remove autofocus edittext android
It's working for me
Edit In the link they suggest to use LinearLayout, but simple View will work
<View
android:id="#+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
Then if this "thief" is placed at the top of the layout (to be first focusable item) calls to clearFocus() will work.
You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.
This is equivalent to :
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
but in XML way.
FYI, you can also hide the keyboard with codes:
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
To hide the keyboard when activity starts.. write the following code in onCreate()..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
To clear focus and remove cursor from edittext.....
editText.clearFocus();
editText.setCursorVisible(false);
try to use this one on your view
it worked for me:
<View
android:id="#+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
Add to your parent layout where did you put your EditText this android:focusableInTouchMode="true"
you have to remove <requestFocus/>
if you don't use it and still the same problem
user LinearLayout as a parent and set
android:focusable="true"
android:focusableInTouchMode="true"
Hope it's help you.
This is my very first answer on SO, so don't be too harsh on me if there are mistakes. :D
There are few answers floating around the SO, but I feel the urge to post my complete solution cause this drove me nuts. I've grabbed bits and pieces from all around so forgive me if I don't give respective credits to everyone... :)
(I'll simplify my result cause my view has too many elements and I don't wanna spam with that and will try to make it as generic as possible...)
For your layout you need a parent your EditText and parent view defined something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lytContainer"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etEditor"
android:inputType="number"
android:layout_gravity="center"
android:hint="#string/enter_your_text"
android:textColor="#android:color/darker_gray"
android:textSize="12dp"
android:textAlignment="center"
android:gravity="center"
android:clickable="true"/>
</LinearLayout>
So, I needed a few things here. I needed to have a Placeholder for my EditText - which is that -
android:hint="hint"
Also,
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
made it happen for EditText not to be focused on entering the Activity and later on in the Activity itself when setting it this setting helps so you can set onTouchListener on it to steal the focus away from EditText.
Now, in the Activity:
package com.at.keyboardhide;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.keyboardmain);
getEditText = (EditText)findViewById(R.id.etEditor);
getLinearLayout = (LinearLayout)findViewById(R.id.lytContainer);
getLinearLayout.setOnTouchListener(this);
getEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.d("EDTA", "text was entered.");
getEditText.clearFocus();
imm.hideSoftInputFromWindow(barcodeNo.getWindowToken(), 0);
return true;
}
return false;
}
});
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(v==getLinearLayout){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getEditText.getWindowToken(), 0);
getEditText.clearFocus();
return true;
}
return false;
}
}
Few of the answers for bits I found on this question page, and the part with the Activity solution I found on this blog. The rest I missed which I had to figure out myself was clearing focus on the EditText which I added to both inside the setOnEditorActionListener and onTouchLister for the parent view.
Hope this helps someone and saves their time. :)
Cheers,
Z.
In the comments you asked if another view can be focused instead of the EditText. Yes it can. Use .requestFocus() method for the view you want to be focused at the beginning (in onCreate() method)
Also focusing other view will cut out some amount of code. (code for hiding the keyboard for example)
I had the same problem. It made me more than crazy.
I had an extended Dialog with a ScrollView that had a TableLayout with extended LinearLayout that contained a SeekBar and a EditText.
The first EditText had always autofocus after showing the Dialog and after finishing editing the text over the keyboard the EditText still had the focus and the keyboard was still visible.
I tried nearly all solutions of this thread and none worked for me.
So here my simple solution: (text = EditText)
text.setOnEditorActionListener( new OnEditorActionListener( ){
public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
(actionId == EditorInfo.IME_ACTION_DONE) ){
text.clearFocus( );
InputMethodManager iMgr = null;
iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
}
return true;
}
});
By the way I didn't used any of the following snippets to solve it:
//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )
AND I didn't used a spacer item like a View with width and height of 1dp.
Hopefully it helps someone :D
editText.setFocusableInTouchMode(true)
The EditText will be able to get the focus when the user touch it.
When the main layout (activity, dialog, etc.) becomes visible the EditText doesn't automatically get the focus even though it is the first view in the layout.
You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.
This is equivalent to:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You can avoid any focus on your elements by setting the attribute android:descendantFocusability of the parent element.
Here is an example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
Here, the attribute android:descendantFocusability set to "blocksDescendants" is blocking the focus on the child elements.
You can find more info here.
check your xml file
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" >
**<requestFocus />**
</EditText>
//Remove **<requestFocus />** from xml