I am trying to add an onClick on the soft keyboard for an activity. The reason why is that i want to check if the user is currently active. So what i have done is that if the user clicks on the app i will reset a inactivity timer. The problem is that when a user interacts with the soft keyboard it doesn't call the function onUserInteraction() which is a function I override in the activity. So i need help to find a way to keep track if the soft keyboard has been clicked for every textfield etc I have in the activity. (I know that i can insert a onclick listerner on every EditText field but i rather not do that, because if I would use many EditText fields it would not be so nice)
So this is what i ended up with. I was hoping for something else, but this solves the problem. Thanks for the help!
public class ActivityEditText extends android.support.v7.widget.AppCompatEditText {
private TextWatcher tw;
public ActivityEditText(Context c)
{
super(c);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOurTCL();
}
private void setOurTCL()
{
this.tw = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
InactivityManager.resetTime();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
this.addTextChangedListener(this.tw);
}
#Override
public void removeTextChangedListener(TextWatcher watcher) {
if(!watcher.equals(this.tw))
super.removeTextChangedListener(watcher);
}
}
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
Create a class some thing like UserInteractionEditText and extend EditText and set the onclick lisetener in that class use that class in all the XML layouts as you use EditText you can do some thing like this:
public class UserInteractionEditText extends EditText implements View.OnClickListener {
public UserInteractionEditText(Context context) {
super(context);
}
public UserInteractionEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UserInteractionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onClick(View v) {
//TODO:: Handle user Click Events
}
}
You can use onUserInteraction() function of activity. You need to override this function in your activity.
This function get calls when you perform any kind of interaction with your activity.
#Override
public void onUserInteraction() {
super.onUserInteraction();
// Your code goes here
}
You can refer this example and also this answer, refer the docs here
Hope this helps.
Related
This question already has answers here:
Android set height and width of Custom view programmatically
(10 answers)
Closed 2 years ago.
I am trying to resize a textbox dynamically using Java code. I want the width to not use wrap content but using a static number in dp. I want this to be done in Java code instead of the XML file. I want it like this is because I want to apply it to each item in the recyclerview. It needs to work with multiple screens sizes. It will work like a min Length for a textfield box size. If you know how to do this would be much appericaiated.
this is called auto sizing and it can be done by adding TextChangedListener which is a listener for Edit Tex. this listener watch the changes of editText and it has three different states. also you can create a component(custom view) and extend it from AppCompatTextView name as you want; in its initialization you can add below code:
public class CustomTextView extends AppCompatTextView {
Context ctx;
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
init();
}
public CustomTextView(Context context) {
super(context);
ctx = context;
init();
}
public void init() {
setOnTouchListener(null);
addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getText().toString().length() > 10){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall);
}
else if (getText().toString().length() > 5){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
also check these out, there is a tone of documentation for it:
Autosizing TextView Tutorial for Android
Autosizing TextViews
Similar to this post. Have a custom view (extends EditText) which must have the ability to call the finish() method of the parent activity if the user presses the END key.
How can I access the activity object of the host activity in order to call its finish() method from within the custom view class?
public class SuppressInputEditText extends androidx.appcompat.widget.AppCompatEditText {
public SuppressInputEditText(Context context) {
super(context);
}
public SuppressInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SuppressInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode){
case 6: //end key
//todo: call finish() method of parent activity.
break;
}
return true;
}
}
I can use the getContext() method of my class, available because it inherits from view, to get a context, but I don't know how to use that to access the finish() method. Any help or pointers would be greatly appreciated.
UPDATE:
Looking for a solution that can keep the class independent. Thanks!
if you know the host i.e the activity where it's custom view is showing then you can do something like this.
(getContext() as? MainActivity)?.finish()
java
((MainActivity)getContext()).finish()
place this under try and catch
Edit: Create an interface which your Host activity implements and pass this as a listener yo your custom view then whenever needed to call this.
for ex.
interface CustomInputEditListener{
public void onFinish();
}
in your Host activity implement this.
MainActivity extends AppCompatActivity() implements CustomInputEditListener{
//call this from onCreate()
public void setHostListener(){
suppressInputEditText.setHostEditListener(this);
}
#Override public void onFinish(){
finish() ;
}
}
in your SuppressInputEditText class create a method like this.
public void setHostEditListener(CustomInputEditListener listener){
this.hostListener = listener;
}
and whenever you need to call finish just call
hostListener.onFinish();
Cast context to Activity then call finish like below
Kotlin
(context as? Activity)?.finish()
Java
((Activity) context).finish()
I initially set background of button with this code at onCreateView.
uc.setBackgroundResource(R.drawable.saat_button_none);
If I initially set background or textColor of button I want to prevent style change when I use onClick
public void onClick(View v) {
switch (v.getId()) {
case R.id.bir:
uc.setBackgroundResource(R.drawable.saat_button); //Should not work
dort.setBackgroundResource(R.drawable.saat_button_sel);
bes.setBackgroundResource(R.drawable.saat_button_sel);
}
}
Is that possible?
Edit: I don't want to use if statement since I have lots of buttons I just want to lock style of button.
To do this, create a custom view simply by extending View and override all methods related to background and put your logic their if background has changed once then overridden method should throw exception saying that you can't change the style as it has been changed while setup the default look and feel.
public class CustomButton extends Button {
boolean backgroundChanged = true;
public CustomButton(Context context) {
this(context, null);
}
public CustomButton(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setBackgroundResource(int resid) {
if(backgroundChanged){
throw new RuntimeException("you can't change the style as it has been changed while setup the default look and feel");
}
super.setBackgroundResource(resid);
}
}
At last in layout file replace <Button tag with <CustomButton
I needed to correlate checkboxes with their position in the list view in the checkboxes' onClickListener. My first solution was to make a very short custom view that extended checkbox but had an extra variable (position) that would be set in getView().
public class ListCheckBox extends CheckBox {
private int position = -1;
public ListCheckBox(Context context)
{super(context);}
public ListCheckBox(Context context, AttributeSet attrs)
{super(context,attrs);}
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
{super(context,attrs,defStyleAttr);}
#TargetApi(21)
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{super(context,attrs,defStyleAttr,defStyleRes);}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
It worked, except it changed the checkboxes' color to black, and nothing I did (including changing android:buttonTint) could change it. For now my solution is a HashMap with a view key and integer value that keeps track of the checkboxes' and their positions, but if anyone has a less ugly solution or any idea as to why I couldn't change the color of the checkboxes, it would be very much appreciated.
You can use the method of setTag() to past a extra variable to a view,then you can use getTag() to get the extra variable. This does not need to custom CheckBox.
//set tag
checkBox.setTag(position);
//get tag
int position = (int)checkBox.getTag();
As title says CordovaWebView and onBackPressed in android in combination are giving weird results.
I have hybrid app. My main activity has DrawerLayout and CordovaWebView.
My onBackPressed:
#Override
public void onBackPressed(){
if(drawerIsOpen){
//close drawer
}else if(webviewIsIn){
//hide webview
}else{
super.onBackPressed();
}
}
When I use android's WebView the overridden method is called as expected. And when I change to CordovaWebView the method wouldn't even get called, instead native onBackPressed would be called instead.
I have tried overriding onKeyDown and onKeyUp but it gives me the same result, the methods are just not being called.
I'm using Cordova 2.9.0 and testing device is Galaxy Note 2, Android jellybean 4.2.2
DrawerLayout has the close on back pressed functionality I've just disabled it.
I hope you guys can understand the problem.
I encountered the same issue. My solution was to derive from CordovaWebView and override public boolean onKeyUp(int keyCode, KeyEvent event) with something like this (for Cordova 3.4.0, the code is a part of the CordovaWebView.onKeyUp(int, KeyEvent)):
public class CustomCordovaWebView extends CordovaWebView {
protected View mCustomView;
protected boolean bound;
public CustomCordovaWebView(final Context context) {
super(context);
}
public CustomCordovaWebView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public CustomCordovaWebView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
#TargetApi(11)
public CustomCordovaWebView(final Context context, final AttributeSet attrs, final int defStyle, final boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
// A custom view is currently displayed (e.g. playing a video)
if (mCustomView!=null){
this.hideCustomView();
}else{
// The webview is currently displayed
// If back key is bound, then send event to JavaScript
if (this.bound) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
} else {
// If not bound
// Go to previous page in webview if it is possible to go back
if (this.backHistory()) {
return true;
}
// If not, then invoke default behavior
else {
//this.activityState = ACTIVITY_EXITING;
//return false;
// If they hit back button when app is initializing, app should exit instead of hang until initialization (CB2-458)
// this.cordova.getActivity().finish();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this thing is closing your activity in CordovaWebView
}
}
}
} else {
return super.onKeyUp(keyCode, event);
}
return false;
}
#Override
public void hideCustomView() {
mCustomView = null;
super.hideCustomView();
}
#Override
public void showCustomView(final View view, final WebChromeClient.CustomViewCallback callback) {
mCustomView = view;
super.showCustomView(view, callback);
}
#Override
public void bindButton(final boolean override) {
bound = override;
super.bindButton(override);
}
}
If there is a better solution, I would be interested in it.