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

Soham Kolhatkar - Assignment 4 - Designing Your User Interface With Views

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

Roll no: 13027

SetA1

MainActivity.java
package com.example.contact_view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

EditText etxtname;
TextView txtsh;
Button btn1;
String[] names= {"Soham","Abhishek","Aditya","Lieke"};
Spinner s1;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SetUpAdapter();
SetUpSpinner();
}

private void SetUpSpinner()


{
s1 =(Spinner)findViewById(R.id.spn1);
s1.setAdapter(aa);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
txtsh = (TextView) findViewById(R.id.textView1);

switch (s1.getSelectedItem().toString())
{
case "Soham": txtsh.setText("Name: Soham Kolhatkar\n Contact number:
9175024088\n E-mail ID: qwertyu@gmail.com");
break;
case "Abhishek":txtsh.setText("Name: Abhishek Datar\n Contact number:
9856324775\n E-mail ID: asdfghjk@gmail.com");

break;
case "Aditya": txtsh.setText("Name: Aditya Bachal\n Contact number:
87463251055\n E-mail ID: zxcvbnm@gmail.com");
break;
case "Lieke": txtsh.setText("Name: Lieke Martens\n Contact number:
9876543210\n E-mail ID: poiuyt@gmail.com");
break;
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
}

private void SetUpAdapter()


{
aa = new ArrayAdapter<>(this,R.layout.spinner_layout,R.id.textViewspn1, names);
aa.setDropDownViewResource(R.layout.spinner_layout);
}

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:id="@+id/txtshow1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="ExtraText">

<Spinner

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMe
nu"
android:id="@+id/spn1"
android:layout_width="match_parent"
android:layout_height="41dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="0dp"
android:layout_marginBottom="607dp"
android:paddingLeft="20dp"
android:hint="Contact name"
tools:layout_editor_absoluteY="415dp" />

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="162dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginBottom="156dp"
android:textColor="@color/black" />

</RelativeLayout>

spinner_layout.xml

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


<RelativeLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewspn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="4dp"
android:layout_marginBottom="7dp"
android:text=""

/>

</RelativeLayout>

OUTPUT:
SetA2
MainActivity.java

package com.example.addspinner;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {


Button add, del;
Spinner sp1;
EditText ilist;
TextView txtdel;
List<String> items = new ArrayList<>();
String[] itemarr;
ArrayAdapter aa;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeArray();
setupAdapter();
setupSpinner();

private void makeArray() {


ilist = (EditText)findViewById(R.id.itemlist);
items.add(ilist.getText().toString());
Object[] obj = items.toArray();
itemarr = Arrays.copyOf(obj,obj.length,String[].class);
}

private void unmakeArray() {


txtdel = (TextView)findViewById(R.id.textView3);
ilist = (EditText)findViewById(R.id.itemlist);
if(items.contains(ilist.getText().toString())) {
items.remove(ilist.getText().toString());
Object[] obj = items.toArray();
itemarr = Arrays.copyOf(obj,obj.length,String[].class);
txtdel.setText("");
}
else
txtdel.setText("No Such item");

}
private void setupSpinner()
{
sp1 = (Spinner)findViewById(R.id.spinner);
sp1.setAdapter(aa);

sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

Toast.makeText(getApplicationContext(),sp1.getSelectedItem().toString(),Toast.LENGTH_LON
G).show();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
}

private void setupAdapter()


{
aa = new ArrayAdapter<>(this,R.layout.spinner_layout,itemarr );
aa.setDropDownViewResource(R.layout.spinner_layout);
}

public void delspin(View view) {


unmakeArray();
Toast.makeText(getApplicationContext(),"Hello delete",Toast.LENGTH_LONG).show();
setupAdapter();
setupSpinner();
}

public void addspin(View view) {


makeArray();
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_LONG).show();
setupAdapter();
setupSpinner();
}
}

activity_main.xml
<?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">

<EditText
android:id="@+id/itemlist"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="109dp"
android:layout_marginTop="116dp"

android:inputType="textPersonName"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="212dp"
android: > android:text="ADD"
tools:layout_editor_absoluteX="29dp"
tools:layout_editor_absoluteY="125dp" />

<Button
android:id="@+id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="212dp"
android:layout_marginEnd="20dp"
android: > android:text="DELETE"
tools:layout_editor_absoluteX="294dp"
tools:layout_editor_absoluteY="125dp" />

<Spinner
android:id="@+id/spinner"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="291dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="325dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="263dp"
android:layout_marginEnd="18dp"
android:text=""
android:textColor="#FB0000" />
</RelativeLayout>
spinner_layout.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:layout_marginTop="4dp"
android:layout_marginBottom="7dp"
>

</TextView>

OUTPUT:
SetA3
MainActivity.java

package com.example.dateandtime;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.sql.Time;
import java.text.*;
import java.time.Year;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


Button DateButton, TimeButton;
TextView Datetxt, Timetxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUIviews();
DateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datehandlebutton();
}
});
TimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timehandlebutton();
}
});
}
private void timehandlebutton() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int min = calendar.get(Calendar.MINUTE);

boolean is24HourFormat = DateFormat.is24HourFormat(this);


TimePickerDialog timePickerDialog = new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int h, int m) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR,h);
calendar1.set(Calendar.MINUTE,m);

CharSequence timeCS = DateFormat.format("hh:mm a",calendar1);


Timetxt.setText(timeCS);
}
}, hour, min, is24HourFormat);
timePickerDialog.show();
}

private void datehandlebutton() {


final Calendar calendar = Calendar.getInstance();
int year= calendar.get(Calendar.YEAR);
int month= calendar.get(Calendar.MONTH);
int date= calendar.get(Calendar.DATE);

DatePickerDialog datePickerDialog = new DatePickerDialog(this, new


DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int y, int m, int d) {
String dstring = d+" / "+m+" / "+y;
Datetxt.setText(dstring);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR,y);
calendar1.set(Calendar.MONTH,m);
calendar1.set(Calendar.DATE,d);

CharSequence dateCharSequence = DateFormat.format("EEEE, dd MMM


yyyy",calendar1);
Datetxt.setText(dateCharSequence);
}
}, year, month, date);
datePickerDialog.show();
}

private void setUIviews() {


DateButton = (Button)findViewById(R.id.sdate);
TimeButton = (Button)findViewById(R.id.stime);
Datetxt = (TextView)findViewById(R.id.txtdate);
Timetxt = (TextView)findViewById(R.id.txttime);
}
}

activity_main.xml

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

<Button
android:id="@+id/stime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txttime"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Set Time" />

<Button
android:id="@+id/sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtdate"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Set Date" />

<TextView
android:id="@+id/txttime"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_marginTop="99dp"
android:text=""
android:textSize="16sp"
android:textColor="@color/black"
android:paddingLeft="20dp"
/>

<TextView
android:id="@+id/txtdate"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_below="@+id/stime"
android:layout_centerVertical="true"
android:layout_marginTop="107dp"
android:text=""
android:textSize="16sp"
android:textColor="@color/black"
android:paddingLeft="20dp"
/>

</RelativeLayout>

OUTPUT:

You might also like