Unit3 Imp Programs
Unit3 Imp Programs
Unit3 Imp Programs
Togglebutton
<!-- activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:checked="false"
android:layout_centerInParent="true"/>
</RelativeLayout>
// MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
if (isChecked) {
} else {
});
2.Chechbox
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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">
<CheckBox
android:id="@+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkbox"
android:textStyle="bold"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/tx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="text here" />
</LinearLayout>
Main.java
package com.example.prc_11;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
}
else {
textView.setVisibility(View.GONE);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
textView.setVisibility(View.VISIBLE);
}
else {
textView.setVisibility(View.GONE);
}
}
});
}
}
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="@+id/rd1"
></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="@+id/rd2"></RadioButton>
</RadioGroup>
<TextView
android:id="@+id/tx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:textSize="20dp"></TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:text="apply"
android:id="@+id/bt1"></Button>
</LinearLayout>
Main.java
package com.example.prc12;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
submit = findViewById(R.id.bt1);
txt = findViewById(R.id.tx1);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int ID = radio.getCheckedRadioButtonId();
radioButton=findViewById(ID);
txt.setText("selected : "+radioButton.getText());
}
});
}}
4.Progressbar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:max="100"
android:progress="0" />
<TextView
android:id="@+id/progressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="0%"
android:textSize="18sp" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progressText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Start Progress" />
</RelativeLayout>
Main.java
package com.example.prc_13;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
progressText = findViewById(R.id.progressText);
startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startProgressBar();
}
});
}
try {
// Simulate a delay to represent a task being performed
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
5.SCROLLVIEW
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 12" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
6.LISTVIEW
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
</RelativeLayout>
Create a new layout file list_item.xml for the list item layout:
<!-- list_item.xml -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textColor="#000000"
// MainActivity.java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
@Override
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
// Array of countries
listView.setAdapter(adapter);
7.GRIDVIEW
<!-- activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:columnWidth="100dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"/>
</RelativeLayout>
Create a new layout file grid_item.xml for the grid item layout:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
In the MainActivity.java file, add logic to populate the GridView with a list of images:
// MainActivity.java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridView);
gridView.setAdapter(adapter);
Create a custom ImageAdapter class to handle the grid item layout and image resources:
// ImageAdapter.java
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
this.context = context;
this.images = images;
@Override
return images.length;
@Override
return null;
@Override
return 0;
@Override
ImageView imageView;
if (convertView == null) {
} else {
}
// Set the image resource based on the position
imageView.setImageResource(images[position]);
return imageView;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="100dp"
android:layout_height="100dp"/>
<Button
android:layout_marginTop="30dp"
android:id="@+id/showDateTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Date and Time"
android: > </LinearLayout>
</RelativeLayout>
MAIN.JAVA
package com.example.prc16_date_time_picker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
String dateTime = "Selected Date and Time: " + day + "/" + month + "/" + year + " " + hour + ":" +
minute;
// You can use the dateTime variable as needed (e.g., display in a TextView)
Toast.makeText(this, dateTime, Toast.LENGTH_SHORT).show();
}
}
</LinearLayout>
CUSTOM_TOASTE.XML
android:id="@+id/toast">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is custom text"
android:textSize="20dp"></TextView>
</LinearLayout>
</RelativeLayout>
MAIN.JAVA
package com.example.prac_15;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"this is default text",Toast.LENGTH_SHORT).show();
// CustomToast();
}
});
}
private void CustomToast(){
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.custom_toaste,this.findViewById(R.id.toast));
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
}