Nothing Special   »   [go: up one dir, main page]

Unit3 Imp Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

1.

Togglebutton
<!-- activity_main.xml -->

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

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;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ToggleButton toggleButton = findViewById(R.id.toggleButton);

// Set a listener to respond to the toggle button state changes

toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

// Handle when the toggle button is in the ON state

showToast("Toggle Button is ON");

} else {

// Handle when the toggle button is in the OFF state

showToast("Toggle Button is OFF");

});

private void showToast(String message) {


Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

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;

public class MainActivity extends AppCompatActivity {


CheckBox checkBox;
TextView textView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox=findViewById(R.id.ch1);
textView=findViewById(R.id.tx1);
if(checkBox.isChecked()){
textView.setVisibility(View.VISIBLE);

}
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);
}
}
});

}
}

3.Radio Button and Radio Group


<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<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;

public class MainActivity extends AppCompatActivity {


RadioGroup radio;
RadioButton radioButton;
TextView txt;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radio = findViewById(R.id.radioGroup);

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;

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;


private TextView progressText;
private Button startButton;

private int progressStatus = 0;


private Handler handler = new Handler(Looper.getMainLooper());

@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();
}
});
}

private void startProgressBar() {


progressStatus = 0;
progressBar.setProgress(progressStatus);
progressText.setText("0%");

new Thread(new Runnable() {


@Override
public void run() {
while (progressStatus < 100) {
progressStatus += 1;

// Update the progress bar and text on the UI thread


handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progressStatus);
progressText.setText(progressStatus + "%");
}
});

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

<!-- activity_main.xml -->

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

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

<?xml version="1.0" encoding="utf-8"?>

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

In the MainActivity.java file

// MainActivity.java

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ListView listView;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

listView = findViewById(R.id.listView);

// Array of countries

String[] countries = {"USA", "Canada", "UK", "Australia", "Germany", "France", "Japan",


"India", "China"};

// Create an ArrayAdapter to populate the ListView

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.list_item,


R.id.textView, countries);

// Set the adapter for the ListView

listView.setAdapter(adapter);

7.GRIDVIEW
<!-- activity_main.xml -->

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

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:

<!-- grid_item.xml -->

<?xml version="1.0" encoding="utf-8"?>

<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;

public class MainActivity extends AppCompatActivity {


private GridView gridView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gridView = findViewById(R.id.gridView);

// Array of image resources

int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3,

R.drawable.image4, R.drawable.image5, R.drawable.image6,

R.drawable.image7, R.drawable.image8, R.drawable.image9};

// Create a custom adapter to populate the GridView with images

ImageAdapter adapter = new ImageAdapter(this, images);

// Set the adapter for the 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;

public class ImageAdapter extends BaseAdapter {

private Context context;

private int[] images;


public ImageAdapter(Context context, int[] images) {

this.context = context;

this.images = images;

@Override

public int getCount() {

return images.length;

@Override

public Object getItem(int position) {

return null;

@Override

public long getItemId(int position) {

return 0;

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ImageView imageView;

if (convertView == null) {

// If the view is not recycled, create a new ImageView

imageView = new ImageView(context);

imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));


imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

} else {

// If the view is recycled, reuse it

imageView = (ImageView) convertView;

}
// Set the image resource based on the position

imageView.setImageResource(images[position]);

return imageView;

8.DATE AND TIME PICKER


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="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;

public class MainActivity extends AppCompatActivity {


private DatePicker datePicker;
private TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = findViewById(R.id.datePicker);
timePicker = findViewById(R.id.timePicker);
}

public void showDateTime(View view) {


int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1; // Month is zero-based
int year = datePicker.getYear();

int hour = timePicker.getHour();


int minute = timePicker.getMinute();

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();
}
}

9.CUSTOM TOASTE ALERT


MAIN.XML

<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show toast"
android:id="@+id/btn"
android:gravity="center"
></Button>

</LinearLayout>

CUSTOM_TOASTE.XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="50dp"

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();
}
}

You might also like