Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

android pushing editText out of the screen resize it

I did myself a slide animation by playing with the margin of the components.

Unfortunately when I "push" the right element (an edittext) out of the screen, it resizes it instead of cutting the edge part.

I can get it to behave like I want by affecting for example 1000dp to the editText, but I want this editText to fit the width of the screen, not an hardcoded width.

Start screen :

start screen

Wrong behaviour :

wrong behaviour

Good behaviour :

Good behaviour

Here is the code of the "animation" in my custom root component :

package com.example.components;

import com.example.database.NoteDataSource;
import com.example.memonotepad.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.app.Activity;

public class SlideRightNLeft extends LinearLayout{

private LinearLayout.LayoutParams params1;

int x;

private SlideRightNLeft srnl;

private Context context_f;

private int slideDistance;


public SlideRightNLeft(Context context) {
    super(context);
}

public SlideRightNLeft(Context context, AttributeSet attrs) {
    super(context, attrs);
    context_f = context;
    srnl = this;
    slideDistance = (int) context_f.getResources().getDimension(R.dimen.slide_distance);

}

public SlideRightNLeft(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}



public void slideLogin(){


    Thread slideIt = new Thread(new Runnable() {
        @Override
        public void run() {

            x = 0;
            params1 = new LinearLayout.LayoutParams(srnl.getChildAt(0).getWidth(), srnl.getChildAt(0).getHeight());

            while(x < slideDistance) {

                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ((Activity)context_f).runOnUiThread(new Runnable(){

                    @Override
                    public void run() {

                        int diff = x - slideDistance;

                        params1.setMargins(diff, 1, 1, 1);

                        srnl.getChildAt(0).setLayoutParams(params1);

                        srnl.invalidate();

                    }
                });

                x++;

            }
        }
    });

    slideIt.start();

}




}

Activity code :

package com.example.memonotepad;

import java.util.Calendar;
import java.util.Date;

import com.example.components.SlideRightNLeft;
import com.example.database.NoteDataSource;
import com.example.adapter.NotesAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MemoNotepad extends Activity {

private SlideRightNLeft mainGroupComponent;
private Button slideLogin;
private Button addNote;

private NoteDataSource nds;

private Date rightNowDate;

private ListView lvNote;

private Cursor lvCursor;

private NotesAdapter na;

private EditText edt;

public static int width;

WindowManager mWinMgr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memo_notepad);

    mWinMgr = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
    width = mWinMgr.getDefaultDisplay().getWidth();

    edt = (EditText) findViewById(R.id.textBook);

    edt.setWidth(width);
    edt.setMinimumWidth(width);

    mainGroupComponent = (SlideRightNLeft) findViewById(R.id.mainGroupLayout);
    nds = new NoteDataSource(this);

    lvNote = (ListView) findViewById(R.id.listNotes);

    lvCursor = nds.getAllNotes();

    na = new NotesAdapter(this, lvCursor);
    lvNote.setAdapter(na);

    slideLogin = (Button) findViewById(R.id.login_button);
    slideLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mainGroupComponent.slideLogin();
        }
    });

    addNote = (Button) findViewById(R.id.new_note);
    addNote.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Calendar rightNow = Calendar.getInstance();
            rightNowDate = rightNow.getTime();  // dt is now the new date
            nds.createNote("Note", "text inside", rightNowDate);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_memo_notepad, menu);
    return true;
}
}

Upvotes: 0

Views: 456

Answers (1)

Tobrun
Tobrun

Reputation: 18381

I can get it to behave like I want by affecting for example 1000dp to the editText, but I want this editText to fit the width of the screen, not an hardcoded width.

Why not get the width of device?

mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int displayWidth = mWinMgr.getDefaultDisplay().getWidth();

Upvotes: 1

Related Questions