I am creating an android application in which i have a activity which contain several elements like Switch, TextView etc.
I need to get the values from all Switch elements whether it is checked or not.
it will be great if anyone show me the proper way to transfer multiple values to different activity for further calculation.
Thanks
here is my XML code:
<LinearLayout
android:id="@+id/configSwitchHldr"
android:layout_marginTop="10dp"
android:layout_span="2"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp" >
<TextView
android:text="@string/coreHardware"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textColor="@color/green"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#363636" />
<Switch
android:id="@+id/switch1"
android:layout_width="fill_parent"
android:layout_height="14.5sp"
android:textOn="ON"
android:textOff="OFF"
android:text="CPU"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#363636" />
<Switch
android:id="@+id/switch2"
android:layout_width="fill_parent"
android:layout_height="14.5sp"
android:textOn="ON"
android:textOff="OFF"
android:text="Memory"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#363636" />
<Switch
android:id="@+id/switch3"
android:layout_width="fill_parent"
android:layout_height="14.5sp"
android:textOn="ON"
android:textOff="OFF"
android:text="Storage"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#363636" />
</LinearLayout>
Ok, so based on your comment this is how it could be done:
In your first
Activity, you would need a method, that gathers all the switches states and pass them along to the next activity. Passing parameters along to other Activities can be achieved by putting them inside theIntentyou send to open the newActivity.The method for getting the
Switchesstates and passing them to a newActivitywould roughly look something like this:Put this inside the onClickListener for a
Buttonfor instance like so:Now when you click the
Buttonyou’ll be sent to NewActivity.To retrieve the parameters passed on from the MainActivity, you should do something like this in you onCreate method of the NewActivity:
You could pass the variables on from MainActivity by putting them inside a boolean array, but that is overkill in my opinion.
Also take a look at this SO post: How do I pass data between Activities in Android application? – there’s a lot of these.