Friday, May 25, 2012

Show the Value of RatingBar in Android

Example show when we rate it simultaneously show the value of rating.
<ratingbar android:id="@+id/ratingBar1" android:layout_height="wrap_content" android:layout_width="wrap_content">
</ratingbar>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
In setOnRatingBarChangeListener event set the value of RatingBar in TextView
final RatingBar rb=(RatingBar)findViewById(R.id.ratingBar1);
final TextView tv=(TextView)findViewById(R.id.textView1); rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
{
// TODO Auto-generated method stub tv.setText(String.valueOf(rating));
}
});

Country/State Example using Spinner in Android

This is a Example of country state. When i select country in one spinner it should populate other spinner with its states
<Spinner android:layout_width="match_parent" android:id="@+id/spinner1" android:layout_height="wrap_content" android:entries="@array/country">
</Spinner>
<Spinner android:layout_width="match_parent" android:id="@+id/spinner2" android:layout_height="wrap_content"></Spinner>
I used strings.xml page you will get in value folder to populate country spinner using string-array
<string-array name="country">
<item>USA</item>
<item>INDIA</item>
<item>SRILANKA</item>
</string-array>
Now code to display state when we select country.

Button nt=(Button)findViewById(R.id.button1);
final Spinner sp=(Spinner)findViewById(R.id.spinner1);
final Spinner sp1=(Spinner)findViewById(R.id.spinner2);
nt.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
List<String> l=new ArrayList<String>();
if(sp.getSelectedItem().toString().equals("USA"))
{

l.add("NEWYORK");
l.add("WASHINTON");

}
else if(sp.getSelectedItem().toString().equals("INDIA"))
{
l.add("gujarat");
l.add("bihar");
}
else if(sp.getSelectedItem().toString().equals("SRILANKA"))
{
l.add("colombo");
}
ArrayAdapter<String> ary=new ArrayAdapter<String>(Example1.this, android.R.layout.simple_spinner_item,l);
ary.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(ary);
}
});

Intend Example in Android

Here in this Example i am showing the use of intend(redirecting to another page and opening url)
<Button android:text="next page" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
<Button android:text="uripage" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
Now find the id of button, set the intend in setOnClickListener event
Code for Redirecting to one page to another page
final Context context=this;
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() 

@Override 
public void onClick(View v)
{ // TODO Auto-generated method stub 
Intent intent=new Intent(context,Example1.class);
startActivity(intent); 
}
});
Code for opening the webpage
Button b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener()
{
@Override 
public void onClick(View v)
{ // TODO Auto-generated method stub
Intent browseintent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
startActivity(browseintent);
}
});
Set the activity in manifest page

<activity android:label="@string/app_name" android:name=".Example1">
</activity>

Monday, May 7, 2012

Append two string one after each other

Android : Android is an open source software stack that include the operating system, middleware and key application along with set of API Libraries for writing mobile application that can shape the look,feel and function of mobile handsets.


3 technique is use for interapplication communication.


1. Notification
2. Intents
3. Content Provider
1-> Is a standard way in which mobile device traditionally alert users (by vibration,flash the device led etc)
2-> Provide mechanism for message passing within and between application
3-> It manage access to your application private database


Example.: Append two string one after each other.
main.xml
<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="@string/hello"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:orientation="horizontal">
<TextView android:text="Enter Your Name:" android:id="@+id/lblfname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/txtfname" android:layout_weight="1">
            <requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:orientation="horizontal">
<TextView android:text="Enter Your Last Name:" android:id="@+id/lbllname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/txtlname" android:layout_weight="1">
           <requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="horizontal">
 <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
 <RadioButton android:layout_width="wrap_content" android:id="@+id/radio0" android:text="Appenddirectly" android:layout_height="wrap_content" android:checked="true"></RadioButton>
 <RadioButton android:layout_width="wrap_content" android:id="@+id/radio1" android:text="Appendindirectly" android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<Button android:text="Display" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>