In Android ListView we can set background color or background image of an ListItem.
Today we show you how to set background color of an alternate ListView Items.
Also we show you how to set alternate background image of an ListView.
For that you need to understand Simple ListView so first check out our previous posts of ListView download sample source and then modify as per below code.You can find Simple ListView Posts Here
After understanding previous post need to modify code as per below
Look at the getView method of FruitsArrayAdapter.java and write before return rowView write below code.
Android ListView is most powerful component for displaying vertical scrollable list.
For displaying item inside ListView component uses adapter.
For Simple list used Android default adapter and For Customization use Custom adapter.ListView is also useful when displaying grouped data.For group data need Expandabale ListView.
Here we will show you custom Android ListView.
Create Android projects call AndroidCustomListView.For Custom ListView we need one xml file and some of fruit images.You have alredy one XML file so modify name of xml file main.xml to fruits_list.xml and download fruit images and put it into res/drawable folder and modify your XML file as below.
Now, Create one FruitsListAdapter.java file and modify as below. FruitsListAdapter extands ArrayAdapter which has getView() it is used to inflate layout as row so in that we have inflate our row file we have create fruits_list.xml So,Modify your FruitsListAdapter.java as below.
package com.idroid.list.adaptor;
import com.idroid.listview.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FruitsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public FruitsArrayAdapter(Context context, String[] values) {
super(context, R.layout.fruits_list, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fruits_list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.name);
ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("Apple")) {
imageView.setImageResource(R.drawable.apple);
} else if (s.equals("Gosseberry")) {
imageView.setImageResource(R.drawable.gooseberry);
} else if (s.equals("Grape")) {
imageView.setImageResource(R.drawable.grape);
} else if (s.equals("Nectarine")) {
imageView.setImageResource(R.drawable.nectarine);
} else if (s.equals("Watermelon")) {
imageView.setImageResource(R.drawable.watermelon);
} else {
imageView.setImageResource(R.drawable.orange);
}
return rowView;
}
}
So now it time to populate ListView for that you need to create FruitsListActivity.java or you already have one Activity you can modify name of that it's fine.Modify FruitsListActivity.java as below.
Now, Build and Run your project on simulator you should see above images like output and also when you click on any of the row you should see toast message.
Android GridView is most powerful and useful layout.
GriedView is generally used when we want to show images,videos,icons.
Also used to make audio player,video player,image gallary.
Today, We show you how to create Custom GridView Example with Custom Adapter.
Create new Android Project call AndroidGridViewExample,Download 15 images and put into res/drawable folder and create gridlayout.xml file and modify as given below.
Now create one new layout in res/layout folder and give name image.xml file for displaying Full screen image like second image display below and modify source as above.
Now, You need two images on and off state and give name switch_off and switch_on and put in res/drawable folder.And modify main.xml file take ToggleButton and TextView for display on and off state.
For getting toggle switch state need to understand setOnCheckedChangeListener() method. This method is executed when the ToggleButton State change from on to off and visa vars. For that you need to modify MainActivity.java as below.
The purpose of this tutorial is to create custom button in android with custom background colors. The result will be like above image.
There is two way to create such a buttons in android by using images and by using xml file which i have describe here.
Android provides default UI buttons with default background sometimes we need some more custom design so it is look batter in any application User Interface.
Follow below step by step tutorial to create custom button with selected unselected state by xml file.
Create new Android Project call Android Custom Buttons and create btn1.xml file in res/drawable folder and modify as below.
By using this btn1.xml file we create one button by using selector in that we have taken two lt;itemgt; tags one for selected and one for unselected you can set both different color in this file for both the state.
stroke property is for displaying border of buttons also you can set any type of color in stroke.
Now it time to use btn1.xml file so look at your main.xml file and modify as per below code.
Now Modify your MainActivity.java file as per below code.
package com.idroid.custombuttons;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void btnClick(View view) {
switch (view.getId()) {
case R.id.btn1:
showToast("Button1 Action");
break;
default:
showToast("");
break;
}
}
private void showToast(String name) {
Toast.makeText(this, "Button " + name + " is pressed!!!",
Toast.LENGTH_SHORT).show();
}
}
Now compile and run your project on Andoid simulator you can see first button display in below layout.If you want more buttons you can create that much xml files in drawable folder add you can create same layout like display in layout or you can download code from below link.
When user click on application icon application is take some time or couple of seconds to load.
During this time user know your application is loading so that time splash screen is important thing in our application
Many application start with splash screen we know that very well because in Game app and other application have a splash screen.
The purpose of splash screen is to user know that application is loading. Does not give more delay time to load splash screen because user does not want more time to wait for application load.
For Creating splash screen you need one image of launch screen we have taken 480x800 here for our example you can see Android Developer Site for Different sizes of splash for all device resolution.
Follow below step by step process of creating Splash Screen for Android Application
Create new Android Project call Android Splash Screen and put your launch image at res/drawable folder.
Also create three xml file one for splash screen give name splashscreen.xml in res/layout folder and create one new folder in res folder and give name anim and create two xml file called fade_in.xml and fade_out.xml for animation.
In this tutorial we show you how to implement Option Menu in Android SDK.
Option menu is one of the attractive user interface in Android.
Option menu is basically used to provide some help in application,setting part of your application and provide some edit,delete functionality or also you can provide navigation in same Activity or Another Activity.
There is three types of menu available in android
Option Menu
Context Menu
Submenus
For Implementing Option Menu in Android Application Follow below step by step guideline
Create new Android Project and fill all require details.
Look at your android project structure find out res folder in that create one menu folder and create color.xml for creating menu items which is shown in above image and modify as given code.
We are created 5 menu items for option menu as display in above image you can also set icon by android:icon property of menu item and set any android drawable resources.
Now open your MenuActivity.java file and type following code. In Following code each items define by ID and it is identify by ID in switch case statement.As per ID selection we will set color of background resources.
package com.idroid.android.menudemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MenuActivity extends Activity
{
TextView tv;
LinearLayout l;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.txt1);
l=(LinearLayout)findViewById(R.id.LL1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.color, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int color=0;
switch(item.getItemId())
{
case R.id.red:
color=getResources().getColor(R.color.red);
break;
case R.id.green:
color=getResources().getColor(R.color.green);
break;
case R.id.blue:
color=getResources().getColor(R.color.blue);
break;
case R.id.pink:
color=getResources().getColor(R.color.pink);
break;
case R.id.yellow:
color=getResources().getColor(R.color.yellow);
break;
default:
break;
}
l.setBackgroundColor(color);
return true;
}
}
Finally run your project by right click on project and run as Android Application and on Android Simulator select Menu button to launch Option Menu
In this tutorial we show you simple spinner example which allow user to select an item from Drop Down list.
In Spinner display static three image data selection after selecting image name picture1,picture2,picture3 effect on ImageView disply below Spinner or Drop Down list.
In Android use android.widget.Spinner class for Drop Down selection list.
Follow below steps for Android Spinner integration
Create new Android Project and fill all require details.
Open String.xml file under resources folder and modify as below.
Open your main.xml file and simply change design display as above layout one Spinner top of the screen and below display ImageView for that you need to modify main.xml file under layout folder.
Now put three images in res>drawable folder particular size which you want to display and also give proper name for images after that modify your MainActivity.java file given below.
Here we have written my second tutorial about passing intent from one activity to another activity and also pass data to second screen by using intent.putExtra method.
Intent.putExtra method accept many kind of data like String,int,float,...etc.
Using intent we can pass control to another Activity.Bundle can be used to pass data from one Activity to another Activity.
Below we have written code about login activity to pass username to next Android Activity.
Now build and run your project in Android simulator provide username and password user you can see next screen with username If you provide wrong username and password it will show you Tost Message.
This post teach you how to create new android project in Eclipse IDE. For creating android project you have must set up Eclipse Android Development Environment if you have already set up Android Development Environment directly Go through below step otherwise follow This Post
Follow few below steps to Create New Android Application/Project :
Select File > New > Android Project
After Selecting Android > Android Project > Click Next
Now You need to specify below all application content
Application Name : Simply name of your Application.
Package Name : Provide package name as identifier of your project as well as organization Example : com.idroid.livecurrencyexchange all character must be small because it is follow naming conversation of Java
Create Activity (Optional): Name of your launcher activity name which is called first when your project runs Example : MySampleApp. It is also follow java class naming conversation.
Min SDK Version : Minimum API Version in which your application properly run and also run on above version.If it is blank than system automatically defines minSdkVersion in you Manifiest file.
For getting started Android development you need to install first Software Development Kit (SDK) You can download from below link
or you can get from your friends
http://developer.android.com/sdk/
Download Eclipse IDE from below link and make sure you are downloading Eclipse IDE for Java Developers
www.eclipse.org/downloads/
After Downloading both the files now Eclipse IDE and Software Development Kit which is Android SDK now available on you system.
Now you need to set up ADT (Android Development Toolkit) plugin for that start your eclipse IDE and select help => Install New software => Popup will be appear copy and past below url to Work with text box and add it.
On Add Site dialog provide any name or write ADT completion of this process your ADT plugin configure.
https://dl-ssl.google.com/android/eclipse/
Now you need to setup for Software Development Kit (SDK) for that in Eclipse Select Window -> Preferences, and then Android.
and browse SDK path which you have downloaded previously.
Finally you can click on Window -> Android SDK and AVD Manager. In the Installed packages pane, we see the Android SDK Tools we just installed and Also create one (Android Virtual Device)AVD for android application testing purpose.
Simply make Image Gallery using ViewPager.When you slide Finger over image and swipe left to right used ViewPager.Many other controller available but i think it is very easy to maintain and create gallery using ViewPager.
Start new project and give name as ViewPagerDemo and begin your development of your android Gallery. Put your all Gallery images into drawable folder and as of now put four images and give name as a,b,c,d.
Now need to create one helper class so it is helpful to in creation of Gallery give name as ImageSwipeAdapter.java and extends PagerAdapter and modified as below
package com.idroid.viewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageSwipeAdapter extends PagerAdapter {
Context context;
private int[] Images = new int[] {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
ImageSwipeAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return Images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int pad = context.getResources().getDimensionPixelSize(R.dimen.m_padding);
imageView.setPadding(pad, pad, pad, pad);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(Images[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Now start programming for Swipe feature for that modify your main.xml file as below code
Android Services is used to do any background task in current activity.There is two types of
Services available in android.Service class is responsible for using Services in android for that we need to extends Services class in out Custom service.
Bound Service :Service which call indefinitely in between activity
Unbound Service :Service which call at the life span of calling activity
Services has different life cycle from Activity. Services has different method for their life cycle which is startService(),stopService(),onBindService()
This Application Quickly access Call log from your android device and display in android TextView. For that you need to create application give name as myCalllog.
1. You need to look your main.m file first. modify main.m code as given below tack one TextView so we can add call log data in TextView
2.Now you need to change your java file as per our requirement you need to get your TextView by using findViewById method and
also set scroll enable so more record it is scroll so in your onCreate() method put below line of code.
3.You have get your TextView now you need to move forword get call log data in cursor so you need one StringBuffer and Cursor for moving each and every record and get Incoming call,Outgoing call and Miss call put below TextView code
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :")
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir + " \nCall Date:--- " + callDayTime
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
tv.setText(sb);
4.Your Call log application is ready to build now build and run your application check output in your simulator.