activity class on off

Upload: prasath676303

Post on 02-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Activity Class on Off

    1/5

    Activity class

    Let's write the code to check which toggle button is ON/OFF.

    File: MainActivity.java

    1. package com.example.togglebutton;

    2.3. import android.os.Bundle;

    4. import android.app.Activity;

    5. import android.view.Menu;6. import android.view.View;

    7. import android.view.View.OnClickListener;

    8. import android.widget.Button;

    9. import android.widget.Toast;10.import android.widget.ToggleButton;

    11.

    12.public class MainActivity extends Activity {

    13. private ToggleButton toggleButton1, toggleButton2;14. private Button buttonSubmit;

    15. @Override

    16. protected void onCreate(Bundle savedInstanceState) {17. super.onCreate(savedInstanceState);

    18. setContentView(R.layout.activity_main);

    19.

    20. addListenerOnButtonClick();21. }

    22.

    public void addListenerOnButtonClick(){23.

    //Getting the ToggleButton and Button instance from the layout xml file24. toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);

    25. toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);

    26. buttonSubmit=(Button)findViewById(R.id.button1);27.

    28. //Performing action on button click

    29. buttonSubmit.setOnClickListener(new OnClickListener(){

    30.31. @Override

    32. public void onClick(View view) {

    33. StringBuilder result = new StringBuilder();

    34.

    result.append("ToggleButton1 : ").append(toggleButton1.getText());35. result.append("\nToggleButton2 : ").append(toggleButton2.getText());

    36. //Displaying the message in toast

    37. Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();

    38. }

    39.

  • 8/10/2019 Activity Class on Off

    2/5

    40. });

    41.

    42. }43. @Override

    44. public boolean onCreateOptionsMenu(Menu menu) {

    45.

    // Inflate the menu; this adds items to the action bar if it is present.46. getMenuInflater().inflate(R.menu.activity_main, menu);47. return true;

    48. }

    49.50.}

    download this example

    Output:

    Next TopicAndroid Checkbox Example

    http://www.javatpoint.com/src/android/togglebutton.ziphttp://www.javatpoint.com/src/android/togglebutton.ziphttp://www.javatpoint.com/android-checkbox-examplehttp://www.javatpoint.com/android-checkbox-examplehttp://www.javatpoint.com/android-checkbox-examplehttp://www.javatpoint.com/src/android/togglebutton.zip
  • 8/10/2019 Activity Class on Off

    3/5

    Android Custom Toast Example

    You are able to create custom toast in android. So, you can display some images likecongratulations or loss on the toast. It means you are able to customize the toast now.

    activity_main.xml

    Drag the component that you want to display on the main activity.

    File: activity_main.xml

    1.

    6.

    7.

    13.

    14.

    customtoast.xml

    Create another xml file inside the layout directory. Here we are having ImageView andTextView in this xml file.

    File: customtoast.xml

    1.

    2.

    9.

  • 8/10/2019 Activity Class on Off

    4/5

    10.

    16.

    17.

    23.

    Activity class

    Now write the code to display the custom toast.

    File: MainActivity.java

    1.

    package com.example.customtoast2;

    2.

    import android.os.Bundle;

    3.

    import android.app.Activity;

    4.

    import android.view.Gravity;

    5.

    import android.view.LayoutInflater;

    6.

    import android.view.Menu;

    7.

    import android.view.View;

    8.

    import android.view.ViewGroup;

    9.

    import android.widget.Toast;

    10.

    11.

    public class MainActivity extends Activity {

    12.

    @Override

    13.

    public void onCreate(Bundle savedInstanceState) {

    14.

    super.onCreate(savedInstanceState);

    15.

    setContentView(R.layout.activity_main);

    16.

    17.

    //Creating the LayoutInflater instance18.

    LayoutInflater li = getLayoutInflater();

    19.

    //Getting the View object as defined in the customtoast.xml file

    20.

    View layout = li.inflate(R.layout.customtoast,

    21.

    (ViewGroup) findViewById(R.id.custom_toast_layout));

    22.

    23.

    //Creating the Toast object

    24.

    Toast toast = new Toast(getApplicationContext());

  • 8/10/2019 Activity Class on Off

    5/5

    25.

    toast.setDuration(Toast.LENGTH_SHORT);

    26.

    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

    27.

    toast.setView(layout);//setting the view of custom toast layout

    28.

    toast.show();

    29.

    }

    30.

    @Override

    31.

    public boolean onCreateOptionsMenu(Menu menu) {

    32.

    getMenuInflater().inflate(R.menu.activity_main, menu);

    33.

    return true;

    34.

    }

    35.

    36.

    }

    download this example

    Output:

    Next TopicAndroid ToggleButton Example

    http://www.javatpoint.com/src/android/customtoast2.ziphttp://www.javatpoint.com/src/android/customtoast2.ziphttp://www.javatpoint.com/android-togglebutton-examplehttp://www.javatpoint.com/android-togglebutton-examplehttp://www.javatpoint.com/android-togglebutton-examplehttp://www.javatpoint.com/android-togglebutton-examplehttp://www.javatpoint.com/src/android/customtoast2.zip