Pages

Showing posts with label Button. Show all posts
Showing posts with label Button. Show all posts

Friday, 14 March 2014

Android: Custom Toast Messages.

In last tutorial we saw how to use Toast Messages, and now we will see how to customize Toast messages.
Output will be as:
Android Custom Toast

Xml file:
res/layout
1. activity_main.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
    >  
    <Button   
      android:id="@+id/button1"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="Android Toast"  
      />  
   </LinearLayout>  
2. toast.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/toast_layout"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="horizontal" >  
    <ImageView android:src="@drawable/rating_good"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         />  
   <TextView android:id="@+id/tv1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        />  
 </LinearLayout>  
Here we have defined a layout for our custom Toast. Also don't forget to add an image in your drawable folder.(res/drawable).

Java file:
src/
 package com.example.toastexample;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.ViewGroup;  
 import android.widget.Button;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           b1=(Button) findViewById(R.id.button1);  
           b1.setOnClickListener(this);  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.button1:  
                LayoutInflater inflater = getLayoutInflater();  
                View layout = inflater.inflate(R.layout.toast,  
                                (ViewGroup) findViewById(R.id.toast_layout));//referencing toast layout  
                TextView text1 = (TextView) layout.findViewById(R.id.tv1);  
                text1.setText("Well Done");  
                Toast toast = new Toast(getApplicationContext());  
                toast.setDuration(Toast.LENGTH_LONG);  
                toast.setView(layout);  
                toast.show();  
                break;  
           }  
      }  
 }  
Here we have referenced our custom toast layout(toast.xml) and added that layout on our toast message.

Android: Using Toast Messages.

Android Toast is a little message used to provide feedback to the user. Today in this tutorial we will see how to make use of these Toast messages.

Output will be as:
 
Android Toast Message


Xml file:
res/layout/
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
    >  
    <Button   
      android:id="@+id/button1"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="Android Toast"  
      />  
   </LinearLayout>  

Java file:
src/
 package com.example.toastexample;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.Toast;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           b1=(Button) findViewById(R.id.button1);  
           b1.setOnClickListener(this);  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.button1:  
                Toast.makeText(getApplicationContext(), "Android Toast Message", Toast.LENGTH_SHORT).show();  
                //Display Toast message  
                break;  
           }  
      }  
 }  
Here Toast.makeText() method is used to make a Toast message which takes Context, Message and the duration for which the toast is to be displayed. And then show() method is used to display the Toast Message.

Tuesday, 4 March 2014

Android: Using ListView

In last tutorial we saw how to add click event to buttons and now in this tutorial we will see how to use a listview.

Output will be as:

XML file: res---layout
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
       android:orientation="vertical"  
  >  
 <EditText   
   android:id="@+id/etText"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   android:hint="Enter text"  
   />  
 <Button   
   android:id="@+id/bAdd"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
      android:text="Add"    
   />  
 <ListView   
   android:id="@+id/listDisplay"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   >  
 </ListView>  
 </LinearLayout>     

In our layout file we have added one EditText, Button and a ListView.
Clicking a button will take the user data and add it to a ListView.

Java file: src---your package name
 import java.util.ArrayList;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1;  
      ListView list;  
      EditText et;  
      private ArrayAdapter<String> adapter;  
      private ArrayList<String> items;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);//referencing UI  
           b1=(Button) findViewById(R.id.bAdd); //referencing button  
           list=(ListView) findViewById(R.id.listDisplay); //referencing listview  
           et=(EditText) findViewById(R.id.etText); //referencing edittext  
           b1.setOnClickListener(this); //adding click event to button  
           items=new ArrayList<String>();  
           adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);  
           //Adapter used to bind array to the listview  
           list.setAdapter(adapter);//applying adapter to listview  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.bAdd:  
                items.add(0, et.getText().toString());//getting data from edittext  
                adapter.notifyDataSetChanged();//refereshes the adapter (listview)  
                et.setText("");  
                break;  
           }  
      }  
 }  
Here we have used an ArrayAdapter which will bind the array(i.e ArrayList) to the ListView.
The onClick method does the job of retrieving user data from EditText and adding it to the ArrayList and the function notifyDataSetChanged() is used to refresh the adapter.

Manifest file:
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.listvieweg"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.listvieweg.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  
No need to make any changes in a Manifest file.

Test your application and if you have any doubts feel free to ask me in the comments below.

Saturday, 1 March 2014

Android: Using Buttons with Click Events

In this tutorial we will create two buttons and on pressing one of the button, TextView displays the text as which button is pressed.

Output will be as:



Xml file: res---layout
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  
  <Button   
    android:id="@+id/button1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Button 1"  
    />  
  <Button   
    android:id="@+id/button2"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="Button 2"  
    />  
  <TextView   
    android:id="@+id/textview1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text=""  
    />  
 </LinearLayout>  

First, Layouts are used to define how your views(i.e Buttons, TextViews, etc) will be positioned on the screen. For these, different layouts are used such as LinearLayout, RelativeLayout, GridLayout, etc.
Here we are using LinearLayout, in LinearLayout views can be either placed in vertical or horizontal format.
So android:orientation="vertical" under LinearLayout defines that the views will be placed vertically.
Next we have used Buttons and TextView, for any view id,width and height are the basic attributes.
Id of the view is further used in java file as a reference to the view defined in xml.
Width and Height defines the width and height of a view.Either you can put width and height in dp for e.g android:width="20dp" or you can use wrap_content,fill_parent or match_parent. fill_parent and match_parent does almost same thing that it will fit the screen and wrap_content means that it will be wrapped in minimum possible space.

Java file: src---your package name
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.TextView;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1,b2;  
      TextView tv1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           b1=(Button) findViewById(R.id.button1);//referencing button defined in xml file  
           b2=(Button) findViewById(R.id.button2);  
           tv1=(TextView) findViewById(R.id.textview1);//referencing textview  
           b1.setOnClickListener(this); //setting on click event on button  
           b2.setOnClickListener(this);  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){//v.getId() for getting id of the pressed button  
           case R.id.button1:  
                //button1 pressed  
                tv1.setText("Button 1 pressed");//set text of textview  
                break;  
           case R.id.button2:  
                tv1.setText("Button 2 pressed");  
                break;  
           }  
      }  
 }  
A java file placed under src---your package name must extend an Activity.
Here as we have to implement click event so we will have to implement OnClickListener and then in onClick method we can add the functionality that will be performed on a click event.

Manifest file: (AndroidManifest.xml)
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.example"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.example.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Whenever a new Activity is created it should be declared in a manifest file.
An Activity should include a name and intent filters,intent filters are basically used to define the role of activity as in our case when our application is launched so the activity that needs to be displayed first should be mentioned as a launcher activity for e.g android:name="android.intent.category.LAUNCHER" under the category.Also the action names can be used to navigate between different activities which we will see in our further tutorials.

So that's it we have created an application having buttons with click events. If you have any doubts feel free to ask me in the comments below.