Skip to main content

Posts

Showing posts from 2014

WeekView in Android

Hello Friends,    Have you Searching for Android calender with WeekView.  Today I am sharing android tutorial for android custom calendar WeekView. WeekView.java public class WeekView extends ActionBarActivity implements OnItemClickListener, OnClickListener { private GridView mGrid; private GregorianCalendar mCalendar; private Date[] mWeek; private TextView mMonthText; private RelativeLayout mArrowRight; private RelativeLayout mArrowLeft; private CalendarAdapter mAdapter; private SimpleDateFormat mFormatMonth = new SimpleDateFormat("MMMM"); private SimpleDateFormat mFormatDay = new SimpleDateFormat("d"); private SimpleDateFormat mFormatYear = new SimpleDateFormat("yyyy"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_week_view); mGrid=(GridView)findViewById(R.id.gridview); mGrid.s...

Pull links from string in Android

Sample code: String Message="http://indianexpress.com/article/sports/football/mexico-holds-host-brazil-to-0-0-draw-at-world-cup/ http://indianexpress.com/article/sports/cricket/suresh-raina-surprised-by-stuart-binnys-performance/ http://indianexpress.com/article/sports/football/fifa-world-cup-every-match-is-like-a-final-for-us-says-andres-iniesta/" ArrayList<String> forrmatted=pullLinks(Message);  for(int i=0;i<forrmatted.size();i++){       String temp=forrmatted.get(i).toString();       Message=Message.replace(temp, "["+temp+"]"); } private ArrayList<String> pullLinks(String text) {  ArrayList<String> links = new ArrayList<String>();  String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";  Pattern p = Pattern.compile(regex);  Matcher m = p.matcher(text);  while(m.find()) {     String urlStr = m.group(); ...

Get domain name from URL in android

Sample Code: String url="http://sports.in.msn.com/football-world-cup-2014/world-cup-animals-5"; String hostName=getDomainName(url); private CharSequence getDomainName(String shareURL) throws URISyntaxException {    URI uri = new URI(shareURL);    String domain = uri.getHost();     return domain; } above method return host name from given URL. You can get more from URL using below methods. uri.getProtocol(); uri.getAuthority(); uri.getHost(); uri.getPort(); uri.getPath(); uri.getQuery(); uri.getFile(); Output: protocol = http authority = sports.in.msn.com host = sports.in.msn.com port = -1 path = /football-world-cup-2014/world-cup-animals-5 query = null filename = /football-world-cup-2014/world-cup-animals-5

Custom Toast in Android

What is Toast? A toast is a view containing a quick little message for the user. When the view is shown to the user, appears as a floating view over the application. It will never receive focus.  Normal syntax of Toast is Toast.makeText(context, text, duration).show(); context the context to use. Usually your android.app.Application or android.app.Activity object. text The text to show. Can be formatted text. duration How long to display the message. Either LENGTH_SHORT or LENGHT_LONG. Here is the example: Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show(); Now let's start with custom toast. Create an Android Project: CustomToastActivity.java public class CustomToastActivity extends ActionBarActivity implements OnClickListener { Button customToast; Button showToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_toast); s...

Check internet connectivity in Android

Write a class which check internet connectivity in android mobile ConncetionDetector.class public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context){ this._context = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);  if (connectivity != null)   {  NetworkInfo[] info = connectivity.getAllNetworkInfo();  if (info != null)   for (int i = 0; i < info.length; i++)   if (info[i].getState() == NetworkInfo.State.CONNECTED)  {  return true;  }  }  return false; } } Activity: public class MainActivity extends ActionBarActivity { ConnectionDetector cd; public static boolean isInternetPresent = false; TextView info; @Override pr...

Load images from external storage into GridView with checkbox in Android using Picasso library

Activity: public class Gallery extends ActionBarActivity { public GridView imagegrid; private ArrayList<String> imageUrls; private ImagesAdapter imageAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery); init(); } private void init() { final String[] columns= {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}; final String orderBy= MediaStore.Images.Media.DATE_TAKEN; Cursor imageCursor= this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy +" DESC"); this.imageUrls=new ArrayList<String>(); for(int i=0; i< imageCursor.getCount();i++){ imageCursor.moveToPosition(i); int dataColumnIndex =imageCursor.getColumnIndex(MediaStore.Images.Media.DATA); imageUrls.add(imageCursor.getString(dataColumnIndex)); }   imagegrid = (GridView) f...

Load images from external storage into GridView in Android using Picasso library

Activity: public class Gallery extends ActionBarActivity { public GridView imagegrid; private ArrayList<String> imageUrls; private ImagesAdapter imageAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery); init(); } private void init() { final String[] columns= {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}; final String orderBy= MediaStore.Images.Media.DATE_TAKEN; Cursor imageCursor= this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy +" DESC"); this.imageUrls=new ArrayList<String>(); for(int i=0; i< imageCursor.getCount();i++){ imageCursor.moveToPosition(i); int dataColumnIndex =imageCursor.getColumnIndex(MediaStore.Images.Media.DATA); imageUrls.add(imageCursor.getString(dataColumnIndex)); }   imagegrid = (GridVi...

Android: Simple ListView using ArrayAdapter

Activity: public class SimpleListView extends ActionBarActivity implements OnItemClickListener { ListView simpleListView; String[] versions={"Cupcake (1.5)","Doughnut (1.6)","Eclair (2.0–2.1)", "Froyo (2.2–2.2.3)","Gingerbread (2.3–2.3.7)","Honeycomb (3.0–3.2.6)", "Ice Cream Sandwich (4.0–4.0.4)","Jelly Bean (4.1–4.3)","KitKat (4.4–4.4.3)" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_character_count); simpleListView=(ListView)findViewById(R.id.listView1); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,versions); simpleListView.setAdapter(adapter); simpleListView.setOnItemClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R....