android linear layout center horizontal and vertical example
A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:
- Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.
- Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.
for the more information, linear layout visit the google developer: https://developer.android.com/
following the android linear layout verticle and horizontal example:
1.MainActivity.java:
package com.akash.linear_layout;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnloginv,btnregisterv,btnresetv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnloginv=(Button)findViewById(R.id.btnloginv);
btnregisterv=(Button)findViewById(R.id.btnregisterv);
btnresetv=(Button)findViewById(R.id.btnresetv);
btnloginv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
Intent i =new Intent(MainActivity.this,Horizontal.class);
startActivity(i);
}
});
btnregisterv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
}
});
btnresetv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
}
});
}
}
2.activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/btnloginv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>
<Button
android:id="@+id/btnregisterv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
<Button
android:id="@+id/btnresetv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
3.Horizontal.java:
package com.akash.linear_layout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Horizontal extends AppCompatActivity {
Button btnlogin,btnregister,btnreset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal);
btnlogin=(Button)findViewById(R.id.btnlogin);
btnregister=(Button)findViewById(R.id.btnregister);
btnreset=(Button)findViewById(R.id.btnreset);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
}
});
btnregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
}
});
btnreset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// put here your code for button
}
});
}
}
4.activity_horizontal:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
<Button
android:id="@+id/btnregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"/>
<Button
android:id="@+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.linear_layout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Horizontal"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment