You are on page 1of 14

Practical 4

XML File :
<?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/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:background="@color/colorBack"

tools:context="com.example.teqip3.ui_widgets.MainActivity">

<ImageView

android:id="@+id/imageView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="0dp"

android:layout_centerHorizontal="true"

android:src="@drawable/widget"/>

<TextView

android:id="@+id/textName"

android:layout_width="82dp"

android:layout_height="wrap_content"

android:text="Name:"

style="@style/CustomFontStyle"

android:layout_marginTop="8dp"

android:layout_below="@id/imageView"

android:layout_marginLeft="5dp"/>

<EditText

android:id="@+id/editTextName"
android:layout_toRightOf = "@+id/textName"

android:layout_marginLeft="10dp"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/imageView"

style="@style/CustomFontStyle"

android:hint="Simple EditText">

</EditText>

<TextView

android:id="@+id/textGender"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Gender:"

style="@style/CustomFontStyle"

android:layout_marginTop="25dp"

android:layout_below="@id/textName"

android:layout_marginLeft="5dp"/>

<RadioGroup

android:id="@+id/radioGroup1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/textName"

android:layout_marginTop="22dp"

android:layout_toRightOf = "@+id/textGender"

android:layout_marginLeft="10dp">

<RadioButton

android:id="@+id/radioButtonMale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

style="@style/CustomFontStyle"

android:text="Male" />
<RadioButton

android:id="@+id/radioButtonFemale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

style="@style/CustomFontStyle"

android:text="Female" />

</RadioGroup>

<TextView

android:id="@+id/textCanSpeak"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Can Speak:"

style="@style/CustomFontStyle"

android:layout_below="@id/radioGroup1"

android:layout_marginLeft="5dp"

android:layout_marginTop="10dp"/>

<CheckBox

android:id="@+id/checkBoxEnglish"

android:layout_toRightOf = "@+id/textCanSpeak"

android:layout_marginLeft="10dp"

android:layout_marginTop="6dp"

android:layout_width="match_parent"

android:layout_below="@id/radioGroup1"

android:layout_height="wrap_content"

style="@style/CustomFontStyle"

android:text="English" />

<CheckBox

android:id="@+id/checkBoxMarathi"

android:layout_toRightOf = "@+id/textCanSpeak"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/checkBoxEnglish"

android:layout_marginLeft="10dp"
style="@style/CustomFontStyle"

android:text="Marathi"/>

<CheckBox

android:id="@+id/checkBoxHindi"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/checkBoxMarathi"

android:layout_toRightOf="@id/textCanSpeak"

android:layout_marginLeft="10dp"

style="@style/CustomFontStyle"

android:text="Hindi"/>

<TextView

android:id="@+id/textHostel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Do You Need Hostel:"

android:layout_marginLeft="5dp"

android:layout_below="@id/checkBoxHindi"

style="@style/CustomFontStyle"

android:layout_marginTop="15dp"/>

<ToggleButton

android:id="@+id/toggleButton"

android:layout_width="50dp"

android:layout_height="30dp"

android:layout_marginLeft="15dp"

android:layout_marginTop="12dp"

android:layout_toRightOf="@id/textHostel"

android:layout_below="@id/checkBoxHindi"

android:background="@drawable/rounded_button"

android:textOff="No"

android:textOn="Yes"

android:textSize="16sp"

android:textColor="@color/colorAccent"
android:textStyle="bold"

android:text="ToggleButton"/>

<TextView

android:id="@+id/textAge"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Age:"

android:layout_marginLeft="5dp"

android:layout_below="@id/textHostel"

style="@style/CustomFontStyle"

android:layout_marginTop="15dp"/>

<SeekBar

android:id="@+id/seekBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="15dp"

android:layout_marginLeft="15dp"

android:layout_toRightOf="@id/textAge"

android:layout_below="@id/toggleButton"/>

<TextView

android:id="@+id/textDate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Your Rating:"

android:layout_marginLeft="5dp"

android:layout_below="@id/seekBar"

style="@style/CustomFontStyle"

android:layout_marginTop="30dp"/>

<RatingBar

android:id="@+id/ratingBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"
android:layout_toRightOf="@id/textDate"

android:layout_below="@id/seekBar"

android:layout_marginLeft="5dp"

android:layout_marginTop="15dp" />

<TextView

android:id="@+id/textCity"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="City:"

android:layout_marginLeft="5dp"

android:layout_below="@id/ratingBar"

style="@style/CustomFontStyle"

android:layout_marginTop="5dp"/>

<ListView

android:id="@+id/Citylist"

android:layout_width="fill_parent"

android:layout_height="100dp"

android:layout_marginLeft="5dp"

android:layout_toRightOf="@id/textCity"

android:divider="#f00"

android:dividerHeight="1dp"

android:textSize="8sp"

android:textStyle="bold"

android:background="@drawable/rounded_button"

android:listSelector="@color/colorPrimaryDark"

android:layout_below="@id/ratingBar"/>

<Button

android:id="@+id/btnSubmit"

android:text="Submit"

android:layout_width="80dp"

android:layout_height="25dp"

style="@android:style/Widget.Button"

android:background="@drawable/rounded_button"
android:textSize="16sp"

