From 5a3c4fe219104322ae6ab49a678eae723d67b58d Mon Sep 17 00:00:00 2001 From: student Date: Wed, 16 Mar 2016 14:47:18 -0500 Subject: [PATCH] Create a switch that changes text from "On" to "Off" --- .idea/.name | 2 +- .idea/misc.xml | 40 +++++++++++++++++++ .idea/modules.xml | 2 +- .idea/vcs.xml | 2 +- .../pbuskell/widgetdemo/WidgetDemo.java | 29 ++++++++++++++ .../main/res/layout/activity_widget_demo.xml | 29 ++++++++++++++ app/src/main/res/values/colors.xml | 3 ++ app/src/main/res/values/strings.xml | 2 +- 8 files changed, 105 insertions(+), 4 deletions(-) diff --git a/.idea/.name b/.idea/.name index b8c8381..80e2a37 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -WidgetDemo \ No newline at end of file +WidgetDemo1.0 \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index e284b1d..7158618 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,30 @@ + + + + @@ -19,4 +43,20 @@ + + + + + 1.8 + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index d18de73..52961d5 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 6564d52..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/com/example/pbuskell/widgetdemo/WidgetDemo.java b/app/src/main/java/com/example/pbuskell/widgetdemo/WidgetDemo.java index 2ff0b3f..600a1f4 100644 --- a/app/src/main/java/com/example/pbuskell/widgetdemo/WidgetDemo.java +++ b/app/src/main/java/com/example/pbuskell/widgetdemo/WidgetDemo.java @@ -2,12 +2,41 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.widget.CompoundButton; +import android.widget.Switch; +import android.widget.TextView; public class WidgetDemo extends AppCompatActivity { + Switch swtchStatus1; + TextView txtvwStatus1; + String switchOn = "Switch is ON"; + String switchOff = "Switch is OFF"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_widget_demo); + + swtchStatus1 = (Switch) findViewById(R.id.swtchStatus1); + txtvwStatus1 = (TextView) findViewById(R.id.txtvwStatus1); + + swtchStatus1.setChecked(true); + swtchStatus1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { + if (bChecked) { + txtvwStatus1.setText(switchOn); + } else { + txtvwStatus1.setText(switchOff); + } + } + }); + if (swtchStatus1.isChecked()) { + txtvwStatus1.setText(switchOn); + } else { + txtvwStatus1.setText(switchOff); + } + } } diff --git a/app/src/main/res/layout/activity_widget_demo.xml b/app/src/main/res/layout/activity_widget_demo.xml index 5bcdf80..71475a2 100644 --- a/app/src/main/res/layout/activity_widget_demo.xml +++ b/app/src/main/res/layout/activity_widget_demo.xml @@ -9,4 +9,33 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.pbuskell.widgetdemo.WidgetDemo"> + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..bc45f54 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,7 @@ #3F51B5 #303F9F #FF4081 + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ab30965..e599672 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - WidgetDemo + Toggle Switch