Wednesday 26 March 2014

Apply Custom Typeface in Editext in Whole Applicaiotn android

package com.abc;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

public class MyEditText extends EditText{
   
    //The image we are going to use for the Clear button
//    private Drawable imgCloseButton = getResources().getDrawable(R.drawable.clear_button_image);
    
    public MyEditText(Context context) {
        super(context);
        init();
    }

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

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    void init() {
        
        // Set bounds of the Clear button so it will look ok
//        imgCloseButton.setBounds(0, 0, imgCloseButton.getIntrinsicWidth(), imgCloseButton.getIntrinsicHeight());

        // There may be initial text in the field, so we may need to display the  button
        handleClearButton();

        //if the Close image is displayed and the user remove his finger from the button, clear it. Otherwise do nothing
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                MyEditText et = MyEditText.this;

                if (et.getCompoundDrawables()[2] == null)
                    return false;
                
                if (event.getAction() != MotionEvent.ACTION_UP)
                    return false;
                
//                if (event.getX() > et.getWidth() - et.getPaddingRight() - imgCloseButton.getIntrinsicWidth()) {
//                    et.setText("");
//                    MyEditText.this.handleClearButton();
//                }
                return false;
            }
        });

        //if text changes, take care of the button
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                MyEditText.this.handleClearButton();
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
        });
    }
    
    //intercept Typeface change and set it with our custom font
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/opensans_bold_webfont.ttf"));
        }
        else {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/opensans_bold_webfont.ttf"));
        }
    }
    
    void handleClearButton() {
        if (this.getText().toString().equals(""))
        {
            // add the clear button
            this.setCompoundDrawables(this.getCompoundDrawables()[0], this.getCompoundDrawables()[1], null, this.getCompoundDrawables()[3]);
        }
        else
        {
            //remove clear button
//            this.setCompoundDrawables(this.getCompoundDrawables()[0], this.getCompoundDrawables()[1], imgCloseButton, this.getCompoundDrawables()[3]);
        }
    }
}

In XML File
 <com.abc.MyEditText
                android:id="@+id/edtt"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_title_amount"
               />

Apply Custom Type Face in Texview in Whole Applicaiton android

package com.example;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextViewTitle extends TextView {
   
    Typeface mTypeface;

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

    public MyTextViewTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public MyTextViewTitle(Context context) {
        super(context);
        init();

    }

    private void init() {
        if (!isInEditMode()) {

            mTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Rupee_Foradian.ttf");           
            setTypeface(mTypeface,Typeface.BOLD);
        }
    }

}
And In XML File
<com.example.MyTextViewTitle
            android:id="@+id/titel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My text"
            android:textSize="22sp"
             />

Wednesday 19 March 2014

How to call SOAP service in android applicaiton


For Running Below code you should have following kasoap2 jar file
Ksoap jar file

public class NewClass extends Activity
{
    public final String NAMESPACE = "***";
    public final String URL = "***";
   
    public final String SOAP_ACTION_1 = "**";
    public final String METHOD_NAME_1 = "**";
   
   
   
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_1);
        /**
         * Below one line is for Passing Parameter to Soap Service.
         */
        request.addProperty("ID",3); //Parameter Pass if needed.
       
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION_1, envelope);
       
            /**
             * Below two line for check SOAP error if you passing wrong parameter or anything else
             */
            SoapFault error = (SoapFault)envelope.bodyIn;
            System.out.println("Error message : "+error.toString());
           
           
            SoapObject mSoapObjectCompanyDetailResponse = (SoapObject)envelope.bodyIn;
           
            Object re= null;
            re = envelope.getResponse();

            Log.i("myApp", re.toString());
            System.out.println("Response "+re.toString());
            //                System.out.println("fff "+re.toString());


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}