I'm trying to create a custom EditText that provides an onLostFocus event. However, I can't get my head around how I tell the custom class what method to run when the focus is lost.
This is my extended EditText:
public class smtyEditText extends EditText {
public smtyEditText(Context context) {
super(context);
}
public smtyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public smtyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setFocusChangeListener() {
this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// notify the relevant activity, probably passing it some parameters like what instance of smtyEditText triggered the event.
}
}
});
}
}
The intention of the setFocusChangeListener function was that from any given activity I could do something like:
public class AddMeal extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_meal);
EditText etMealName = (EditText) findViewById(R.id.txtmealName);
etMealName.setFocusChangeListener(this.fieldLostFocus)
}
// then
public void fieldLostFocus(eventSource) {
// run some kind of validation on the field text.
}
}
Clearly I'm "code paraphrasing" here. I also get that Interfaces, and some other "EventNotifier" class might be needed. These are the resources I've tried to decipher so far:
http://www.javaworld.com/article/2077462/learn-java/java-tip-10--implement-callback-routines-in-java.html
How to Define Callbacks in Android?
http://www.justinmccandless.com/blog/Setting+Up+a+Callback+Function+in+Android
But for whatever reason I can't crystallize what is needed. Do you have any suggestions on how I can achieve this?
You don't need the inheritance... it only adds an unnecessary layer of indirection. Just add the focus change handler in your activity.
public class AddMeal extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_meal);
EditText etMealName = (EditText) findViewById(R.id.txtmealName);
etMealName.setFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// call method to handle loss of focus
}
}
});
}
// then
public void fieldLostFocus(eventSource) {
// run some kind of validation on the field text.
}
}
Related
I need to create custom view - TextView and Switch button.
I have custom view:
public class CustomTextWithSwitch extends LinearLayout implements View.OnClickListener {
private CustomTextWithSwitchBinding binding;
private boolean defaultValue;
public CustomTextWithSwitch(Context context) {
this(context, null);
}
public CustomTextWithSwitch(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextWithSwitch(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
binding = CustomTextWithSwitchBinding.inflate(LayoutInflater.from(getContext()), this);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextWithSwitch);
defaultValue = a.getBoolean(R.styleable.CustomTextWithSwitch_defaultValue, false);
a.recycle();
}
#Override
protected void onFinishInflate() {
...
binding.sToggle.setChecked(defaultValue);
super.setOnClickListener(this);
super.onFinishInflate();
}
public void toggle() {
binding.sToggle.toggle();
defaultValue = binding.sToggle.isChecked();
}
#Override
public void onClick(View view) {
toggle();
}
public void setDefaultValue(boolean defaultValue) {
this.defaultValue = defaultValue;
binding.sToggle.setChecked(defaultValue);
}
}
I use that in the activity:
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
myCustomView.setDefaultValue(true);
}
}
When I open this activity, everything works as expected - switch button is checked. However, when I rotate the screen, it is checked to false. Can somebody advise what am I doing incorrectly? Note: I use ViewModel and the value is restored correctly. However, the Switch button in this custom view is not toggled.
UPDATE: This issue happens only when I have another CustomTextWithSwich in my activity, so it means they have effect on each other(it seems that default value is set according to second View). Is it possible to separate them, so they are standalone?
I suggest you to use a ViewModdel. During rotation it is normal that you will lost the states. ViewModel will not be destroyed during app rotation so the last state of CustomTextWithSwich will be not lost.
On the other hand you can override onConfigurationChanged method every time app is rotated this method will run, so you can use it to set your data.
#Override
public void onConfigurationChanged(#NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Please check whether both of your CustomTextWithSwitch and its children on Activity has android:ids. Ids should be different. In this case Android take care about saving instance state for your view.
If it does not save your state, use this code inside your CustomTextWithSwitch
#Override
public Parcelable onSaveInstanceState()
{
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
bundle.putBoolean("isChecked", defaultValue); // ... save check state
return bundle;
}
#Override
public void onRestoreInstanceState(Parcelable state)
{
if (state instanceof Bundle)
{
Bundle bundle = (Bundle) state;
defaultValue = bundle.getBoolean("isChecked"); // ... load state
binding.sToggle.setChecked(defaultValue);
state = bundle.getParcelable("superState");
}
super.onRestoreInstanceState(state);
}
Have faced the same issue. I fixed it by setting setSaveEnabled(false) on the switch view. Just let the viewModel restore it's state.
public CustomTextWithSwitch(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
binding = CustomTextWithSwitchBinding.inflate(LayoutInflater.from(getContext()), this);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextWithSwitch);
defaultValue = a.getBoolean(R.styleable.CustomTextWithSwitch_defaultValue, false);
a.recycle();
//Add this (assuming your switch view id is 'switch_view')
binding.switchView.setSaveEnabled(false);
}
I have an EditText what I populate via
editText.setText(content)
The reason this is an EditText and not a TextView is because I also want to paste stuff (later when user is operating the app) in it or manually type in it, if applicable.
But I have to
reset a flag if editText set via setText()
and nothing if pasted by user
How can I distinguish how a EditText was populated? addTextChangedListener()'s callbacks are triggered in each case.
You can set Listener Class:
public interface GoEditTextListener {
void onUpdate();
}
Custom Edittext
public class GoEditText extends EditText
{
ArrayList<GoEditTextListener> listeners;
public GoEditText(Context context)
{
super(context);
listeners = new ArrayList<>();
}
public GoEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
listeners = new ArrayList<>();
}
public GoEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
listeners = new ArrayList<>();
}
public void addListener(GoEditTextListener listener) {
try {
listeners.add(listener);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/**
* Here you can catch paste, copy and cut events
*/
#Override
public boolean onTextContextMenuItem(int id) {
boolean consumed = super.onTextContextMenuItem(id);
switch (id){
case android.R.id.cut:
onTextCut();
break;
case android.R.id.paste:
onTextPaste();
break;
case android.R.id.copy:
onTextCopy();
}
return consumed;
}
public void onTextCut(){
}
public void onTextCopy(){
}
/**
* adding listener for Paste for example
*/
public void onTextPaste(){
for (GoEditTextListener listener : listeners) {
listener.onUpdate();
}
}
}
xml
<com.yourname.project.GoEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/editText1"/>
Code
private GoEditText editText1;
editText1 = (GoEditText) findViewById(R.id.editText1);
editText1.addListener(new GoEditTextListener() {
#Override
public void onUpdate() {
//here do what you want when text Pasted
}
});
Simply extend EditText, include the flag, and override setText:
public class MyEditText extends EditText {
boolean fromSetText;
#Override
public void setText(String text) {
super.setText(text);
fromSetText = true;
}
}
You can define your own setters/getters and constructors based on your requirements.
i would like to create a class, which extends Button and implement a method, which is alwasy called, when the Button is clicked. But i still want it's OnClickListener to be called.
My Idea is to save the OnClickListener into a private member when the constructor or setOnClickListener is called and then set the OnClickListener to my own OnClickListener. This one would then call my method and the saved OnClickListener.
But i don't see how i can get the OnClickListenr, i only see, how to set it.
Is there a way to acces it?
Or do you have a better idea? (it doesn't matter wheter my method is called before or after the OnClickListener)
I guess you could do this:
public class OnceClickedTwiceRunButton extends Button{
public OnceClickedTwiceRunButton(Context context) {
super(context);
}
public OnceClickedTwiceRunButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OnceClickedTwiceRunButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private OnClickListener extraClickMethod;
#Override
public void setOnClickListener(OnClickListener newListener)
{
super.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
DefaultClickMethod(v);
if(extraClickMethod != null)
{
extraClickMethod.onClick(v);
}
}
});
extraClickMethod = newListener;
}
private void DefaultClickMethod(View v)
{
//TODO
}
}
I would like to create my own OnTouchListener. Then I would like to encapsulate it to a .jar file for making it reusable.
This is my specific OnTouchListener:
public class TouchableView extends View implements OnTouchListener{
myTouch t=null;
public TouchableView(Context context) {
super(context);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public TouchableView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public TouchableView(Context context, AttributeSet attrs) {
super(context, attrs);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public void setmyTouch(myTouch listener) {
t = listener;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
t.downTouch();
return true;
}
return false;
}
public interface myTouch{
public abstract boolean downTouch();
}
}
This is how I'm trying to use it:
public class MyTouchImplement extends Activity implements myTouch{
/** Called when the activity is first created. */
TextView tv;
int i=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.tv);
TouchableView view = (TouchableView) findViewById(R.id.view);
view.setmyTouch(this);
}
#Override
public boolean downTouch() {
i++;
tv.setText(i+"");
return true;
}
}
I would like to make it work for every component that the OnTouchListener works with.
The following works for me. Please check and see if this helps. Please feel free to modify the constructor to suit your needs. For this test I used a linear layout with two TextView (txtX, txtY) fields and one GridLayout control.
MineSweeperOnTouch.java
public class MineSweeperOnTouch implements View.OnTouchListener {
private View gridLayout = null;
private TextView txtX = null;
private TextView txtY = null;
public MineSweeperOnTouch(View aGridLayout, TextView aTxtX, TextView aTxtY) {
this.gridLayout = aGridLayout;
this.txtX = aTxtX;
this.txtY = aTxtY;
}
public boolean onTouch(View v, MotionEvent event) {
txtTimeX.setText("X: " + String.valueOf(event.getX()));
txtY.setText("Y: " + String.valueOf(event.getY()));
return true;
}
}
MainActivity.java (code snippet only)
-------------------------------------
public class MainActivity extends Activity {
private MineSweeperOnTouch gridLayoutListener = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//custom code starts here
final View gridLayout = findViewById(R.id.gridLayout);
final TextView txtX = (TextView) findViewById(R.id.txtX);
final TextView txtY = (TextView) findViewById(R.id.txtY);
gridLayoutListener = new MineSweeperOnTouch(gridLayout, txtX, txtY);
gridLayout.setOnTouchListener(gridLayoutListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You've created an awfully complicated web of dependencies that you should simplify. For example you shouldn't be passing around the activity object like that.
Also you when creating an Activity class you do not need to redefine the constructors. Using the super constructors is fine. What you do need to define are the onCreate onStart onPause onStop onDestroy methods. I highly suggest you read the Activity Documentation
A simpler implementation than what you have above, would be to get rid of your myTouch interface. Remove the implements OnTouchListener from the TouchableView class and create a OnTouchListener class inside your activity class.
It would look something like this:
public class MyTouchActivity extends Activity{
TouchableView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TouchableView();
tv.setOnTouchListener(new MyOwnTouchListener());
}
class MyOnTouchListener implements OnTouchListener{
public boolean onTouchEvent(View v, MotionEvent e){
switch(e.getAction){
case (MotionEvent.TOUCH_DOWN)
MyTouchActivity.this.touchDown();
break;
}
}
}
public boolean touchDown(){
//touch down happened
}
}
Could someone help me to create user defined listener interface with some code snippets?
Create a new file:
MyListener.java:
public interface MyListener {
// you can define any parameter as per your requirement
public void callback(View view, String result);
}
In your activity, implement the interface:
MyActivity.java:
public class MyActivity extends Activity implements MyListener {
#override
public void onCreate(){
MyButton m = new MyButton(this);
}
// method is invoked when MyButton is clicked
#override
public void callback(View view, String result) {
// do your stuff here
}
}
In your custom class, invoke the interface when needed:
MyButton.java:
public class MyButton {
MyListener ml;
// constructor
MyButton(MyListener ml) {
//Setting the listener
this.ml = ml;
}
public void MyLogicToIntimateOthers() {
//Invoke the interface
ml.callback(this, "success");
}
}
please do read observer pattern
listener interface
public interface OnEventListener {
void onEvent(EventResult er);
// or void onEvent(); as per your need
}
then in your class say Event class
public class Event {
private OnEventListener mOnEventListener;
public void setOnEventListener(OnEventListener listener) {
mOnEventListener = listener;
}
public void doEvent() {
/*
* code code code
*/
// and in the end
if (mOnEventListener != null)
mOnEventListener.onEvent(eventResult); // event result object :)
}
}
in your driver class MyTestDriver
public class MyTestDriver {
public static void main(String[] args) {
Event e = new Event();
e.setOnEventListener(new OnEventListener() {
public void onEvent(EventResult er) {
// do your work.
}
});
e.doEvent();
}
}
I have created a Generic AsyncTask Listener which get result from AsycTask seperate class and give it to CallingActivity using Interface Callback.
new GenericAsyncTask(context,new AsyncTaskCompleteListener()
{
public void onTaskComplete(String response)
{
// do your work.
}
}).execute();
Interface
interface AsyncTaskCompleteListener<T> {
public void onTaskComplete(T result);
}
GenericAsyncTask
class GenericAsyncTask extends AsyncTask<String, Void, String>
{
private AsyncTaskCompleteListener<String> callback;
public A(Context context, AsyncTaskCompleteListener<String> cb) {
this.context = context;
this.callback = cb;
}
protected void onPostExecute(String result) {
finalResult = result;
callback.onTaskComplete(result);
}
}
Have a look at this , this question for more details.
There are 4 steps:
1.create interface class (listener)
2.use interface in view 1 (define variable)
3.implements interface to view 2 (view 1 used in view 2)
4.pass interface in view 1 to view 2
Example:
Step 1: you need create interface and definde function
public interface onAddTextViewCustomListener {
void onAddText(String text);
}
Step 2: use this interface in view
public class CTextView extends TextView {
onAddTextViewCustomListener onAddTextViewCustomListener; //listener custom
public CTextView(Context context, onAddTextViewCustomListener onAddTextViewCustomListener) {
super(context);
this.onAddTextViewCustomListener = onAddTextViewCustomListener;
init(context, null);
}
public CTextView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CTextView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CTextView(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
public void init(Context context, #Nullable AttributeSet attrs) {
if (isInEditMode())
return;
//call listener
onAddTextViewCustomListener.onAddText("this TextView added");
}
}
Step 3,4: implements to activity
public class MainActivity extends AppCompatActivity implements onAddTextViewCustomListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get main view from layout
RelativeLayout mainView = (RelativeLayout)findViewById(R.id.mainView);
//create new CTextView and set listener
CTextView cTextView = new CTextView(getApplicationContext(), this);
//add cTextView to mainView
mainView.addView(cTextView);
}
#Override
public void onAddText(String text) {
Log.i("Message ", text);
}
}
Create listener interface.
public interface YourCustomListener
{
public void onCustomClick(View view);
// pass view as argument or whatever you want.
}
And create method setOnCustomClick in another activity(or fragment) , where you want to apply your custom listener......
public void setCustomClickListener(YourCustomListener yourCustomListener)
{
this.yourCustomListener= yourCustomListener;
}
Call this method from your First activity, and pass the listener interface...
In the year of 2018, there's no need for listeners interfaces. You've got Android LiveData to take care of passing the desired result back to the UI components.
If I'll take Rupesh's answer and adjust it to use LiveData, it will like so:
public class Event {
public LiveData<EventResult> doEvent() {
/*
* code code code
*/
// and in the end
LiveData<EventResult> result = new MutableLiveData<>();
result.setValue(eventResult);
return result;
}
}
and now in your driver class MyTestDriver:
public class MyTestDriver {
public static void main(String[] args) {
Event e = new Event();
e.doEvent().observe(this, new Observer<EventResult>() {
#Override
public void onChanged(final EventResult er) {
// do your work.
}
});
}
}
For more information along with code samples you can read my post about it, as well as the offical docs:
When and why to use LiveData
Official docs
In Android,you can create an interface such as Listener,and your Activity implements it,but i don't think it is a good idea.
if we have many components to listen the changes of their state,we can create a BaseListener implements interface Listener,and use type code to handle them.
we can bind the method when we create XML file,for example:
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button4"
android:onClick="Btn4OnClick" />
and the source code:
public void Btn4OnClick(View view) {
String strTmp = "点击Button04";
tv.setText(strTmp);
}
but i don't think it is a good idea...
I have done it something like below for sending my model class from the Second Activity to First Activity. I used LiveData to achieve this, with the help of answers from Rupesh and TheCodeFather.
Second Activity
public static MutableLiveData<AudioListModel> getLiveSong() {
MutableLiveData<AudioListModel> result = new MutableLiveData<>();
result.setValue(liveSong);
return result;
}
"liveSong" is AudioListModel declared globally
Call this method in the First Activity
PlayerActivity.getLiveSong().observe(this, new Observer<AudioListModel>() {
#Override
public void onChanged(AudioListModel audioListModel) {
if (PlayerActivity.mediaPlayer != null && PlayerActivity.mediaPlayer.isPlaying()) {
Log.d("LiveSong--->Changes-->", audioListModel.getSongName());
}
}
});
May this help for new explorers like me.
Simple method to do this approach. Firstly implements the OnClickListeners in your Activity class.
Code:
class MainActivity extends Activity implements OnClickListeners{
protected void OnCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.activity_main.xml);
Button b1=(Button)findViewById(R.id.sipsi);
Button b2=(Button)findViewById(R.id.pipsi);
b1.SetOnClickListener(this);
b2.SetOnClickListener(this);
}
public void OnClick(View V)
{
int i=v.getId();
switch(i)
{
case R.id.sipsi:
{
//you can do anything from this button
break;
}
case R.id.pipsi:
{
//you can do anything from this button
break;
}
}
}