Tag Archives: UI Widgets

radio_androidarena

Hello folks ! working with another UI widget in android application is very easy task. Radio Button is used to quick select and select the choice between two or three. So, just start with Radio Button coding parts . I will show you complete registration form using sqlite later on with my tutorial series. How to make a proper animated registration form I will show you later. But first you should learn UI widgets so that you can fit according to your need.

Tip – While using Radio Button you should use RadioGroup in xml ,that’s a good practice for being a developer .

Download Code

So lets start with designing part . main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CHOICE IS YOURS"
        android:textSize="25sp"/>

    <RadioGroup
        android:id="@+id/group_radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female" />

    </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show me" />

</LinearLayout>

Now , working with MainActivity I am going to give the file name is MyAndroidAppActivity.java.In the Java file I have just defined the below line i.e.

int selectedId = radioSex_Group.getCheckedRadioButtonId();
radioSex_Button = (RadioButton) findViewById(selectedId);
package androidarena.radiobutton;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

	private RadioGroup radioSex_Group;
	private RadioButton radioSex_Button;
	private Button btn_Display;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		checkbutton();

	}

	public void checkbutton() {

		radioSex_Group = (RadioGroup) findViewById(R.id.group_radioSex);
		btn_Display = (Button) findViewById(R.id.btnDisplay);

		btn_Display.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// get selected radio button from radioGroup
				int selectedId = radioSex_Group.getCheckedRadioButtonId();

				// find the radiobutton by returned id
				// you can do any operation here like send values on server or save values on sqlite
				radioSex_Button = (RadioButton) findViewById(selectedId);

				Toast.makeText(MyAndroidAppActivity.this,
						radioSex_Button.getText(), 2000).show();

			}

		});

	}
}

Ouput – radio_button This is the quick and simple Radio Button in android application. You guys can include these two lines as I above mentioned. and can have the access in your app. For more query and suggestions please comment here . 😛

AlertDialogBox

As per the name suggests Alert Dialog Box is same as other programming language . It shows or prompt user that so you really want to exit this application or similar things. Lets Explore – Download Code

1). First of all create new project and in activity_main.xml file their is nothing special but if you want to develop something interesting app then you can customize by your own like on back button press Alert dialog box etc.

2). MainActivity.java , I include AlertDialog.Builder class and via its object I set Message , Title and other properties like  two buttons namely yes or no etc .

MainActivity.java 

package androidarena.alertdialog;

import androidarena.alertdialog.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
		
		builder.setMessage("Do you want to close this app ?")
		    .setCancelable(false)
		    .setPositiveButton("Yeah !", new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int id) {
		    finish();
		    }
		    })
		    .setNegativeButton("No way", new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int id) {
		    //  Action for 'NO' Button
		    dialog.cancel();
		}
		    });
		
		AlertDialog alert = builder.create();
		 alert.setTitle("Alert-BOX");
		 alert.show();
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

Output – alertdialogboxAny query or customization on this tutorial , feel free to share . Happy Coding :p

editextimage

Working with EditText ,TextView and Buttons is simple as simple in Java. How to get values from EditText and how to set OnClickListener to Our Button and set on TextView is simple.How? Lets code –

Download Code

1). First of all create new project and then open its activity_main.xml file and take two Edittext ,TextView with one Button .

2). Now I’am going to show you how to get values from EditText and Onclick of Button and will Toast those Strings and also set those values over TextView.

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <EditText android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:hint="enter name1"
        android:id="@+id/e1"/>
	<EditText android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:hint="enter name2"
        android:id="@+id/e2"
        android:layout_below="@id/e1"/>
	<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click"
    android:id="@+id/b1"
    android:layout_below="@id/e2"/>
	
	<TextView android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:id="@+id/t1"
	    android:text="Result"
	    android:textSize="30sp"
	    android:layout_below="@+id/b1"/>
</RelativeLayout>

Result –

edittext1

Now MainActivity.java file what we have to do is get the values from  EditText via e1.getText() method where e1 is our EditText etc. And set onClickListener on button b1.

MainActivity.java

