Abdul1991
Abdul1991

Reputation: 13

Can't control cursor position in edittext

I am practising android development, and I'm trying to follow guides on how to make a Twitter client app. I'm implementing my compose activity, but when I run the app the cursor is shown far down from the first line. I've followed many tips on stackoverflow to fix it but couldn't

here's a screenshot: http://t.co/qNA0gssOUG

ComposeActivity.java

package codepath.exercises.tweets;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class ComposeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compose);
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    Button tweetButton = (Button) findViewById(R.id.tweet_button);
    Button cancelButton = (Button) findViewById(R.id.cancel_button);
    final EditText tweetEditText = (EditText) findViewById(R.id.edittext_tweet);
    tweetEditText.requestFocus();
    tweetEditText.setSelection(0);
    tweetButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
        }

    });
    cancelButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.compose, menu);
    return true;
}

}

activity_compose.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"
android:background="#efefef"
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=".ComposeActivity" >

<Button
    android:id="@+id/cancel_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/edittext_tweet"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:text="@string/cancel_button" />

<EditText
    android:id="@id/edittext_tweet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/cancel_button"
    android:layout_marginTop="10dp"
    android:background="#ffffff"
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/tweet_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/cancel_button"
    android:layout_alignBottom="@+id/cancel_button"
    android:layout_alignRight="@id/edittext_tweet"
    android:text="@string/tweet_button" />

</RelativeLayout>

Upvotes: 1

Views: 636

Answers (3)

Ahmad Ronagh
Ahmad Ronagh

Reputation: 800

You can add

android:gravity="top" 

to EditText in Xml

Upvotes: 0

Kaushik
Kaushik

Reputation: 6162

set gravity top for edittext

<EditText
    android:id="@id/edittext_tweet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/cancel_button"
    android:layout_marginTop="10dp"
    android:background="#ffffff"
    android:gravity="top"
    android:inputType="textMultiLine" />

Upvotes: 0

Emmanuel
Emmanuel

Reputation: 13223

If I understand correctly, what you are seeing is the normal behavior of the EditText. The cursor appears at the center line.

Upvotes: 1

Related Questions