Android Open Source - justified Justified Edit Text






From Project

Back to project page justified.

License

The source code is released under:

Apache License

If you think the Android project justified listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 UNCOPT LLC./*ww w  .j av  a 2 s  . c o  m*/
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.uncopt.android.widget.text.justify;

import android.content.Context;
import android.text.Layout;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A TextView with justified text.<br>
 * The TextView has a ScrollingMovementMethod by default. You can change the MovementMethod,
 * but you should not set it to null.
 */
public class JustifiedEditText extends EditText implements Justify.Justified {

  @SuppressWarnings("unused")
  public JustifiedEditText(final @NotNull Context context) {
    super(context);
    super.setMovementMethod(new ScrollingMovementMethod());
  }

  @SuppressWarnings("unused")
  public JustifiedEditText(final @NotNull Context context, final AttributeSet attrs) {
    super(context, attrs);
    super.setMovementMethod(new ScrollingMovementMethod());
  }

  @SuppressWarnings("unused")
  public JustifiedEditText(final @NotNull Context context,
                           final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    super.setMovementMethod(new ScrollingMovementMethod());
  }

  @Override
  protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // Make sure we don't call setupScaleSpans again if the measure was triggered
    // by setupScaleSpans itself.
    if (!mMeasuring) {
      mMeasuring = true;
      try {
        // Setup ScaleXSpans on whitespaces to justify the text.
        Justify.setupScaleSpans(this, mSpanStarts, mSpanEnds, mSpans);
      }
      finally {
        mMeasuring = false;
      }
    }
  }

  @Override
  protected void onTextChanged(final CharSequence text,
                               final int start, final int lengthBefore, final int lengthAfter) {
    super.onTextChanged(text, start, lengthBefore, lengthAfter);
    final Layout layout = getLayout();
    if (layout != null) {
      Justify.setupScaleSpans(this, mSpanStarts, mSpanEnds, mSpans);
    }
  }

  @Override
  @NotNull
  public TextView getTextView() {
    return this;
  }

  @Override
  public float getMaxProportion() {
    return Justify.DEFAULT_MAX_PROPORTION;
  }

  private static final int MAX_SPANS = 512;

  private boolean mMeasuring = false;

  private int[] mSpanStarts = new int[MAX_SPANS];
  private int[] mSpanEnds = new int[MAX_SPANS];
  private Justify.ScaleSpan[] mSpans = new Justify.ScaleSpan[MAX_SPANS];

}




Java Source Code List

com.uncopt.android.example.justify.ExampleActivity.java
com.uncopt.android.example.justify.MyJustifiedEditText.java
com.uncopt.android.example.justify.MyJustifiedTextView.java
com.uncopt.android.example.justify.MyViewPager.java
com.uncopt.android.example.justify.NotNull.java
com.uncopt.android.example.justify.Nullable.java
com.uncopt.android.widget.text.justify.JustifiedEditText.java
com.uncopt.android.widget.text.justify.JustifiedTextView.java
com.uncopt.android.widget.text.justify.Justify.java
com.uncopt.android.widget.text.justify.NotNull.java
com.uncopt.android.widget.text.justify.Nullable.java