How do I place Edittext in the middle of the screen?

Asked

Viewed 430 times

1

I used Gravity.CENTER, what centered the text in Edittext and not Edittext in Activity.

package com.example.wbsoftware.testetcc;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

    private LinearLayout layout;
    private EditText[] vetorEdits;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        layout = (LinearLayout) findViewById(R.id.layout);

        int count = getIntent().getIntExtra("qtd", 0);
        vetorEdits = new EditText[count];


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

            vetorEdits[i] = new EditText(Main2Activity.this);
            vetorEdits[i].setHint("Periodo " + (i+1));
            vetorEdits[i].setHintTextColor(Color.WHITE);
            vetorEdits[i].setGravity(Gravity.CENTER);
            vetorEdits[i].setTextColor(Color.WHITE);


            layout.addView(vetorEdits[i], new ViewGroup.LayoutParams(300,150));


        }

    }

}

1 answer

4


When you call vetorEdits[i].setGravity(Gravity.CENTER); , you are centering only the content of EditText

To center itself EditText in relation to the LinearLayout, you must do the following

layout.setGravity(Gravity.CENTER);

In this way, you determine that the elements within the LinearLayout must be centralised

  • Thanks, I got!

  • @Hacker If the response of the regmoraes solved your problem, mark his answer as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.