/*
* Iterator traverse elements only in one direction i.e forward
* ListIterator traverse elements in both directions i.e forward and backward
* Using ListIterator we can modify the existing list as well as done in below sample
*/
public class IteratorClass extends Activity
{
ArrayList<String> miteratorList;
Iterator<String> mIterator;
ListIterator<String> mListIterator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initiViews();
}
private void initiViews()
{
//Initializing the ArrayList
miteratorList=new ArrayList<String>();
//Adding values to the ArrayList
miteratorList.add("A");
miteratorList.add("B");
miteratorList.add("C");
miteratorList.add("D");
miteratorList.add("E");
//method to traverse elements using Iterator here
usingIterator();
//Initializing the mListIterator here
mListIterator=miteratorList.listIterator();
//method to traverse and modify elements using mListIterator here
usingListIterator();
//method to traverse elements using Iterator here
usingIterator();
usingListIteratorTOTraverseBackward();
}
private void usingIterator()
{
//Initializing the mIterator here
mIterator=miteratorList.iterator();//Returns an iterator on the elements of this list.
while(mIterator.hasNext())
{
Object element = mIterator.next();
System.out.print(element + " ");
}
//O/P goes like this => 04-29 18:32:44.590: I/System.out(26302): A B C D E
System.out.println();
}
private void usingListIterator()
{
//Modifying the existing list using list iterator
while (mListIterator.hasNext()) {
Object element=mListIterator.next();
mListIterator.set(element+"Z");
}
System.out.println();
//04-29 18:45:44.588: I/System.out(1605): AZ BZ CZ DZ EZ
}
private void usingListIteratorTOTraverseBackward()
{
// Now, display the list in reverse order using ListIterator
while(mListIterator.hasPrevious()) {
Object element = mListIterator.previous();
System.out.print(element + " ");
}
System.out.println();
// 04-29 18:52:30.478: I/System.out(5954): EZ DZ CZ BZ AZ
}
}
Thursday, 30 April 2015
Difference between Iterator and List Iterator Example
Wednesday, 22 April 2015
Clearing/Deleting Webview's Web Storage in Android
Sometimes our webview load's previously loaded url when we are saving cookies and maintaining cache etc.now what happens is webview maintains web storage for these loaded pages.In order to load new url everytime we've to clear/delete this web storage.
Now in order to do this we've to write this code :
Now in order to do this we've to write this code :
WebStorage webStorage = WebStorage.getInstance(); webStorage.deleteAllData(); wvAppWebView.reload(); wvAppWebView.loadUrl(URL);
Monday, 20 April 2015
Login Via Instagram and getting user profile info Android
Generally we get an requirement to login via instagram in our android app. So how to do the i'm going to describe here :
Download and include instagramlib.jar in libs
Note : get your scerets keys from instagram developer when Registering your app.
Download and include instagramlib.jar in libs
Follow the code and you are done :
1. MainActivity.java
public class MainActivity extends Activity {
private InstagramApp mApp;
private Button btnConnect,btnshowpofile;
private TextView tvSummary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instagram_main);
mApp = new InstagramApp(this, ApplicationData.CLIENT_ID,
ApplicationData.CLIENT_SECRET, ApplicationData.CALLBACK_URL);
mApp.setListener(listener);
tvSummary = (TextView) findViewById(R.id.tvSummary);
btnConnect = (Button) findViewById(R.id.btnConnect);
btnshowpofile = (Button) findViewById(R.id.btnshowpofile);
btnshowpofile.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openInstagram(MainActivity.this, mApp.getUserName());
}
});
btnConnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view)
{
if (mApp.hasAccessToken()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Disconnect from Instagram?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
mApp.resetAccessToken();
btnConnect.setText("Connect");
tvSummary.setText("Not connected");
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
else
{
mApp.authorize();
}
}
});
if (mApp.hasAccessToken())
{
tvSummary.setText("Connected as " + mApp.getUserName());
btnConnect.setText("Disconnect");
}
}
OAuthAuthenticationListener listener = new OAuthAuthenticationListener() {
@Override
public void onSuccess() {
tvSummary.setText("Connected as " + mApp.getUserName());
btnConnect.setText("Disconnect");
}
@Override
public void onFail(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
}
};
private void openInstagram(Context con,String mUserName) {
try {
Intent iIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
iIntent.setData(Uri.parse("instagram://user?username="+mUserName));
con.startActivity(iIntent);
}
catch (Exception e) {
con.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/"+mUserName)));
}
}
}
Note : get your scerets keys from instagram developer when Registering your app.
2. Instragramkeys.java
public class Instragramkeys{
public static final String CLIENT_ID = "your client id here";
public static final String CLIENT_SECRET = "your client secret id here";
public static final String CALLBACK_URL = "instagram://connect";
}
XML Layout:
1. instagram_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="40dip"
android:text="Connect" />
<Button
android:id="@+id/btnshowpofile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="40dip"
android:text="showprofile" />
<TextView
android:id="@+id/tvSummary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dp"
android:text="Not connected" />
</LinearLayout>
2. mainifestfile
<uses-permission android:name="android.permission.INTERNET" />
output Screens :
Sunday, 19 April 2015
Post a Picture using Facebook sdk 4.01 Android
Use this Method to post Image on facebook using facebook 4.01 SDK. To login follow previous tutorials.
private void postPhoto() {
Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);
SharePhoto sharePhoto = new SharePhoto.Builder().setBitmap(image).build(); // oR SharePhoto sharePhoto = new SharePhoto.Builder().setImageUrl("path").build();
ArrayList<SharePhoto> photos = new ArrayList<>();
photos.add(sharePhoto);
SharePhotoContent sharePhotoContent =
new SharePhotoContent.Builder().setRef("Testing picture post").setPhotos(photos).build();
ShareApi.share(sharePhotoContent, shareCallback);
}
Thursday, 16 April 2015
Posting a status update on Facebook using 4.01
- Declare these variables
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private CallbackManager callbackManager;private ShareDialog shareDialog;
- initialize callbackmanager
callbackManager = CallbackManager.Factory.create();
- Now Call this method to share content on facebook (including text,url etc)
private void postStatusUpdate() {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(
"The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/docs/android"))
.build();
ShareApi.share(linkContent, shareCallback);
}
Facebook Login with Facebook 4.01 SDK Released recently
There are many changes in new sdk release by facebook for app
integration.New Facebook sdk has simplified many things from older versions.
So what we have to do to integrate facebook in our app with new sdk
follows :
Download the facebook sdk from this link :
Include latest Facebook sdk in your workspace.Some libraries you may
have to include in order to make sdk run.that will be ;
1. android-support-v4.jar
2. bolts-android-1.1.2.jar (this can be found from older sdk libs
folder).
clean the imported sdk and all set now.
now create your android project and include facebook sdk in your project
and finally your project structure will go like this :
now from code side .
1. MainActivity.java
public class MainActivity extends ActionBarActivity {
Button mFacebookloginbtn;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initiViews();
}
private void initiViews() {
mFacebookloginbtn=(Button)findViewById(R.id.Facebookloginbtn);
FacebookSdk.sdkInitialize(MainActivity.this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e("loginresult", loginResult.toString());
if(Profile.getCurrentProfile()!=null){
Log.e("Pofile",Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}else{
Profile.fetchProfileForCurrentAccessToken();
Log.e("Pofile",Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
mFacebookloginbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,Arrays.asList("public_profile", "user_friends"));
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
1. activity_main.xml
Output Screen :
Things to be noticed
here are :
1.LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,
Arrays.asList("public_profile",
"user_friends"));
: this lone
will make us start with login in facebook.
Define your app
permissions here read,write,publish etc.
2. FacebookSdk.sdkInitialize(MainActivity.this.getApplicationContext());
: This line
will initialize our facebook sdk and Initialize our
sdk for our app.
3. You have
to implement a callback named LoginManager in
order to get the
login response here . Which goes like this :
LoginManager.getInstance().registerCallback(callbackManager,new
FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult
loginResult) {
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException
exception) {
}
});
Here you have to pass
a CallbackManager interface which can be
initialize like this
:
private CallbackManager callbackManager;
callbackManager =CallbackManager.Factory.create();
4. Now if we
want some info regarding user then we can get like
this on login success
callback :
if(Profile.getCurrentProfile()!=null){
Log.e("Profile<><><>",
Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}else{
Profile.fetchProfileForCurrentAccessToken();
Log.e("Profile<><><>",
Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}
Here we have to use
Profile class define in facebook sdk to fetch the
information.
5. Resultcode
inside onActivityResult after login success will
be : 64206
Which can be used if
we've to handle multiple conditions inside
onActivityResult
Cheers!!!
Wednesday, 15 April 2015
Webview content coming small in 5+ android versions as compared to lower versions
Issue Faced when changing target version from 17 to 21
hi guys today i was working on a issue which was : Content (i.e images,text etc loaded from a url) inside the webview was coming small and Same content was coming of bigger size in Lower versions (Android Version <= 4.4).As there are some changes made in the latest webview this was the reason.
I added following properties to my webview and My Problem was solved.
Before
After
hope this help others as well.
Happy coding!!
Cheers!!
hi guys today i was working on a issue which was : Content (i.e images,text etc loaded from a url) inside the webview was coming small and Same content was coming of bigger size in Lower versions (Android Version <= 4.4).As there are some changes made in the latest webview this was the reason.
I added following properties to my webview and My Problem was solved.
wvWebView.setInitialScale(1); wvWebView.getSettings().setLoadWithOverviewMode(true); wvWebView.getSettings().setUseWideViewPort(true);
Before
After
hope this help others as well.
Happy coding!!
Cheers!!
Subscribe to:
Posts (Atom)






