Saturday, May 30, 2015

How to reference xml elements using findViewById within fragment

While developing the Android application using activity, we use to reference the XML elements using findViewById method as below:

TextView name = (TextView) findViewById(R.id.Config_name);

This works only if we extend the class with Activity. It gives the compilation error as method not found if we extend the class with Fragment.

Though we are extending the class with the fragment, we can use the findViewById method with small modification. We need to get the rootview and in that view we can use this method as below

TextView name = (TextView) getView(). findViewById(R.id.Config_name);

This has to be used after inflating the xml. Also we can use the below way also:

myFragmentView = inflater.inflate(R.layout.MyLayout, container, false);
TextView name = (TextView) myFragmentView. findViewById(R.id.Config_name)

Here I am using the TextView example. You can use this for any of the views. That's it. Hope this helps. Happy Coding.



Friday, May 29, 2015

Converting an activity to a Fragment in android

Earlier I developed an android application using the Activity. But now the trend is to develop the android application using the Fragments. So I thought of converting the Activity based android application to Fragment based Android application. Earlier I thought it’s very difficult to do it. But It’s very simple. Here are the steps  how we convert an activity to a Fragment:

1.     First we need to import the package: FragmentActivity of Support V4 app package:

Import android.support.v4.app.FragmentActivity;

2.     Change the extends Activity to extends Fragment. Eg:


public class HomeFragment extends Fragment{

}

3.     Add the method onCreateView and move everything from onCreate of Activity to onCreateView except super.onCreate() and setContentView() 

public View onCreateView(LayoutInflater inflater, ViewGroup parent,     Bundle savedInstanceState) {
            View v = null;
            v = inflater.inflate(R.layout.fragment_home, parent, false);
           
// Content of previous onCreate() here
    // ...
     
    // Don't use this method, it's handled by inflater.inflate() above :
    // setContentView(R.layout.fragment_home);
           
            return v;        
      }

4.      Remove method onCreate(). 

5.      Everywhere you access the Activity with “this” or just something or “this.something” replace with super.getActivity(). or use the value saved in the onCreateView

Example : Intent i = getIntent();
becomes Intent i = super.getActivity().getIntent();



That’s it. Happy Coding…

Wednesday, May 20, 2015

How to integrate whatsapp with android application or a website


We can integrate whatsapp with the android app in two ways:

1, Using the Android Intent System which we discussed in my previous blog: "How to share the media and text with the social android apps"

2. Using the custom URL scheme provided by whatsapp itself.

We will discuss the second one in this blog. 

WhatsApp provides a custom URL scheme to interact with whatsapp. If you have a website and want to open a WhatsApp chat with a pre-defined message, you can use whatsapp’s custom URL scheme.

Opening whatsapp://send?text= followed by the text to send, will open WhatsApp, allow the user to choose a contact, and pre-defined the input field with the specified text.

Here is an example of how to write this on your website:
<a href="whatsapp://send?text=Hello%20World!">Hello, world!</a>

This opens a Whatsapp application to select the contact. Once you select the contact, the predefined text "Hello World" will be posted to the chat window.

That’s it. Happy Coding...

Reference:  whatsapp faq. 

Tuesday, May 19, 2015

How to share the media or Message Text with social apps in your android application



I was developing an android application to a customer. One of the requirement was to share some media and text with the installed social android apps like Google+, Email, Gmail, Facebook, Drive, Messaging, Whatsapp etc. Also to share the application recommendation message to these apps

In Android, you can share the text or the media with the social apps installed in your device.  We take the example of sharing a Predefined text.

I am using our Panchanga app to demonstrate it. In the application, I have added a menu item as “Share”. Here I am going to show only the required code. You can modify or include the below code according to your project

Creating Menu Item:

MenuItem share;

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
               super.onCreateOptionsMenu(menu);
              
               share = menu.add("Share");
               share.setIcon(R.drawable.share);
               share.setOnMenuItemClickListener(this);
              
        return true;
        }



Note: share.png icon is added to the drawable folder. Now we will see how we can implement the onMenuItemClickListener:



public boolean onMenuItemClick(MenuItem menuItem) {
 
        if (menuItem == share) {
                       Intent sendIntent = new Intent();
                       sendIntent.setAction(Intent.ACTION_SEND);
                       sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                       sendIntent.setType("text/plain");
                       startActivity(sendIntent);
               }
               return true;
}



 

Here the system picks all the social apps installed in your device and show. You can select the required app to send. Here “This is my text to send.” is the text I am sending to other apps. You can add text or media as your requirement. Below are the images how it works:





















If you want to share with only specific app instead of giving the selecting option to user, you can do that by using setPackage in your intent. Eg: If you want to share only with whatsapp, then:

 

sendIntent.setPackage("com.whatsapp");

 “com.Whatsapp” is the package name of the sharing app. You can use the required app there. That’s it. Happy Coding