Java tutorial
/******************************************************************************* * Copyright 2016 stfalcon.com * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.stfalcon.chatkit.messages; import android.content.Context; import android.graphics.drawable.Drawable; import android.support.v4.widget.Space; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.TextView; import com.stfalcon.chatkit.R; import java.lang.reflect.Field; /** * Component for input outcoming messages */ public class MessageInput extends RelativeLayout implements View.OnClickListener, TextWatcher { private EditText messageInput; private ImageButton messageSendButton; private Space buttonSpace; private CharSequence input; private InputListener inputListener; public MessageInput(Context context) { super(context); init(context); } public MessageInput(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public MessageInput(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } /** * Set callback to be invoked when user entered his input * @param inputListener input callback */ public void setInputListener(InputListener inputListener) { this.inputListener = inputListener; } @Override public void onClick(View view) { int id = view.getId(); if (id == R.id.messageSendButton) { boolean isSubmitted = onSubmit(); if (isSubmitted) { messageInput.setText(""); } } } /** * This method is called to notify you that, within s, * the count characters beginning at start have just replaced old text that had length before */ @Override public void onTextChanged(CharSequence s, int start, int count, int after) { input = s; messageSendButton.setEnabled(input.length() > 0); } /** * This method is called to notify you that, within s, * the count characters beginning at start are about to be replaced by new text with length after. */ @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } /** *This method is called to notify you that, somewhere within s, the text has been changed. */ @Override public void afterTextChanged(Editable editable) { } private boolean onSubmit() { return inputListener != null && inputListener.onSubmit(input); } private void init(Context context, AttributeSet attrs) { init(context); MessageInputStyle style = MessageInputStyle.parse(context, attrs); this.messageInput.setMaxLines(style.getInputMaxLines()); this.messageInput.setHint(style.getInputHint()); this.messageInput.setText(style.getInputText()); this.messageInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, style.getInputTextSize()); this.messageInput.setTextColor(style.getInputTextColor()); this.messageInput.setHintTextColor(style.getInputHintColor()); this.messageInput.setBackground(style.getInputBackground()); setCursor(style.getInputCursorDrawable()); this.messageSendButton.setBackground(style.getInputButtonBackground()); this.messageSendButton.setImageDrawable(style.getInputButtonIcon()); this.messageSendButton.getLayoutParams().width = style.getInputButtonWidth(); this.messageSendButton.getLayoutParams().height = style.getInputButtonHeight(); this.buttonSpace.getLayoutParams().width = style.getInputButtonMargin(); if (getPaddingLeft() == 0 && getPaddingRight() == 0 && getPaddingTop() == 0 && getPaddingBottom() == 0) { setPadding(style.getInputDefaultPaddingLeft(), style.getInputDefaultPaddingTop(), style.getInputDefaultPaddingRight(), style.getInputDefaultPaddingBottom()); } } private void init(Context context) { inflate(context, R.layout.view_message_input, this); messageInput = (EditText) findViewById(R.id.messageInput); messageSendButton = (ImageButton) findViewById(R.id.messageSendButton); buttonSpace = (Space) findViewById(R.id.buttonSpace); messageSendButton.setOnClickListener(this); messageInput.addTextChangedListener(this); messageInput.setText(""); } private void setCursor(Drawable drawable) { try { Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); f.setAccessible(true); f.set(this.messageInput, drawable); } catch (Exception ignore) { } } /** * Interface definition for a callback to be invoked when user entered his input */ public interface InputListener { /** * Fires when user press send button. * * @param input input entered by user * @return if input text is valid, you must return {@code true} and input will be cleared, otherwise return false. */ boolean onSubmit(CharSequence input); } }