Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Center a Button in a Linear Layout in Android?

Improve Article
Save Article
Like Article
  • Last Updated : 17 Jul, 2022
Improve Article
Save Article
Like Article

LinearLayout is a view group that aligns all children vertically or horizontally in a single direction. The “android:orientation” parameter allows you to define the layout direction. A button is a component of the user interface that can be tapped or clicked to carry out an action. Nowadays while developing one of our applications, we wanted to have center-aligned buttons, while in one application we were using LinearLayout and It’s Looking Pretty Nice For Our Application.

We will be building a simple application in which we will be displaying a button in the center of the screen with LinearLayout.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio. The code can be implemented in both Java and Kotlin Programming Language for Android.

Step 2: Working with the activity_main.xml File

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:gravity="center" >
    <!-- Button centering in screen -->
        android:text="GeeksForGeeks" />

Since there is no change in the Java/Kotlin MainActivity File, we’ve only provided the XML File Code.


Center Button in Linear Layout


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!