Browse Source

autofit grid for buttons, text color, text size, latex

pull/45/head
Thodoris1999 6 years ago
parent
commit
27c12a4129
  1. 55
      app/src/main/java/gr/thmmy/mthmmy/utils/AutoFitGridLayout.java
  2. 22
      app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java
  3. 6
      app/src/main/res/drawable/ic_format_color_text_36dp.xml
  4. 5
      app/src/main/res/drawable/ic_format_size_black_36dp.xml
  5. 5
      app/src/main/res/drawable/ic_functions_black_36dp.xml
  6. 49
      app/src/main/res/layout/editor_view.xml
  7. 6
      app/src/main/res/values/attrs.xml

55
app/src/main/java/gr/thmmy/mthmmy/utils/AutoFitGridLayout.java

@ -0,0 +1,55 @@
package gr.thmmy.mthmmy.utils;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.GridLayout;
import gr.thmmy.mthmmy.R;
public class AutoFitGridLayout extends GridLayout {
private int columnWidth;
private int defaultColumnCount;
public AutoFitGridLayout(Context context) {
super(context);
init(context, null, 0);
}
public AutoFitGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public AutoFitGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
public void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoFitGridLayout, 0, defStyleAttr);
try {
columnWidth = a.getDimensionPixelSize(R.styleable.AutoFitGridLayout_columnWidth, 0);
int[] set = {android.R.attr.columnCount};
a = context.obtainStyledAttributes(attrs, set, 0, defStyleAttr);
defaultColumnCount = a.getInt(0, 6);
} finally {
a.recycle();
}
setColumnCount(1);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
int width = MeasureSpec.getSize(widthSpec);
if (columnWidth > 0 && width > 0) {
int totalSpace = width - getPaddingRight() - getPaddingLeft();
int columnCount = Math.max(1, totalSpace / columnWidth);
setColumnCount(columnCount);
} else {
setColumnCount(defaultColumnCount);
}
}
}

22
app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java

@ -2,14 +2,17 @@ package gr.thmmy.mthmmy.utils;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.AppCompatImageButton;
import android.text.Editable; import android.text.Editable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
@ -90,11 +93,18 @@ public class EditorView extends LinearLayout {
getText().insert(editText.getSelectionStart(), "[s][/s]"); getText().insert(editText.getSelectionStart(), "[s][/s]");
editText.setSelection(editText.getSelectionStart() - 4); editText.setSelection(editText.getSelectionStart() - 4);
}); });
findViewById(R.id.unordered_list_button).setOnClickListener(view -> { // TODO: popup menu for colors
findViewById(R.id.text_color_button).setOnClickListener(view -> {
if (editText.hasSelection()) if (editText.hasSelection())
editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd());
getText().insert(editText.getSelectionStart(), "[list]\n[li][/li]\n[li][/li]\n[li][/li]\n[/list]"); getText().insert(editText.getSelectionStart(), "[color=][/color]");
editText.setSelection(editText.getSelectionStart() - 33); editText.setSelection(editText.getSelectionStart() - 8);
});
findViewById(R.id.text_size_button).setOnClickListener(view -> {
if (editText.hasSelection())
editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd());
getText().insert(editText.getSelectionStart(), "[size=10pt][/size]");
editText.setSelection(editText.getSelectionStart() - 7);
}); });
findViewById(R.id.align_left_button).setOnClickListener(view -> { findViewById(R.id.align_left_button).setOnClickListener(view -> {
if (editText.hasSelection()) if (editText.hasSelection())
@ -114,6 +124,12 @@ public class EditorView extends LinearLayout {
getText().insert(editText.getSelectionStart(), "[right][/right]"); getText().insert(editText.getSelectionStart(), "[right][/right]");
editText.setSelection(editText.getSelectionStart() - 8); editText.setSelection(editText.getSelectionStart() - 8);
}); });
findViewById(R.id.math_button).setOnClickListener(view -> {
if (editText.hasSelection())
editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd());
getText().insert(editText.getSelectionStart(), "[tex][/tex]");
editText.setSelection(editText.getSelectionStart() - 6);
});
} }
public Editable getText() { public Editable getText() {

6
app/src/main/res/drawable/ic_format_color_text_36dp.xml

@ -0,0 +1,6 @@
<vector android:height="36dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha=".36" android:fillColor="@color/colorAccent" android:pathData="M0,20h24v4H0z"/>
<path android:fillColor="#FF4B4B4B" android:pathData="M11,3L5.5,17h2.25l1.12,-3h6.25l1.12,3h2.25L13,3h-2zM9.62,12L12,5.67 14.38,12L9.62,12z"/>
</vector>

5
app/src/main/res/drawable/ic_format_size_black_36dp.xml

@ -0,0 +1,5 @@
<vector android:height="36dp" android:tint="#4B4B4B"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M9,4v3h5v12h3L17,7h5L22,4L9,4zM3,12h3v7h3v-7h3L12,9L3,9v3z"/>
</vector>

5
app/src/main/res/drawable/ic_functions_black_36dp.xml

@ -0,0 +1,5 @@
<vector android:height="36dp" android:tint="#4B4B4B"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M18,4H6v2l6.5,6L6,18v2h12v-3h-7l5,-5 -5,-5h7z"/>
</vector>

49
app/src/main/res/layout/editor_view.xml

@ -4,67 +4,78 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<LinearLayout <gr.thmmy.mthmmy.utils.AutoFitGridLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal"
app:columnWidth="36dp">
<!--bold, italic, etc buttons--> <!--bold, italic, etc buttons-->
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/bold_button" android:id="@+id/bold_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_bold_grey_36dp" app:srcCompat="@drawable/ic_format_bold_grey_36dp"/>
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/italic_button" android:id="@+id/italic_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_italic_grey_36dp" app:srcCompat="@drawable/ic_format_italic_grey_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/underline_button" android:id="@+id/underline_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_underlined_grey_36dp" app:srcCompat="@drawable/ic_format_underlined_grey_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/strikethrough_button" android:id="@+id/strikethrough_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_strikethrough_s_grey_36dp" app:srcCompat="@drawable/ic_strikethrough_s_grey_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/text_color_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_color_text_36dp"/>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/text_size_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_size_black_36dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/unordered_list_button" android:id="@+id/unordered_list_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_list_bulleted_black_36dp" app:srcCompat="@drawable/ic_format_list_bulleted_black_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/align_left_button" android:id="@+id/align_left_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_align_left_grey_36dp" app:srcCompat="@drawable/ic_format_align_left_grey_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/align_center_button" android:id="@+id/align_center_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_align_center_grey_36dp" app:srcCompat="@drawable/ic_format_align_center_grey_36dp" />
android:padding="4dp"/>
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/align_right_button" android:id="@+id/align_right_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_format_align_right_grey_36dp" app:srcCompat="@drawable/ic_format_align_right_grey_36dp" />
android:padding="4dp"/>
</LinearLayout> <android.support.v7.widget.AppCompatImageButton
android:id="@+id/math_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_functions_black_36dp"/>
</gr.thmmy.mthmmy.utils.AutoFitGridLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"

6
app/src/main/res/values/attrs.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AutoFitGridLayout" >
<attr name="columnWidth" format="dimension" />
</declare-styleable>
</resources>
Loading…
Cancel
Save