0% found this document useful (0 votes)
55 views2 pages

Practical 22

The document contains code snippets for two Android applications, each with an XML layout file and a corresponding Java activity. The first application changes the background color of a layout based on the device's orientation, while the second application lists all available sensors using a ListView. Both applications utilize the AndroidX library and are structured with a main activity class extending AppCompatActivity.

Uploaded by

ristonrodz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views2 pages

Practical 22

The document contains code snippets for two Android applications, each with an XML layout file and a corresponding Java activity. The first application changes the background color of a layout based on the device's orientation, while the second application lists all available sensors using a ListView. Both applications utilize the AndroidX library and are structured with a main activity class extending AppCompatActivity.

Uploaded by

ristonrodz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical 22

Ans 3) activity_main.xml MainActivity.java


<?xml version=”1.0” encoding=”utf-8”?> package com.example.exp22;
<androidx.constraintlayout.widget.ConstraintLayout import
xmlns:android=”http://schemas.android.com/apk/ androidx.appcompat.app.AppCompatActivit
res/android” y;
xmlns:app=”http://schemas.android.com/apk/res- import
auto” androidx.constraintlayout.widget.Constraint
xmlns:tools=”http://schemas.android.com/tools” Layout;
android:layout_width=”match_parent” import android.content.res.Configuration;
android:layout_height=”match_parent” import android.graphics.Color;
android:id=”@+id/Layout” import android.os.Bundle;
tools:context=”.MainActivity”> public class MainActivity extends
</ AppCompatActivity
androidx.constraintlayout.widget.ConstraintLayout> {ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle
savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout =
findViewById(R.id.Layout);
if(getResources().getConfiguration().orienta
tion ==
Configuration.ORIENTATION_PORTRAIT){
constraintLayout.setBackgroundColor( Color
.parseColor(“#2cbdf2”));
}
if(getResources().getConfiguration().orienta
tion ==
Configuration.ORIENTATION_LANDSCAPE)
{ constraintLayout.setBackgroundColor(Col
or.parse
Color”#f083f2”));
}}}
Ans 4) actvity_main.xml MainActivity.java

<?xml version=”1.0” encoding=”utf- package com.example.exp22_1;


8”?> import
<androidx.constraintlayout.widget.Co androidx.annotation.RequiresApi;
nstraintLayout import
xmlns:android=”http:// androidx.appcompat.app.AppCompatA
schemas.android.com/apk/res/ ctivity;
android” import android.content.Context;
xmlns:app=”http:// import android.hardware.Sensor;
schemas.android.com/apk/res-auto” import
xmlns:tools=”http:// android.hardware.SensorManager;
schemas.android.com/tools” import android.os.Build;
android:layout_width=”match_parent import android.os.Bundle;
” import android.widget.ArrayAdapter;
android:layout_height=”match_parent import android.widget.ListView;
” import java.util.List;
tools:context=”.MainActivity”> public class MainActivity extends
<ListView AppCompatActivity
android:id=”@+id/listView1” {SensorManager smm;
android:layout_width=”match_parent List<Sensor> sensor;
” ListView lv;
android:layout_height=”match_parent @RequiresApi(api =
” /> Build.VERSION_CODES.M)
</ @Override
androidx.constraintlayout.widget.Con protected void onCreate(Bundle
straintLayout> savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main
);
smm = (SensorManager)
getSystemService(Context.SENSOR_SE
RVICE);
lv = (ListView) findViewById
(R.id.listView1);
sensor =
smm.getSensorList(Sensor.TYPE_ALL);
lv.setAdapter(new
ArrayAdapter<Sensor>(this,android.R.l
ayout.simple_list_item_1,
sensor));
}
}

You might also like