Skip to content

Commit dcf1443

Browse files
committed
Add Simple source position listener in the example app
1 parent c341e75 commit dcf1443

6 files changed

Lines changed: 109 additions & 6 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
<activity
1414
android:name=".MainActivity"
15-
android:exported="true">
15+
android:exported="true"
16+
android:windowSoftInputMode="adjustResize">
1617
<intent-filter>
1718
<action android:name="android.intent.action.MAIN" />
1819

app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import android.widget.ArrayAdapter;
1515
import android.widget.EditText;
1616
import android.widget.ImageButton;
17+
import android.widget.TextView;
1718

1819
import com.amrdeveloper.codeview.Code;
1920
import com.amrdeveloper.codeview.CodeView;
2021
import com.amrdeveloper.codeviewlibrary.plugin.CommentManager;
22+
import com.amrdeveloper.codeviewlibrary.plugin.SourcePositionListener;
2123
import com.amrdeveloper.codeviewlibrary.syntax.ThemeName;
2224
import com.amrdeveloper.codeviewlibrary.syntax.LanguageName;
2325
import com.amrdeveloper.codeviewlibrary.syntax.LanguageManager;
@@ -33,6 +35,9 @@ public class MainActivity extends AppCompatActivity {
3335
private LanguageManager languageManager;
3436
private CommentManager commentManager;
3537

38+
private TextView languageNameText;
39+
private TextView sourcePositionText;
40+
3641
private LanguageName currentLanguage = LanguageName.JAVA;
3742
private ThemeName currentTheme = ThemeName.MONOKAI;
3843

@@ -119,13 +124,30 @@ private void configCodeViewPlugins() {
119124
commentManager = new CommentManager(codeView);
120125
configCommentInfo();
121126

127+
languageNameText = findViewById(R.id.language_name_txt);
128+
configLanguageName();
129+
130+
sourcePositionText = findViewById(R.id.source_position_txt);
131+
sourcePositionText.setText(getString(R.string.source_position, 0, 0));
132+
configSourcePositionListener();
122133
}
123134

124135
private void configCommentInfo() {
125136
commentManager.setCommentStart(languageManager.getCommentStart(currentLanguage));
126137
commentManager.setCommendEnd(languageManager.getCommentEnd(currentLanguage));
127138
}
128139

140+
private void configLanguageName() {
141+
languageNameText.setText(currentLanguage.name().toLowerCase());
142+
}
143+
144+
private void configSourcePositionListener() {
145+
SourcePositionListener sourcePositionListener = new SourcePositionListener(codeView);
146+
sourcePositionListener.setOnPositionChanged((line, column) -> {
147+
sourcePositionText.setText(getString(R.string.source_position, line, column));
148+
});
149+
}
150+
129151
@Override
130152
public boolean onCreateOptionsMenu(Menu menu) {
131153
getMenuInflater().inflate(R.menu.menu_main, menu);
@@ -155,6 +177,7 @@ private void changeTheEditorLanguage(int languageId) {
155177

156178
if (currentLanguage != oldLanguage) {
157179
languageManager.applyTheme(currentLanguage, currentTheme);
180+
configLanguageName();
158181
configLanguageAutoComplete();
159182
configLanguageAutoIndentation();
160183
configCommentInfo();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.amrdeveloper.codeviewlibrary.plugin;
2+
3+
import android.text.Layout;
4+
import android.view.View;
5+
import android.view.accessibility.AccessibilityEvent;
6+
import android.widget.EditText;
7+
8+
public class SourcePositionListener {
9+
10+
private final EditText editText;
11+
12+
@FunctionalInterface
13+
public interface OnPositionChanged {
14+
void onPositionChange(int line, int column);
15+
}
16+
17+
private OnPositionChanged onPositionChanged;
18+
19+
public SourcePositionListener(EditText editText) {
20+
this.editText = editText;
21+
editText.setAccessibilityDelegate(viewAccessibility);
22+
}
23+
24+
public void setOnPositionChanged(OnPositionChanged listener) {
25+
onPositionChanged = listener;
26+
}
27+
28+
private final View.AccessibilityDelegate viewAccessibility = new View.AccessibilityDelegate() {
29+
30+
@Override
31+
public void sendAccessibilityEvent(View host, int eventType) {
32+
super.sendAccessibilityEvent(host, eventType);
33+
if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED && onPositionChanged != null) {
34+
int selectionStart = editText.getSelectionStart();
35+
Layout layout = editText.getLayout();
36+
if (layout == null) return;
37+
int line = editText.getLayout().getLineForOffset(selectionStart);
38+
int column = selectionStart - editText.getLayout().getLineStart(line);
39+
onPositionChanged.onPositionChange(line + 1, column + 1);
40+
}
41+
}
42+
};
43+
}
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
66
tools:context=".MainActivity">
77

88
<com.amrdeveloper.codeview.CodeView
99
android:id="@+id/codeView"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:layout_above="@+id/source_info_layout"
13+
android:background="@color/darkGrey"
1014
android:dropDownWidth="@dimen/dimen150dp"
1115
android:dropDownHorizontalOffset="0dp"
1216
android:dropDownSelector="@color/darkGrey"
13-
android:gravity="top|start"
14-
android:background="@color/darkGrey"
17+
android:gravity="top|start" />
18+
19+
<RelativeLayout
20+
android:id="@+id/source_info_layout"
1521
android:layout_width="match_parent"
16-
android:layout_height="match_parent"/>
17-
</LinearLayout>
22+
android:layout_height="@dimen/dimen35dp"
23+
android:layout_alignParentBottom="true"
24+
android:background="@color/black">
25+
26+
<TextView
27+
android:id="@+id/language_name_txt"
28+
android:layout_width="wrap_content"
29+
android:layout_height="match_parent"
30+
android:layout_alignParentStart="true"
31+
android:layout_alignParentLeft="true"
32+
android:layout_marginStart="@dimen/dimen5dp"
33+
android:layout_marginLeft="@dimen/dimen5dp"
34+
android:gravity="center"
35+
android:textColor="@color/grey"
36+
tools:text="Java" />
37+
38+
<TextView
39+
android:id="@+id/source_position_txt"
40+
android:layout_width="wrap_content"
41+
android:layout_height="match_parent"
42+
android:layout_alignParentEnd="true"
43+
android:layout_alignParentRight="true"
44+
android:layout_marginEnd="@dimen/dimen5dp"
45+
android:layout_marginRight="@dimen/dimen5dp"
46+
android:gravity="center"
47+
android:textColor="@color/grey"
48+
tools:text="0:0" />
49+
</RelativeLayout>
50+
</RelativeLayout>

app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<dimen name="dimen10dp">10dp</dimen>
66
<dimen name="dimen20dp">20dp</dimen>
77
<dimen name="dimen25dp">25dp</dimen>
8+
<dimen name="dimen35dp">35dp</dimen>
89
<dimen name="dimen40dp">40dp</dimen>
910
<dimen name="dimen45dp">45dp</dimen>
1011
<dimen name="dimen50dp">50dp</dimen>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
<string name="noctics_white">Noctics White</string>
2525
<string name="five_color_dark">Five Color Dark</string>
2626
<string name="orange_box">Orange Box</string>
27+
28+
<string name="source_position">%1$d:%2$d</string>
2729
</resources>

0 commit comments

Comments
 (0)