package androidarena.androideditbutton;

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.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	
	Button b1;
	EditText e1,e2;
	String s1,s2;
	TextView t1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1= (Button)findViewById(R.id.b1);
		e1 = (EditText)findViewById(R.id.e1);
		e2 = (EditText)findViewById(R.id.e2);
		t1= (TextView)findViewById(R.id.t1);
		
		b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				s1=e1.getText().toString();
				s2 = e2.getText().toString();
				
				Toast.makeText(getApplicationContext(), s1+""+s2, 3000).show();
				t1.setText(s1+s2);
			}
		});
	}

	@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;
	}

}

Output –

editext_androidarena

You can customize your java code according to your need like if you want to take this string values from one Activity to another Activity you can use Intent .

For more Queries and if you found any bug do Post here . Happy Coding nerds :p

 

spinner_tutorial

Spinner is a Android Widget which is used to select one value among lot of values given in array.It is very simple to work with. lets code –

Download Code

First of all you have to take spinner widget in activity_main.xml ,

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp" />

</RelativeLayout>

Now in MainActivity.java you have to define a String type Array and adapter and set adapter to spinner .

Make sure your MainActivity.java file should implement AdapterView.OnItemSelectedListener . I take version Array of String type and Adapter aa .

MainActivity.java

package androidarena.spinner;
import androidarena.spinner.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {

	String[] version = { "donut", "cupcake", "kitkat", "jelly bean", "Other",  };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Spinner spin = (Spinner) findViewById(R.id.spinner1);
		spin.setOnItemSelectedListener(this);
		
		ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,version);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spin.setAdapter(aa);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
		Toast.makeText(getApplicationContext(),version[position] ,Toast.LENGTH_LONG).show();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}

Output –

spinner output spinner_output

For more queries and tutorials do post here and comment . Happy coding :p

Option Menu

Android Menus is one of the best Part in Android Apps . As a Android developer we should use it whenever we need Views which are not so important like “About us”,”Like and share” etc. Lets explore –

Download Code

STEPS – 

1) .  create new project in eclipse IDE  , goto File => New => Android Project . Give your desire Project Name .

2). create menu.xml under Layout folder .

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- Single menu item 
		 Set id, icon and Title for each menu item
	-->
    <item android:id="@+id/menu_bookmark"
    	  android:icon="@drawable/icon_bookmark"
          android:title="Bookmark" />
    <item android:id="@+id/menu_save"
    	  android:icon="@drawable/icon_save"
          android:title="Save" />
    <item android:id="@+id/menu_search"
    	  android:icon="@drawable/icon_search"
          android:title="Search" />
    <item android:id="@+id/menu_share"
    	  android:icon="@drawable/icon_share"
          android:title="Share" />
    <item android:id="@+id/menu_delete"
    	  android:icon="@drawable/icon_delete"
          android:title="Delete" />  
    <item android:id="@+id/menu_preferences"
    	  android:icon="@drawable/icon_preferences"
          android:title="Preferences" /> 
</menu>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click on Menu to show Menu Items"
    android:textSize="20dip"
    android:textStyle="bold"
    android:layout_margin="20dip"
    />
</LinearLayout>

AndroidMenusActivity.java is your main activity in which you will inflate the menu .

package androidarena.androidmenus;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    /* Initiating Menu XML file (menu.xml) */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        
        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
        	// Single menu item is selected do something
        	// Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_save:
        	Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_search:
        	Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_share:
        	Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_delete:
        	Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menu_preferences:
        	Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    

}

Final Output

Output

Final Output

 

Now RUN your project and for queries and errors do post happy coding 🙂

android-cover-720x340

DOWNLOAD CODE

Toast in Android is something in  which developer can see their result’s ,ouput’s . lets code ….

1). First take Button in  activity_main.xml . After Taking Button in xml file go for MainActivity.java file .

2) . MainActivity.java

package androidarena.simpletoast;

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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button b = (Button)findViewById(R.id.button1);
		b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Toast.makeText(getApplicationContext(), "WOOOOh !!!! My TOAST IS WORKING", 5000).show();
				
			}
		});
	}

	@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;
	}

}

Output – toast output   For more queries and tutorials visit my other tutorials and do comment and post here 🙂 happy coding :p