0

Hey so I am adding a custom edit text to my fragment and for some reason I can't get it to align with the top of the screen. It always ends up in the middle of the screen.

"NotePad" edit text:

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context) {
        super(context);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(getResources().getColor(R.color.textColorPrimary)); //SET YOUR OWN COLOR HERE
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();//for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);//first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }
}

Code to set layout params and add to layout:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_note, container, false);
    LinedEditText linedEditText = new LinedEditText(getActivity());
    RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.note_relative_layout);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.alignWithParent = true;
    params.addRule(RelativeLayout.ALIGN_TOP);
    linedEditText.setLayoutParams(params);
    relativeLayout.addView(linedEditText);
    return rootView;}

XML:

<RelativeLayout 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"
tools:context="com.glenn.sacramenttalktool.NoteFragment"
android:id="@+id/note_relative_layout"
android:gravity="top">

I've tried using FrameLayout, LinearLayout, and RelativeLayout but the behavior is the same. All I need is for the edit text to be aligned with the top of the screen.

Main activity layout

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.ldsTalkTool">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LDS Talk Tool"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/textColorPrimary"
        android:id="@+id/toolbar_title" />
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

I have tried adding it to XML like this even though that throws an error:

<com.glenn.[app_name].LinedEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Glenn Werner
  • 2,048
  • 2
  • 12
  • 16
  • Why are you creating this programatically and not just in XML? It seems pointless. Also this code wouldn't align it to the top of the screen, but to the top of the relative layout. At best it would put it at the top of the fragment. Is this fragment at the top of the screen in your activity? – Gabe Sechan Mar 08 '16 at 16:35
  • @GabeSechan I am not adding it in XML because it will throws an error when I try that. I'm assuming it would work in a regular activity but it won't in a fragment. And yes the fragment is at the top of the screen in my activity. – Glenn Werner Mar 08 '16 at 17:10
  • You're doing something wrong adding it to your xml then. There should be no problems adding a custom view to xml – Gabe Sechan Mar 08 '16 at 17:12
  • Also, post the activity layout – Gabe Sechan Mar 08 '16 at 17:12
  • Did that. I also noticed that there is a fragment_main layout that has MainActivity$PlaceholderFragment on the last part of the name in tools:context. Would that be messing with it? – Glenn Werner Mar 08 '16 at 17:21
  • did you try with Relativelayout.ALIGN_PARENT_TOP ? – PedroHawk Mar 08 '16 at 17:28
  • @PedroHawk Yeah it didn't change anything – Glenn Werner Mar 08 '16 at 17:30

2 Answers2

0

Try this. Change:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

to:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
0

It turns out that I had deleted the AttributeSet parameter from the constructor for the LinedEditText class.

public LinedEditText(Context context) {
    super(context);

According to this Error inflating when extending a class both the context and the AttributeSet are needed to inflate a view which is why it was throwing an error when I tried to just add the view in XML. This is what it should look like:

public LinedEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

Once I fixed that then I could directly add it to the XML and set the gravity to "start". It now fills the screen.

Community
  • 1
  • 1
Glenn Werner
  • 2,048
  • 2
  • 12
  • 16