Sunday, January 22, 2017

Creating a button in Android Studio


Creating a button in Android Studio



This tutorial is just to show how to create a button in android studio. Whatever you do after the button is clicked is up to you! For this case, we are going to change the text after the button is clicked


Adding Button To The Layout

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:id="@+id/main"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   android:padding="25dp"  
   tools:context="com.masukami.cravetechstuts.cravetechstuts.Main">  
   
   <Button  
     android:text="Switch"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_marginBottom="25dp"  
     android:id="@+id/btnSwitch" />  
   
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:gravity="center_horizontal"  
     android:textSize="25sp"  
     android:text="OFF"  
     android:id="@+id/tvSwitch" />  
   
   
 </LinearLayout>  



Configuring The Button. Lets move to our Main.java

Create the button variable. Dont forget to import the Button class! 
Press Alt+Enter for quick and easy import

 private Button btnSwitch;  

- Implementing the View.OnClickListener
- Set the button variable to the button in the layout

 package com.masukami.cravetechstuts.cravetechstuts;  
   
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
   
 public class Main extends AppCompatActivity implements View.OnClickListener{  
   
   private Button btnSwitch;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     btnSwitch = (Button) findViewById(R.id.btnSwitch);  
     btnSwitch.setOnClickListener(this);  
   }  
   
   @Override  
   public void onClick(View v) {  
       
   }  
 }  

Do whatever you want with the button. For this case, changing the TextView text.

 package com.masukami.cravetechstuts.cravetechstuts;  
   
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.TextView;  
   
 import org.w3c.dom.Text;  
   
 public class Main extends AppCompatActivity implements View.OnClickListener{  
   
   private Button btnSwitch;  
   private TextView tvSwitch;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     btnSwitch = (Button) findViewById(R.id.btnSwitch);  
     tvSwitch = (TextView) findViewById(R.id.tvSwitch);  
     btnSwitch.setOnClickListener(this);  
   }  
   
   @Override  
   public void onClick(View v) {  
     switch (v.getId()){  
       case R.id.btnSwitch:  
         tvSwitch.setText("ON");  
         break;  
     }  
   }  
 }  


That's it! You now have a functional button

0 comments:

Post a Comment

 

Contact Us

Email us: suhailqamilbiz@gmail.com