Android Prac 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Bhavesh Patil

6209

Practical 4
Aim: Programs related to different Layouts
a) Linear Layout Example.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="409dp"
android:layout_height="729dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout
Bhavesh Patil
6209

Output:

b) Relative Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<RelativeLayout
android:layout_width="match_parent"
Bhavesh Patil
6209

android:layout_height="match_parent">

<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="50dp"
android:inputType="textPersonName"
android:text="Name" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextTextPersonName2"
android:layout_marginTop="6dp"
android:text="Button" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Bhavesh Patil
6209

c) Table Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<TableLayout
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
Bhavesh Patil
6209

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="User name"
android:text="User name" />
<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:hint="Password"
android:text="Password" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</TableRow>
Bhavesh Patil
6209

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Output:
Bhavesh Patil
6209

d) Absolute Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
Bhavesh Patil
6209

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="44dp"
android:layout_y="157dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="81dp"
android:layout_y="216dp"
android:text="Button" />
</AbsoluteLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Bhavesh Patil
6209

Output:
Bhavesh Patil
6209

e) Frame Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<FrameLayout

android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<TextView

android:id="@+id/textView3"
android:layout_width="117dp"
android:layout_height="44dp"
android:text="Frame Layout"
android:textSize="14sp" />

</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Bhavesh Patil
6209

Output:
Bhavesh Patil
6209

f) List View Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ListView
android:id="@+id/Mylist"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.prac4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity

{ ListView lv;
String[] fruits=new
Bhavesh Patil
6209

String[]{"Apple","Mango","Guava","Watermelon","Jackfruit"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> ad=new
ArrayAdapter<String>(getApplicationContext(),
androidx.appcompat.R.layout.support_simple_spinner_dropdown_it
em,fruits);

lv=(ListView) findViewById(R.id.Mylist);
lv.setAdapter(ad);
}
}

Output:
Bhavesh Patil
6209
Bhavesh Patil
6209

g) Grid View Layout Example.


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:text="TextView" />
<EditText
android:id="@+id/editTextTextPersonName7"
android:layout_width="wrap_content"
Bhavesh Patil
6209

android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text="Button" />
</GridLayout>

Output:
Bhavesh Patil
6209

XML Code
Bhavesh Patil
6209

Practical
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:foregroundTint="@color/white"
android:visibility="visible"
tools:context=".MainActivity"
tools:visibility="visible" >

<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="111dp"
android:layout_height="30dp"
android:layout_row="0"
android:layout_column="0"
android:text="Enter URL"
android:textSize="20sp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text="Search" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Bhavesh Patil
6209

android:layout_row="0"
android:layout_column="1"
android:ems="10"
android:inputType="textPersonName" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Java Code-
package com.example.prac5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText et;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.editTextTextPersonName);
b=(Button) findViewById(R.id.button);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=et.getText().toString();

Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(url));


startActivity(i);
Bhavesh Patil
6209

}});

Output-
Bhavesh Patil
6209

Practical 5(B)
XML1 Code-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://
schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.co
m/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="409d
p"
android:layout_height="729d
p"
android:orientation="vertical
"
tools:layout_editor_absolute
X="1dp"
tools:layout_editor_absolute
Y="1dp">

<EditText
android:id="@+id/editTextTextPe
rsonName"
android:layout_width="match_p
arent"
Bhavesh Patil
6209

android:layout_height="wrap_co
ntent" android:ems="10"
android:inputType="textPersonN
ame" android:text="User Name"
/>

<EditText
android:id="@+id/editTextText
Password"
android:layout_width="match_
parent"
android:layout_height="wrap_
content" android:ems="10"
android:inputType="textPassw
ord" android:text="Password"
/>
Bhavesh Patil
6209

<Button
android:id="@+id/bu
tton"
android:layout_width="matc
h_parent"
android:layout_height="wrap
_content"
android:text="Login" />

<TextView
android:id="@+id/te
xtView"
android:layout_width="match
_parent"
android:layout_height="45dp"
android:textColor="#E80909"
android:textColorLink="#EF0A
0A" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

XML2 Code-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://
schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.co
m/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Bhavesh Patil
6209

tools:context=".MainActivity2">

<TextView
android:id="@+id/text
View2"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_c
ontent" android:textSize="24sp"
tools:layout_editor_absoluteX="
176dp"
tools:layout_editor_absoluteY="
210dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bhavesh Patil
6209

JAVA Code1-
package com.example.prac5b;

import
androidx.appcompat.app.AppCompatActi
vity; import android.content.Intent;
import android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextVie
w;

public class MainActivity extends


AppCompatActivity { EditText et1,et2;
Button b;
TextView t;

@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main
);
Bhavesh Patil
6209

et1=(EditText) findViewById(R.id.editTextTextPersonName);
et2=(EditText) findViewById(R.id.editTextTextPassword);
b=(Button) findViewById(R.id.button);
t=(TextView) findViewById(R.id.textView);

b.setOnClickListener(new
View.OnClickListener() { @Override
public void
onClick(View view) {
String u,p;
u=et1.getText().toStri
ng();
Bhavesh Patil
6209

p=et2.getText().toString();
if(u.equals("Tushar")&&
p.equals("2911")) {
Intent i = new Intent(getApplicationContext(),
MainActivity2.class); i.putExtra("uname", u);
startActivity(i);

}
else
{
t.setText("Invalid User name or password :( ");
}
}
});
}
}

JAVA2 Code-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://
schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.co
m/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<TextView
android:id="@+id/text
View2"
Bhavesh Patil
6209

android:layout_width="wrap_co
ntent"
android:layout_height="wrap_c
ontent" android:textSize="24sp"
tools:layout_editor_absoluteX="
176dp"
tools:layout_editor_absoluteY="
210dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bhavesh Patil
6209

Output-

You might also like