Tag Archives: Android UI

Check Box in Android UI Control

Check Box is an Android control which has two states on/off that can be toggle by the user. Check Boxes allows the user to select one or more options from a set.Check Boxes Options allow the user to select multiple Items and each check box is manage separately and we must register a click listner for each one.

 

Example of Check Box:-

.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > 

<CheckBox android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />

<CheckBox android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:checked="true" />

<CheckBox android:id="@+id/Matlab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Matlab" />

<Button android:id="@+id/Clickhere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" />

</LinearLayout>

.Java file:

package com.coreprogrammers.checkboxdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;


public class MainActivity extends Activity
{

private CheckBox android, java, Matlab;
private Button Clickhere;

@Override
public void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

android = (CheckBox) findViewById(R.id.android);
java = (CheckBox) findViewById(R.id.java);
Matlab = (CheckBox) findViewById(R.id.Matlab);

Clickhere = (Button) findViewById(R.id.Clickhere);



Clickhere.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{

// Create string buffer to

StringBuffer strbuff = new StringBuffer();

strbuff.append("Android : ").append(android.isChecked());

strbuff.append("\nJava : ").append(java.isChecked());

strbuff.append("\nMatlab:").append(Matlab.isChecked());

Toast.makeText(MainActivity.this, strbuff.toString(),Toast.LENGTH_LONG).show();

}
});



android.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
Toast.makeText(MainActivity.this, "Android is Checked",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Android is UnChecked",Toast.LENGTH_LONG).show();
}

}
});


java.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
Toast.makeText(MainActivity.this, "Java is Checked",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Java is UnChecked",Toast.LENGTH_LONG).show();
}

}
});


Matlab.setOnCheckedChangeListener(new OnCheckedChangeListener()
{

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
Toast.makeText(MainActivity.this, "Matlab is Checked",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Matlab is UnChecked",Toast.LENGTH_LONG).show();
}

}
});






}
}

 

Auto-complete Text View

Auto-complete text view is an edit text that shows a list of suggestions automatically when the user is typing the list of suggestions is displayed in a drop-down menu, the user can choose an item from the drop-down list to replace the content of auto-complete text view.

followings are the attributes of Auto-complete text view:-

  1. android:completionHint :- this defines the hint which is to be displayed in the drop-down menu.
  2. android:completionHintView:– this defines the hint view displays in the drop-down menu.
  3. android:completionThreshhold:- this defines the number of characters that the user must type before completion suggestions are displayed in the drop-down menu.
  4. android:dropDownAnchor:- this is the view to anchor the auto-complete drop-down tool.
  5. android:dropDownHeight:- this specifies basic height of drop-down menu.
  6. android:dropDownHorizontaloffset:- this specifies the amount of pixels by which the drop-down should be off set horizontally.
  7. android:dropDownselector:- this specifies a selector in a drop down list.
  8. android:dropDownVerticaloffset:- this specifies the amount of pixels by off set vertically.
  9. android:dropDownWIdth:- this specifies the basic width of the drop down.
  10. android:popupBackground:- this sets the background of the pop-up.

example of auto-complete text view:-

  .xml file :- 

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="${relativePackage}.${activityClass}" >

<TextView android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:layout_marginTop="15dp" 
android:layout_marginLeft="36dp" 
android:text="what is your favourite programming language" /> 

<AutoCompleteTextView android:id="@+id/autoCompleteTextView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_below="@+id/textView1" 
android:layout_marginLeft="36dp" 
android:layout_marginTop="17dp" 
android:ems="10" 
android:text=""> 

<requestFocus /> 
</AutoCompleteTextView>

</RelativeLayout>

   .java file:- 

package com.coreprogrammers.autocompletetextviewdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class MainActivity extends Activity 
{

String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP","Matlab","R Programming","oracle","My Sql"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

//Creating the instance of ArrayAdapter containing list of language names 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.select_dialog_item,language); 


//Getting the instance of AutoCompleteTextView 
AutoCompleteTextView autoCompleteTextView1= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 

autoCompleteTextView1.setThreshold(2);//will start working from first character 
autoCompleteTextView1.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView 

autoCompleteTextView1.setTextColor(Color.RED); 
}
}

 

 

 

Skip to toolbar