4 Constraint Layout




Algorithm:
Step 1) Create a new Android Studio Project

Step 2)Goto Project Structure and open activity_main.xml file
Step 3) Create an EditText in Activity_main.xml file or drag and drop from the palette. Step 4) Add android:onClick=”show” attribute to Button in the activity_main.xml file.
Implement the show method created in MainActivity.java file.

     public void show(View view) {
    }


Step 5) Add EditText and Button constraints to left, right, top and bottom of the parent view.

Step 6) Run the application from the menu bar select Run and Click on Run App option. Program:
Activity_main.xml

<?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">

<EditText android:id="@+id/editText"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="329dp" android:layout_marginEnd="97dp" android:layout_marginStart="72dp" android:layout_marginTop="137dp"
android:ems="10" android:inputType="textPersonName" android:hint="Enter you name" app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.165" />

<Button android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="260dp" android:layout_marginEnd="148dp" android:layout_marginStart="148dp" android:text="Button" android:onClick="show"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>


Program:

package com.example.constraintlayout;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity { EditText editText;
Button button; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
public void show(View view) { editText=(EditText)findViewById(R.id.editText); String name=editText.getText().toString(); Toast.makeText(getApplicationContext(),
name,Toast.LENGTH_SHORT).show();
}
}

OUTPUT:


Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

19 SQLIte SImple Database Example