; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

BottomNavigation in Android Using Support Library



Step: 1
======

Add latest support library to build.gradle file under app folder


compile 'com.android.support:design:25.0.0'



Step: 2
======
create an xml file under menu folder for the bottom navigation.

bottom_bar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_hotnews"
android:enabled="true"
android:icon="@drawable/ic_whatshot_white_24px"
android:title="@string/news"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_movies"
android:enabled="true"
android:icon="@drawable/ic_movie_white_24px"
android:title="@string/movies"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_music"
android:enabled="true"
android:icon="@drawable/ic_music_note_white_24px"
android:title="@string/music"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_games"
android:enabled="true"
android:icon="@drawable/ic_games_white_24px"
android:title="@string/games"
app:showAsAction="ifRoom" />


<item
android:id="@+id/action_more"
android:enabled="true"
android:icon="@drawable/ic_more_white_24px"
android:title="More"
app:showAsAction="ifRoom" />


</menu>


Step: 3
======
Create an xml Layout file for the Activity Class

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>


<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/color_selector"
app:itemTextColor="@drawable/color_selector"

app:menu="@menu/bottom_bar_menu" />

</RelativeLayout>



Step: 4
======
Create an Activity Class like below


package com.pratap.bottombar;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

private BottomNavigationView bottomNavigationView;


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

bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);

bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {

case R.id.action_hotnews:
Utils.showToast(MainActivity.this, "News");
break;
case R.id.action_movies:
Utils.showToast(MainActivity.this, "Movies");
break;
case R.id.action_music:
Utils.showToast(MainActivity.this, "Music");
break;
case R.id.action_games:
Utils.showToast(MainActivity.this, "Games");
break;
case R.id.action_more:
Utils.showToast(MainActivity.this, "More");
break;

}
return false;
}
});

}


}


ScreenShot
========
 



Source Code
=========

Download Link


Demo Video
=========















Consuming SOAP Webservice using HttpURLConnection in Android

Consuming SOAP Webservice in Android using HttpUrlConnection.

Create a class like below. Please make sure to call this function in background Thread/AsyncTask/Service.

ServiceHandler.java
====================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
* Created by pratap.kesaboyina on 07-05-2016.
*/
public class ServiceHandler {
    /*
        requestUrl= endpoint
        methodName = namespace+ServiceMethodName
        soapxml= soap xml string
      */
public static ServiceResponse soapWebServiceRequest(String requestUrl, String methodName, String soapBody) {

ServiceResponse serviceResponse = new ServiceResponse();
URL oURL = null;
InputStream inputStream = null;
try {
oURL = new URL(requestUrl);
HttpURLConnection con = (HttpURLConnection) oURL.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
con.setRequestProperty("SOAPAction", methodName);

OutputStream reqStream = con.getOutputStream();
reqStream.write(soapBody.getBytes());

// if 200 , then proceed
int statusCode = con.getResponseCode();

if (statusCode == 200) {

inputStream = con.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

String line = "";
String result = "";

while ((line = bufferedReader.readLine()) != null) {
result += line;
}

/* Close Stream */
if (null != inputStream) {
inputStream.close();
}

serviceResponse.setIsSuccessOrFail(true);
serviceResponse.setXmlResponse(result);
} else {
serviceResponse.setIsSuccessOrFail(false);
serviceResponse.setXmlResponse("Error in Connection");
}

} catch (MalformedURLException e) {
e.printStackTrace();
serviceResponse.setIsSuccessOrFail(false);
serviceResponse.setXmlResponse("Error in Connection");
} catch (ProtocolException e) {
e.printStackTrace();
serviceResponse.setIsSuccessOrFail(false);
serviceResponse.setXmlResponse("Error in Connection");
} catch (IOException e) {
e.printStackTrace();
serviceResponse.setIsSuccessOrFail(false);
serviceResponse.setXmlResponse("Error in Connection");
}


return serviceResponse;


}


}



ServiceResponse.java
==================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
**
* Created by pratap.kesaboyina on 26-08-2015.
*/
public class ServiceResponse {

// is request failed/sucess
private boolean isSuccessOrFail;

// if request success then xmlResponse else contains error msg
private String xmlResponse;


public ServiceResponse() {
}


public ServiceResponse(boolean isSuccessOrFail, String xmlResponse) {
this.isSuccessOrFail = isSuccessOrFail;
this.xmlResponse = xmlResponse;
}


public void setIsSuccessOrFail(boolean isSuccessOrFail) {
this.isSuccessOrFail = isSuccessOrFail;
}

public boolean isSuccessOrFail() {
return isSuccessOrFail;
}


public String getXmlResponse() {
return xmlResponse;
}

public void setXmlResponse(String xmlResponse) {
this.xmlResponse = xmlResponse;
}


}




SFTP file upload android example


Uploading files from Android using SFTP.

Jars files Needed

1) commons-logging-1.2.jar
2) commons-vfs2-2.1.jar
3) jsch-0.1.53.jar

Make sure you call this in a background thread.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.adroitapps.ttcl.utils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import java.io.File;

/**
* Created by pratap.kesaboyina on 24-05-2016.
*/
public class SftpClass {


public static void uploadFile(File file) {


String host = "", username = "",
password = "";


String localFilePath = file.getAbsolutePath();

String fileName = localFilePath.substring(localFilePath.lastIndexOf("/") + 1);

String remoteFilePath = "/foldername/" + fileName;


JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();

Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.put(localFilePath, remoteFilePath);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();

}
}
}



Reference from stackoverflowlink





Android DatePicker Example


Recently, i got a requirement to have multiple datepickers in a single form.
So i decided to create a customview for android datepicker


Here is the code for CustomView extended from EditText


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.pratap.calendarview.views;


import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* Date picker widget.
*
* @author bgamard
*/
public class DatePickerView extends EditText implements DatePickerDialog.OnDateSetListener {

private Date date;

// private Date previousSelectedDate;

public DatePickerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public DatePickerView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttributes();
}

public DatePickerView(Context context) {
super(context);
setAttributes();
}

private void setAttributes() {

setHint("Select Date");
setGravity(Gravity.LEFT | Gravity.CENTER);
setFocusable(false);
// setTextSize(18);
// setPadding(10, 10, 10, 10);

setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
}
DatePickerDialog datePicker = new DatePickerDialog(
DatePickerView.this.getContext(), DatePickerView.this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePicker.setCancelable(false);

// datePicker.setCanceledOnTouchOutside(true);
datePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();

}
}
});


datePicker.show();
}
});
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


Date date = new GregorianCalendar(year, monthOfYear, dayOfMonth).getTime();

setDate(date);
}

public void setDate(Date date) {
if (date != null) {
this.date = date;
SimpleDateFormat newformat = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = newformat.format(date);
setText(formattedDate);
} else {

setText("");
}
}

public Date getDate() {
return date;
}

public Calendar getModifiedDate() {
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
}
return calendar;
}
}



Use it xml Layout like below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:title="Add New Event"
android:titleTextColor="#FFF"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<LinearLayout
android:id="@+id/formLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_margin="16dp"
android:layout_weight="1"
android:orientation="vertical">

<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="Enter Title"
android:textSize="18sp" />

<com.pratap.calendarview.views.DatePickerView
android:id="@+id/startDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="start Date"
android:fontFamily="sans-serif"
android:textSize="18sp" />


<com.pratap.calendarview.views.DatePickerView
android:id="@+id/endDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="end Date"
android:fontFamily="sans-serif"
android:textSize="18sp" />


</LinearLayout>


<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/formLayout"
android:backgroundTint="@color/colorPrimary"
android:text="Save"
android:textColor="#FFF" />

</LinearLayout>




Screenshots:


CalendarView like Google Calendar in Android


I found a great library to add events to our own CalendarView.

Credits : 
github : https://github.com/SundeepK/CompactCalendarView

Step: 1
======
Add Dependency to build.gradle file


dependencies {

compile 'com.github.sundeepk:compact-calendar-view:1.8.3'
}

Step: 2
======

Create an XML Layout like below with CompactCalendarView.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


<com.github.sundeepk.compactcalendarview.CompactCalendarView
android:id="@+id/compactcalendar_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:compactCalendarBackgroundColor="#00bcd4"
app:compactCalendarCurrentDayBackgroundColor="#1a8cd7"
app:compactCalendarCurrentSelectedDayBackgroundColor="#E57373"
app:compactCalendarTextColor="#FFF"
app:compactCalendarTextSize="12sp" />

</LinearLayout>

Step: 3
======
Create an Activity


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.pratap.calendarview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.CalendarDayEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
CompactCalendarView compactCalendarView;

private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("MMMM- yyyy", Locale.getDefault());

private Calendar currentCalender = Calendar.getInstance(Locale.getDefault());

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


toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24px);
actionBar.setDisplayHomeAsUpEnabled(true);
// Setting default toolbar title to empty
actionBar.setTitle(null);


compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
compactCalendarView.drawSmallIndicatorForEvents(true);
compactCalendarView.setUseThreeLetterAbbreviation(true);

//set initial title
actionBar.setTitle(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth()));

//set title on calendar scroll
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override
public void onDayClick(Date dateClicked) {


Toast.makeText(MainActivity.this, "Date : " + dateClicked.toString(), Toast.LENGTH_SHORT).show();

}

@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
// Changes toolbar title on monthChange
actionBar.setTitle(dateFormatForMonth.format(firstDayOfNewMonth));

}

});


addDummyEvents();

// gotoToday();


}

// Adding dummy events in calendar view for April, may, june 2016
private void addDummyEvents() {

addEvents(compactCalendarView, Calendar.APRIL);
addEvents(compactCalendarView, Calendar.MAY);
addEvents(compactCalendarView, Calendar.JUNE);

// Refresh calendar to update events
compactCalendarView.invalidate();
}


// Adding events from 1 to 6 days

private void addEvents(CompactCalendarView compactCalendarView, int month) {
currentCalender.setTime(new Date());
currentCalender.set(Calendar.DAY_OF_MONTH, 1);
Date firstDayOfMonth = currentCalender.getTime();
for (int i = 0; i < 6; i++) {
currentCalender.setTime(firstDayOfMonth);
if (month > -1) {
currentCalender.set(Calendar.MONTH, month);
}
currentCalender.add(Calendar.DATE, i);
setToMidnight(currentCalender);
compactCalendarView.addEvent(new CalendarDayEvent(currentCalender.getTimeInMillis(), Color.argb(255, 255, 255, 255)), false);
}
}


private void setToMidnight(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}


public void gotoToday() {

// Set any date to navigate to particular date
compactCalendarView.setCurrentDate(Calendar.getInstance(Locale.getDefault()).getTime());


}
}

Screenshot
========




Source Code 
==========

Dropbox Link


Demo
====





UPTET news