Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. package com.preons.util;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.os.Build;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.util.AttributeSet;
  10. import android.util.Log;
  11. import android.view.KeyEvent;
  12. import android.view.LayoutInflater;
  13. import android.view.MotionEvent;
  14. import android.view.View;
  15. import android.view.inputmethod.EditorInfo;
  16. import android.widget.EditText;
  17. import android.widget.ImageButton;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20.  
  21. import com.preons.feedbackforums.R;
  22.  
  23. //Just Some preDefined constants
  24. import static com.preons.util.AllUtils.getNumberText;
  25. import static com.preons.util.AllUtils.isVisible;
  26. import static com.preons.util.AllUtils.setVis;
  27. import static com.preons.util.constants.TAG;
  28. import static com.preons.util.constants.defaultQue;
  29. import static com.preons.util.constants.width;
  30.  
  31. public class TV extends LinearLayout{
  32.  
  33. //Defined below view in your xml
  34. private EditText editText; //Title Value
  35. private TextView textView; //Title
  36. private TextView textView1; //id ()series number before title
  37. private ImageButton imageButton; //editButton (Pencil icon)
  38.  
  39. private String s = getContext().getString(R.string.d_title_text);//default title preDefined in xml
  40.  
  41. private boolean Mode; //selectable Editmode-true & ViewMode-false
  42. private OnEditListener listener; //editing listiner listen to when edit text is visible
  43.  
  44. public TV(Context context) {
  45. super(context);
  46. OnCreate(context, null);
  47. }
  48.  
  49. public TV(Context context, AttributeSet attrs) {
  50. super(context, attrs);
  51. OnCreate(context,attrs);
  52. }
  53.  
  54. public TV(Context context, AttributeSet attrs, int defStyleAttr) {
  55. super(context, attrs, defStyleAttr);
  56. OnCreate(context,attrs);
  57. }
  58.  
  59. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  60. public TV(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  61. super(context, attrs, defStyleAttr, defStyleRes);
  62. OnCreate(context,attrs);
  63. }
  64.  
  65. private void OnCreate(final Context c, AttributeSet attrs) {
  66. listener = null;//Inilizes listener
  67. getWorkMode();
  68. TypedArray typedArray = c.obtainStyledAttributes(attrs, R.styleable.TV);
  69. int i = 0;
  70. try {
  71. s = typedArray.getString(R.styleable.TV_title);
  72. s=s==null?defaultQue:s;
  73. i = typedArray.getResourceId(R.styleable.TV_style, R.style.Default_TextTheme);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. } finally {
  77. typedArray.recycle();
  78. }
  79. LayoutInflater.from(c).inflate(R.layout.sin_tit_lay, this);
  80. textView = (TextView) findViewById(R.id.tit_tv);
  81. textView1 = (TextView) findViewById(R.id.tit_id);
  82. editText = (EditText) findViewById(R.id.tit_ed);
  83. imageButton = (ImageButton) findViewById(R.id.tit_img_btn);
  84. /**
  85. * Use this to Enable Editable icon (Pencil icon)
  86. * workSet(Mode);
  87.  
  88. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) textView.setTextAppearance(i);
  89. else textView.setTextAppearance(c, i);
  90. textView.setText(s);
  91. }
  92. }
  93.  
  94. private void workSet(boolean b){
  95. if(b) {
  96. imageButton.setOnClickListener(new OnClickListener() {
  97. @Override
  98. public void onClick(View v) {
  99. clickListener();
  100. }
  101. });
  102. imageButton.setVisibility(VISIBLE);
  103. setOnTouchListener(new OnTouchListener() {
  104. @Override
  105. public boolean onTouch(View v, MotionEvent event) {
  106. clearEditTextFocus(editText);
  107. return false;
  108. }
  109. });
  110. editText.addTextChangedListener(new TextWatcher() {
  111. @Override
  112. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  113.  
  114. }
  115.  
  116. @Override
  117. public void onTextChanged(CharSequence s, int start, int before, int count) {
  118. if(count>0) setVis(imageButton, true);
  119. else if(count==0&&editText.getVisibility()!=INVISIBLE)
  120. setVis(imageButton,false);
  121. }
  122.  
  123. @Override
  124. public void afterTextChanged(Editable s) {
  125.  
  126. }
  127. });
  128.  
  129. textView.setOnClickListener(new OnClickListener() {
  130. @Override
  131. public void onClick(View v) {
  132. clickListener();
  133. }
  134. });
  135. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  136. @Override
  137. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  138. imageButton.animate().translationX(0);
  139. setVis(editText,false);
  140. setVis(textView,true);
  141. imageButton.setImageResource(R.drawable.ic_edit);
  142. setVis(imageButton,true);
  143. setTitle(editText.getText().toString());
  144. return true;
  145. }
  146. });
  147.  
  148. editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
  149. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  150. @Override
  151. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  152. imageButton.animate().translationX(0);
  153. setVis(editText, false);
  154. setVis(textView, true);
  155. imageButton.setImageResource(R.drawable.ic_edit);
  156. setVis(imageButton, true);
  157. setTitle(editText.getText().toString());
  158. if(listener!=null)
  159. listener.onExpandedView(false);
  160. return true;
  161. }
  162. });
  163.  
  164. }else{
  165. editText.setVisibility(GONE);
  166. textView.setOnClickListener(null);
  167. imageButton.setVisibility(GONE);
  168. }
  169. }
  170.  
  171. //preDefined as stated above
  172. private void getWorkMode() {
  173. Mode = (boolean) constants.workMode;
  174. }
  175.  
  176. //common onClickfunction of textview and image button
  177. private void clickListener() {
  178. boolean b = isVisible(editText);
  179. if (b) {
  180. //if already Editing the use image button to clear text
  181. editText.setText("");
  182. setVis(imageButton, false);
  183. }
  184. else {
  185. imageButton.animate().translationX(width-width/8-imageButton.getX()-imageButton.getWidth());
  186. editText.setText(textView.getText());
  187. setVis(editText,true);
  188. setVis(textView,false);
  189. imageButton.setImageResource(R.drawable.ic_clear);
  190. editText.setText(textView.getText());
  191. editText.requestFocus();
  192. }
  193. if(listener!=null)
  194. listener.onExpandedView(true);
  195. }
  196.  
  197. public boolean setProperties(String Title, Object Element){
  198. //TODO : Do work here
  199. textView.setText(Title);
  200. //noinspection ConstantConditions
  201. textView1.setText(getNumberText((Integer) Element));
  202. return false;
  203. }
  204.  
  205. private void clearEditTextFocus(View view) {
  206. view.clearFocus();
  207. }
  208.  
  209. public void setTitle(String s){
  210. if(s.isEmpty())
  211. textView.setText(R.string.Empty_message);
  212. else
  213. textView.setText(s);
  214. }
  215.  
  216. public CharSequence getTitle(){
  217. return textView.getText();
  218. }
  219.  
  220. public void setNeeded(boolean b){
  221. setVisibility(b?VISIBLE:GONE);
  222. if(b) clickListener();
  223. }
  224. public void setMode(boolean mode) {
  225. Mode = mode;
  226. workSet(mode);
  227. }
  228.  
  229. public interface OnEditListener{
  230. void onExpandedView(Boolean b);
  231. }
  232.  
  233. // Assign the listener implementing events interface that will receive the events
  234. public void setOnEditListener(OnEditListener listener) {
  235. this.listener = listener;
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement