Using SeekBar

For adding a seekbar to your layout file (res/layout/*.xml)

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

For adding the functionality part of the SeekBar, copy and paste the below code in your onCreate() function of JAVA File.

Note: Below the SetContentView() line in onCreate() function.

		SeekBar sb = (SeekBar) findViewById(R.id.seekBar1);
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "stop"+Integer.toString(progressed),
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				progressed = progress;
			}
		});

Note: Make sure you declare a variable in Java Class file. Before the function and inside the class file at top.

int progressed = 0;

Save the changes and run your Android Application in Emulator or an Android Device. Post your comments.

Advertisements

Using Radio Group and Radio Button

For adding a group of radio buttons to your layout file (res/layout/*.xml)

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="One" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Two" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Three" />
    </RadioGroup>

For adding the functionality part of the Radio Group and Radio Button, copy and paste the below code in your onCreate() function of JAVA File.

Note: Below the SetContentView() line in onCreate() function.

		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
		rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				//int selection = rg.getCheckedRadioButtonId();
				RadioButton rb = (RadioButton) findViewById(checkedId);
				Toast.makeText(MainActivity.this, rb.getText() ,
						Toast.LENGTH_SHORT).show();
			}
		});

Save the changes and run your Android Application in Emulator or an Android Device. Post your comments.

Using Switch Controls

A Switch is a two-state toggle switch widget that can select between two options. Its similiar to Wireless on/off method.

For adding a Switch to your layout file (res/layout/*.xml)

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch" />

For adding the functionality part of the Switch, copy and paste the below code in your onCreate() function of JAVA File.

Note: Below the SetContentView() line in onCreate() function.

		Switch sw = (Switch) findViewById(R.id.switch1);
		sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					Toast.makeText(MainActivity.this, "true",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "false",
							Toast.LENGTH_SHORT).show();
				}
			}
		});

Note: Make sure you change the minSdkVersion to 14 in AndroidManifest.xml file.

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

Save the changes and run your Android Application in Emulator or an Android Device. Post your comments.

Using Toggle Button

A toggle button allows the user to change a setting between two states.

For adding a ToggleButton to your layout file (res/layout/*.xml)

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

For adding the functionality part of the ToggleButton, copy and paste the below code in your onCreate() function of JAVA File.

Note: Below the SetContentView() line in onCreate() function.

		ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
		tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					Toast.makeText(MainActivity.this, "true",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "false",
							Toast.LENGTH_SHORT).show();
				}
			}
		});

Save the changes and run your Android Application in Emulator or an Android Device. Post your comments.