android:textColor="@color/colorAccent"

android:textStyle="bold"

android:layout_toRightOf="@id/textCity"

android:layout_below="@id/textCity"

android:layout_marginLeft="200dp"

android:layout_marginTop="100dp"

android:layout_marginBottom="0dp" />

</RelativeLayout>

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>

<shape android:shape="rectangle">

<solid android:color="#ffffff"/>

<corners android:radius="8dp" />

<gradient

android:type="linear"

android:startColor="#FFffffff"

android:endColor="#FF008000"

android:angle="45"/>

</shape>

</item>

</selector>

 strings.xml
<resources>

<string name="app_name">UI Widgets</string>

<string-array name="cities_array">

<item>Aurangabad</item>

<item>Pune</item>
<item>Nagpur</item>

<item>Mumbai</item>

<item>Hyderabad</item>

</string-array>

</resources>

 styles.xml
<resources>

<!-- Base application theme. -->

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<!-- Customize your theme here. -->

<item name="colorPrimary">@color/colorPrimary</item>

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

<item name="colorAccent">@color/colorAccent</item>

</style>

<style name="CustomFontStyle">

<item name="android:typeface">serif</item>

<item name="android:textSize">15sp</item>

<item name="android:textStyle">bold</item>

<item name="android:textColor">#990000</item>/>

</style>

</resources>

Java File :
import android.support.v7.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.CheckBox;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.RadioGroup;
import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import android.widget.ToggleButton;

import android.widget.SeekBar;

import android.widget.RatingBar;

import android.view.WindowManager;

import static android.R.attr.value;

public class MainActivity extends AppCompatActivity

private EditText nameTextBox;

private RadioGroup radioGroup1;

private RadioButton radioButtonGender;

private Button btnSubmit;

private CheckBox checkboxEnglish,checkboxHindi,checkboxMarathi;

private ToggleButton toggleButtonNeedHostel;

private SeekBar AgeSeekBar;

private RatingBar ratingbar;

private ListView listView;

String[] listItem;

String listItemValue;

Integer age;

StringBuilder result;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Get full screen

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParam

s.FLAG_FULLSCREEN);

getSupportActionBar().hide();

nameTextBox = (EditText) findViewById(R.id.editTextName);


radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);

btnSubmit = (Button) findViewById(R.id.btnSubmit);

checkboxEnglish = (CheckBox) findViewById(R.id.checkBoxEnglish);

checkboxMarathi = (CheckBox) findViewById(R.id.checkBoxMarathi);

checkboxHindi = (CheckBox) findViewById(R.id.checkBoxHindi);

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

AgeSeekBar=(SeekBar)findViewById(R.id.seekBar);

ratingbar=(RatingBar)findViewById(R.id.ratingBar);

listView=(ListView)findViewById(R.id.Citylist);

result = new StringBuilder();

//Add listener to radiobutton

//ListView

listItem = getResources().getStringArray(R.array.cities_array);

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, android.R.id.text1, listItem);

listView.setAdapter(adapter);

//Listview listener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int position,

long l)

// TODO Auto-generated method stub

listItemValue=adapter.getItem(position);

Toast.makeText(getApplicationContext(),listItemValue,Toast.LENGTH_SHORT).show();

});

btnSubmit.setOnClickListener(new View.OnClickListener()

@Override

public void onClick(View view)


{

result.delete(0,result.length()); //Clear buffer

result.append("Entered Details:\nName: ");

//Get text from Name textbox

result.append(nameTextBox.getText()+"\n");

//Get selected redio button text

int selectedId = radioGroup1.getCheckedRadioButtonId();

radioButtonGender = (RadioButton) findViewById(selectedId);

result.append("Gender: " + radioButtonGender.getText()+"\n");

//Get selected languages

result.append("Can speak: " );

if(checkboxEnglish.isChecked())

result.append(checkboxEnglish.getText()+" ");

if(checkboxMarathi.isChecked())

result.append(checkboxMarathi.getText()+" ");

if(checkboxHindi.isChecked())

result.append(checkboxHindi.getText()+" ");

//Get Toggle Button text

result.append("\nDo you Need Hostel:

").append(toggleButtonNeedHostel.getText());

//Get age

result.append("\nAge:" + String.valueOf(age));

//Get rating value

result.append("\nYour Rating:" + String.valueOf(ratingbar.getRating()));


//Get city

result.append("\nCity: " + listItemValue);

Toast.makeText(getApplicationContext(), result.toString(),

Toast.LENGTH_LONG).show();

});

//Seekbar Listener

AgeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()

@Override

public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

Toast.makeText(getApplicationContext(),"seekbar progress: "+progress,

Toast.LENGTH_SHORT).show();

age = progress;

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

//Toast.makeText(getApplicationContext(),"seekbar touch started!",Toast.LENGTH_SHORT).show();

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

//Toast.makeText(getApplicationContext(),"seekbar touch stopped!",Toast.LENGTH_SHORT).show();

});

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.smssendingapp">

<uses-permission android:name="android.permission.SEND_SMS"/>

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

Outputs :
Result :
Hence, we have created an android application with UI controls/Widgets
with event handling.

You might also like