Saturday, 18 October 2014

ExpandableListView


>extends ListView

>This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

>Following is an demo application: Click Here to download source code

Main Activity

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

ExpandableListAdapter expandableListAdapter;
ExpandableListView expandableListView;
List<String> parentListHeader;
HashMap<String, List<String>> childListData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initializeDisplayElements();
prepareExpandableListData();

expandableListAdapter = new ExpandableListAdapter(this,
parentListHeader, childListData);
expandableListView.setAdapter(expandableListAdapter);

expandableListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
parentListHeader.get(groupPosition)
+ " : "
+ childListData.get(
parentListHeader.get(groupPosition))
.get(childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});

}

/**
* This method is used to prepare the expandable list data parent and
* corresponding child items.
*/
private void prepareExpandableListData() {
parentListHeader = new ArrayList<String>();
childListData = new HashMap<String, List<String>>();

// adding parent data
parentListHeader.add("Micromax");
parentListHeader.add("Moto");
parentListHeader.add("Samsung");

// adding child data
List<String> micromax = new ArrayList<String>();
micromax.add("micromax Canvas Nitro");
micromax.add("micromax Canvas Knight");
micromax.add("micromax gold");

List<String> moto = new ArrayList<String>();
moto.add("moto-G");
moto.add("moto-E");
moto.add("moto-G2");

List<String> samsung = new ArrayList<String>();
samsung.add("samsung grand");
samsung.add("samsung Note 2");
samsung.add("samsung Galaxy");

childListData.put(parentListHeader.get(0), micromax);
childListData.put(parentListHeader.get(1), moto);
childListData.put(parentListHeader.get(2), samsung);
}

/**
* This method is used to initialize display elements present on layout
*/
private void initializeDisplayElements() {
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);

}
}

Tuesday, 15 July 2014

Android APL Levels And Versions

What is API Level?


API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform.
The Android platform provides a framework API that applications can use to interact with the underlying Android system. The framework API consists of:
  • A core set of packages and classes
  • A set of XML elements and attributes for declaring a manifest file
  • A set of XML elements and attributes for declaring and accessing resources
  • A set of Intents
  • A set of permissions that applications can request, as well as permission enforcements included in the system
Platform Version
API Level
VERSION_CODE
Notes
9
8
7
6
5
4
3
2
Android 1.0
1

Friday, 4 July 2014

Speech To Text

speech to text conversion feature quite creatively. In today’s world of Siri, voice commands are of utmost importance. Android natively provides feature of Speech to Text so why not to use it in our app!

At the end of this post you can download the source code


Step 1: Create Basic Android Project

Go to New > Project > Android Project. Give the project name as SpeechToText


Step 2: Create an Layout



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Speach To Text Example"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/speakButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Speak" />
    <TextView
        android:id="@+id/displayTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

Step 3: Android Java Code 


package com.example.activity;
import java.util.ArrayList;
import com.example.speechtotext.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SpeechActivity extends Activity {
TextView displayText;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.speech_layout);

displayText = (TextView) findViewById(R.id.displayTextView);

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

speakButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

try {
startActivityForResult(in, 1);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(
getApplicationContext(),
"sorry, your device doesn't support speech to text feature",
Toast.LENGTH_LONG).show();
}

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

displayText.setText(text.get(0));
}
}
super.onActivityResult(requestCode, resultCode, data);
}

}

The Final Result looks like this


  

Download the source code from below link

DOWNLOAD NOW