How to put EditText in the middle of the screen? [duplicate]

1

I used Gravity.CENTER , which 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));


        }

    }

}
    
asked by anonymous 11.09.2017 / 03:38

1 answer

4

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

To center the EditText itself against LinearLayout , you should do the following

layout.setGravity(Gravity.CENTER);

In this way, you determine that the elements within LinearLayout should be centralized

    
11.09.2017 / 03:48