Custom EditText with Line Number

Custom Edittext with Line Number

Mou icon

view raw
readme.md
hosted with ❤ by GitHub

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<rupantor.cusedittext.MyEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</ScrollView>

view raw
main.xml
hosted with ❤ by GitHub

package rupantor.cusedittext;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Author S Mahbub Uz Zaman on 5/9/15.
* Lisence Under GPL2
*/
public class MyEditText extends EditText {
private Rect rect;
private Paint paint;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
rect = new Rect();
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
}
@Override
protected void onDraw(Canvas canvas) {
int baseline = getBaseline();
for (int i = 0; i < getLineCount(); i++) {
canvas.drawText("" + (i+1), rect.left, baseline, paint);
baseline += getLineHeight();
}
super.onDraw(canvas);
}
}

view raw
MyEditText.java
hosted with ❤ by GitHub

How to change Android Studio Keymap (MAC OS X)

It is very difficult for the eclipse users to switch to Android Studio, specially when it comes to keyboard shortcuts. So to solve this issue just follow the following steps.

1. Go to Preference from (cmd + ,)
2. From the left hand find Keymap under IDE settings
3. After selecting the Keymap, you will see in the right hand side a drop down menu under Keymap
4. Select Eclipse (Mac OS X)

And you are done

here are few keyboard shortcuts

1. format code: cmd + shift + f
2. up or down line of codes: alt + up key / down key
3. open resource: cmd + shift + t
4. comment line: cmd + /
5. delete line: cmd + d
6. go to line number: cmd + l
7. last edit line: ctrl + q
8. implement/override methods: cmd + o

If any of the shortcuts is not working you can always edit or add them from the keymap section.

enjoy 🙂

Android Time Class

Get Old Month Using Time Class

// Author: Mahbub

/*
http://developer.android.com/reference/android/text/format/Time.html

just provide the month, year and day and get previous dates

Since Time class does not provides months and day name, you have to do that by yourself
*/

public void oldDate(int month, int year, int day) {
    
    --month;

    oldDay = 1; // here i did not vary the old months date

    String s;
    
    for (int i = 1; i <= 5; ++i) { // i am fetching last 5 month data
        
        if (month == -1) {
            month = 11;
            year = year - 1;
        }

        oldYear = year;
        oldMonth = month;
        
        Time old = new Time();
        old.set(oldDay, oldMonth, oldYear);
        s = "Month of "+old.month+ +old.year;
        
        // update
        --month;
    }
}



https://gist.github.com/lifeparticle/7139069