Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.preons.util;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Build;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.inputmethod.EditorInfo;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.preons.feedbackforums.R;
- //Just Some preDefined constants
- import static com.preons.util.AllUtils.getNumberText;
- import static com.preons.util.AllUtils.isVisible;
- import static com.preons.util.AllUtils.setVis;
- import static com.preons.util.constants.TAG;
- import static com.preons.util.constants.defaultQue;
- import static com.preons.util.constants.width;
- public class TV extends LinearLayout{
- //Defined below view in your xml
- private EditText editText; //Title Value
- private TextView textView; //Title
- private TextView textView1; //id ()series number before title
- private ImageButton imageButton; //editButton (Pencil icon)
- private String s = getContext().getString(R.string.d_title_text);//default title preDefined in xml
- private boolean Mode; //selectable Editmode-true & ViewMode-false
- private OnEditListener listener; //editing listiner listen to when edit text is visible
- public TV(Context context) {
- super(context);
- OnCreate(context, null);
- }
- public TV(Context context, AttributeSet attrs) {
- super(context, attrs);
- OnCreate(context,attrs);
- }
- public TV(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- OnCreate(context,attrs);
- }
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public TV(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- OnCreate(context,attrs);
- }
- private void OnCreate(final Context c, AttributeSet attrs) {
- listener = null;//Inilizes listener
- getWorkMode();
- TypedArray typedArray = c.obtainStyledAttributes(attrs, R.styleable.TV);
- int i = 0;
- try {
- s = typedArray.getString(R.styleable.TV_title);
- s=s==null?defaultQue:s;
- i = typedArray.getResourceId(R.styleable.TV_style, R.style.Default_TextTheme);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- typedArray.recycle();
- }
- LayoutInflater.from(c).inflate(R.layout.sin_tit_lay, this);
- textView = (TextView) findViewById(R.id.tit_tv);
- textView1 = (TextView) findViewById(R.id.tit_id);
- editText = (EditText) findViewById(R.id.tit_ed);
- imageButton = (ImageButton) findViewById(R.id.tit_img_btn);
- /**
- * Use this to Enable Editable icon (Pencil icon)
- * workSet(Mode);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) textView.setTextAppearance(i);
- else textView.setTextAppearance(c, i);
- textView.setText(s);
- }
- }
- private void workSet(boolean b){
- if(b) {
- imageButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- clickListener();
- }
- });
- imageButton.setVisibility(VISIBLE);
- setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- clearEditTextFocus(editText);
- return false;
- }
- });
- editText.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(count>0) setVis(imageButton, true);
- else if(count==0&&editText.getVisibility()!=INVISIBLE)
- setVis(imageButton,false);
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- textView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- clickListener();
- }
- });
- editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- imageButton.animate().translationX(0);
- setVis(editText,false);
- setVis(textView,true);
- imageButton.setImageResource(R.drawable.ic_edit);
- setVis(imageButton,true);
- setTitle(editText.getText().toString());
- return true;
- }
- });
- editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
- editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- imageButton.animate().translationX(0);
- setVis(editText, false);
- setVis(textView, true);
- imageButton.setImageResource(R.drawable.ic_edit);
- setVis(imageButton, true);
- setTitle(editText.getText().toString());
- if(listener!=null)
- listener.onExpandedView(false);
- return true;
- }
- });
- }else{
- editText.setVisibility(GONE);
- textView.setOnClickListener(null);
- imageButton.setVisibility(GONE);
- }
- }
- //preDefined as stated above
- private void getWorkMode() {
- Mode = (boolean) constants.workMode;
- }
- //common onClickfunction of textview and image button
- private void clickListener() {
- boolean b = isVisible(editText);
- if (b) {
- //if already Editing the use image button to clear text
- editText.setText("");
- setVis(imageButton, false);
- }
- else {
- imageButton.animate().translationX(width-width/8-imageButton.getX()-imageButton.getWidth());
- editText.setText(textView.getText());
- setVis(editText,true);
- setVis(textView,false);
- imageButton.setImageResource(R.drawable.ic_clear);
- editText.setText(textView.getText());
- editText.requestFocus();
- }
- if(listener!=null)
- listener.onExpandedView(true);
- }
- public boolean setProperties(String Title, Object Element){
- //TODO : Do work here
- textView.setText(Title);
- //noinspection ConstantConditions
- textView1.setText(getNumberText((Integer) Element));
- return false;
- }
- private void clearEditTextFocus(View view) {
- view.clearFocus();
- }
- public void setTitle(String s){
- if(s.isEmpty())
- textView.setText(R.string.Empty_message);
- else
- textView.setText(s);
- }
- public CharSequence getTitle(){
- return textView.getText();
- }
- public void setNeeded(boolean b){
- setVisibility(b?VISIBLE:GONE);
- if(b) clickListener();
- }
- public void setMode(boolean mode) {
- Mode = mode;
- workSet(mode);
- }
- public interface OnEditListener{
- void onExpandedView(Boolean b);
- }
- // Assign the listener implementing events interface that will receive the events
- public void setOnEditListener(OnEditListener listener) {
- this.listener = listener;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement