List of usage examples for android.text TextUtils copySpansFrom
public static void copySpansFrom(Spanned source, int start, int end, Class kind, Spannable dest, int destoff)
start...end
in source
to the region destoff...destoff+end-start
in dest
. From source file:Main.java
/** * Returns a CharSequence concatenating the specified CharSequences using the specified delimiter, * retaining their spans if any./*from w w w . java 2 s . co m*/ * * This is mostly borrowed from TextUtils.concat(); */ public static CharSequence joinSpannables(String delimiter, CharSequence... text) { if (text.length == 0) { return ""; } if (text.length == 1) { return text[0]; } boolean spanned = false; for (int i = 0; i < text.length; i++) { if (text[i] instanceof Spanned) { spanned = true; break; } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length; i++) { if (i > 0) { sb.append(delimiter); } sb.append(text[i]); } if (!spanned) { return sb.toString(); } SpannableString ss = new SpannableString(sb); int off = 0; for (int i = 0; i < text.length; i++) { int len = text[i].length(); if (text[i] instanceof Spanned) { TextUtils.copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off); } off += len + delimiter.length(); } return new SpannedString(ss); }
From source file:org.cnc.mombot.utils.ContactsEditText.java
private void init(Context context) { // Set image height mDropdownItemHeight = 48;// w ww .j a va2 s . c o m // Set default image BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture_holo_light, options); options.inSampleSize = Utils.calculateInSampleSize(options, mDropdownItemHeight, mDropdownItemHeight); options.inJustDecodeBounds = false; mLoadingImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture_holo_light, options); // Set adapter mAdapter = new ContactsAdapter(context); setAdapter(mAdapter); mAdapter.swapCursor(mAdapter.runQueryOnBackgroundThread("")); // Separate entries by commas setTokenizer(new Tokenizer() { @Override public CharSequence terminateToken(CharSequence text) { int i = text.length(); while (i > 0 && text.charAt(i - 1) == ' ') { i--; } if (i > 0 && text.charAt(i - 1) == ' ') { return text; } else { if (text instanceof Spanned) { SpannableString sp = new SpannableString(text + " "); TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0); return sp; } else { return text + " "; } } } @Override public int findTokenStart(CharSequence text, int cursor) { int i = cursor; while (i > 0 && text.charAt(i - 1) != ' ') { i--; } // Check if token really started with ' ', else we don't have a valid token if (i < 1 || text.charAt(i - 1) != ' ') { return cursor; } return i; } @Override public int findTokenEnd(CharSequence text, int cursor) { int i = cursor; int len = text.length(); while (i < len) { if (text.charAt(i) == ' ') { return i; } else { i++; } } return len; } }); // Pop up suggestions after 1 character is typed. setThreshold(1); setOnClickListener(this); setOnFocusChangeListener(this); }
From source file:nz.ac.otago.psyanlab.common.designer.program.operand.RenameOperandDialogueFragment.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle args = getArguments();//from ww w . j a v a 2 s. c o m if (args != null) { mOperandId = args.getLong(ARG_OPERAND_ID, -1); } if (mOperandId == -1) { throw new RuntimeException("Invalid operand id."); } mOperand = mCallbacks.getOperand(mOperandId); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialogue_rename_variable, null); mName = (EditText) view.findViewById(R.id.name); mName.setText(mOperand.getName()); // Thanks to serwus <http://stackoverflow.com/users/1598308/serwus>, // who posted at <http://stackoverflow.com/a/20325852>. Modified to // support unicode codepoints and validating first character of input. InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { boolean keepOriginal = true; StringBuilder sb = new StringBuilder(end - start); int offset = 0; String s = source.toString(); while (offset < s.length()) { final int codePoint = s.codePointAt(offset); if ((offset == 0 && isAllowedAsFirst(codePoint)) || (offset > 0 && isAllowed(codePoint))) { sb.appendCodePoint(codePoint); } else { keepOriginal = false; } offset += Character.charCount(codePoint); } if (keepOriginal) return null; else { if (source instanceof Spanned) { SpannableString sp = new SpannableString(sb); TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0); return sp; } else { return sb; } } } private boolean isAllowed(int codePoint) { return Character.isLetterOrDigit(codePoint); } private boolean isAllowedAsFirst(int codePoint) { return Character.isLetter(codePoint); } }; mName.setFilters(new InputFilter[] { filter }); // Build dialogue. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(getString(R.string.title_rename_variable, mOperand.getName())).setView(view) .setPositiveButton(R.string.action_rename, mPositiveListener) .setNegativeButton(R.string.action_cancel, mNegativeListener); // Create the AlertDialog object and return it Dialog dialog = builder.create(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); return dialog; }
From source file:org.akvo.rsr.up.UpdateEditorActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUser = SettingsUtil.getAuthUser(this); nextLocalId = SettingsUtil.ReadInt(this, ConstantUtil.LOCAL_ID_KEY, -1); // find which update we are editing // null means create a new one Bundle extras = getIntent().getExtras(); projectId = extras != null ? extras.getString(ConstantUtil.PROJECT_ID_KEY) : null; if (projectId == null) { DialogUtil.errorAlert(this, R.string.noproj_dialog_title, R.string.noproj_dialog_msg); }/* www.j a v a 2 s .c om*/ updateId = extras != null ? extras.getString(ConstantUtil.UPDATE_ID_KEY) : null; if (updateId == null) { updateId = savedInstanceState != null ? savedInstanceState.getString(ConstantUtil.UPDATE_ID_KEY) : null; } //Limit what we can write InputFilter postFilter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { boolean keepOriginal = true; StringBuilder sb = new StringBuilder(end - start); for (int i = start; i < end; i++) { char c = source.charAt(i); if (isCharAllowed(c)) // put your condition here sb.append(c); else keepOriginal = false; } if (keepOriginal) return null; else { if (source instanceof Spanned) { SpannableString sp = new SpannableString(sb); TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0); return sp; } else { return sb; } } } private boolean isCharAllowed(char c) { // return !Character.isSurrogate(c); //From API 19 return !(c >= 0xD800 && c <= 0xDFFF); } }; // get the look setContentView(R.layout.activity_update_editor); // find the fields progressGroup = findViewById(R.id.sendprogress_group); uploadProgress = (ProgressBar) findViewById(R.id.sendProgressBar); projTitleLabel = (TextView) findViewById(R.id.projupd_edit_proj_title); projupdTitleCount = (TextView) findViewById(R.id.projupd_edit_titlecount); projupdTitleCount.setText(Integer.toString(TITLE_LENGTH)); projupdTitleText = (EditText) findViewById(R.id.projupd_edit_title); projupdTitleText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(TITLE_LENGTH), postFilter }); projupdTitleText.addTextChangedListener(new TextWatcher() { //Show count of remaining characters public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { projupdTitleCount.setText(String.valueOf(TITLE_LENGTH - s.length())); } public void afterTextChanged(Editable s) { } }); projupdDescriptionText = (EditText) findViewById(R.id.projupd_edit_description); projupdDescriptionText.setFilters(new InputFilter[] { postFilter }); projupdImage = (ImageView) findViewById(R.id.image_update_detail); photoAndToolsGroup = findViewById(R.id.image_with_tools); photoAddGroup = findViewById(R.id.photo_buttons); photoCaptionText = (EditText) findViewById(R.id.projupd_edit_photo_caption); photoCaptionText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(75), postFilter }); photoCreditText = (EditText) findViewById(R.id.projupd_edit_photo_credit); photoCreditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(25), postFilter }); positionGroup = findViewById(R.id.position_group); latField = (TextView) findViewById(R.id.latitude); lonField = (TextView) findViewById(R.id.longitude); eleField = (TextView) findViewById(R.id.elevation); accuracyField = (TextView) findViewById(R.id.gps_accuracy); searchingIndicator = (TextView) findViewById(R.id.gps_searching); gpsProgress = (ProgressBar) findViewById(R.id.progress_gps); // Activate buttons btnSubmit = (Button) findViewById(R.id.btn_send_update); btnSubmit.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { sendUpdate(); } }); btnDraft = (Button) findViewById(R.id.btn_save_draft); btnDraft.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { saveAsDraft(true); } }); btnTakePhoto = (Button) findViewById(R.id.btn_take_photo); btnTakePhoto.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // generate unique filename captureFilename = FileUtil.getExternalPhotoDir(UpdateEditorActivity.this) + File.separator + "capture" + System.nanoTime() + ".jpg"; takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(captureFilename))); startActivityForResult(takePictureIntent, photoRequest); } }); btnAttachPhoto = (Button) findViewById(R.id.btn_attach_photo); btnAttachPhoto.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, photoPick); } }); btnDelPhoto = (Button) findViewById(R.id.btn_delete_photo); btnDelPhoto.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Forget image update.setThumbnailFilename(null); // TODO: delete image file if it was taken through this app? // Hide photo w tools showPhoto(false); } }); btnRotRightPhoto = (Button) findViewById(R.id.btn_rotate_photo_r); btnRotRightPhoto.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Rotate image right rotatePhoto(true); } }); btnGpsGeo = (Button) findViewById(R.id.btn_gps_position); btnGpsGeo.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onGetGPSClick(view); } }); btnPhotoGeo = (Button) findViewById(R.id.btn_photo_position); btnPhotoGeo.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onGetPhotoLocationClick(view); } }); dba = new RsrDbAdapter(this); dba.open(); Project project = dba.findProject(projectId); projTitleLabel.setText(project.getTitle()); if (updateId == null) { // create new update = new Update(); update.setUuid(UUID.randomUUID().toString()); // should do sth // better, especially // if MAC address is // avaliable /* * WifiManager wifiManager = (WifiManager) * getSystemService(Context.WIFI_SERVICE); WifiInfo wInfo = * wifiManager.getConnectionInfo(); String macAddress = * wInfo.getMacAddress(); if (macAddress == null) txt_View.append( * "MAC Address : " + macAddress + "\n" ); else txt_View.append( * "MAC Address : " + macAddress + "\n" ); } */ update.setUserId(mUser.getId()); update.setDate(new Date()); editable = true; } else { update = dba.findUpdate(updateId); if (update == null) { DialogUtil.errorAlert(this, R.string.noupd_dialog_title, R.string.noupd2_dialog_msg); } else { // populate fields editable = update.getDraft(); // This should always be true with // the current UI flow - we go to // UpdateDetailActivity if it is sent if (update.getTitle().equals(TITLE_PLACEHOLDER)) { projupdTitleText.setText(""); //placeholder is just to satisfy db } else { projupdTitleText.setText(update.getTitle()); } projupdDescriptionText.setText(update.getText()); photoCaptionText.setText(update.getPhotoCaption()); photoCreditText.setText(update.getPhotoCredit()); latField.setText(update.getLatitude()); lonField.setText(update.getLongitude()); eleField.setText(update.getElevation()); if (update.validLatLon()) { positionGroup.setVisibility(View.VISIBLE); } // show preexisting image if (update.getThumbnailFilename() != null) { // btnTakePhoto.setText(R.string.btncaption_rephoto); ThumbnailUtil.setPhotoFile(projupdImage, update.getThumbnailUrl(), update.getThumbnailFilename(), null, null, false); photoLocation = FileUtil.exifLocation(update.getThumbnailFilename()); showPhoto(true); } } } // register a listener for a completion and progress intents broadRec = new ResponseReceiver(); IntentFilter f = new IntentFilter(ConstantUtil.UPDATES_SENT_ACTION); f.addAction(ConstantUtil.UPDATES_SENDPROGRESS_ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(broadRec, f); enableChanges(editable); btnDraft.setVisibility(editable ? View.VISIBLE : View.GONE); btnSubmit.setVisibility(editable ? View.VISIBLE : View.GONE); // btnTakePhoto.setVisibility(editable?View.VISIBLE:View.GONE); // Show the Up button in the action bar. // setupActionBar(); }