Friday 28 April 2017

Typescript Plugin For Netbeans IDE

Netbeans is one of my favorite IDE, and i use it for coding ionic apps. And here is the plugin that i used in netbeans to write TypeScript Download

Sunday 23 April 2017

How to get adsense account in a week and start making money online

From my experience its quite easy to get an adsense account. I'm going to mention how i got my adsense account within a week.

1. Created a blog in blogger.com.

2. Changed my blog's theme to be a bit professional, also added navigation menu.

3. Brought professional domain from godaddy.com (i brought .in domain @ just Rs.199).

4. Added 8 posts. I'm a software developer, so I started to write posts about programming. Also added about page for my blog.

5. Finally submitted my blog for adsense.

!!Oh my God on 6th day evening i received my adsense approval email.

Friday 21 April 2017

Android broadcast event to trigger function when push notification recieved

In your activity add the below code:

@Override
public void onResume() {
    super.onResume();
    context.registerReceiver(mMessageReceiver, new IntentFilter("any_key"));
}

//Must unregister onPause()
@Override
protected void onPause() {
    super.onPause();
    context.unregisterReceiver(mMessageReceiver);
}

//this method will handle the broadcast call
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
//get data from intend
        String text = intent.getStringExtra("text");
    }
};

Call the below method in any class to trigger the above broadcast event:

static void refreshActivity(Context context, String text) {
    Intent intent = new Intent("any_key");
    //add any text, if any
    intent.putExtra("text", message);
    //send to broadcast
    context.sendBroadcast(intent);
}

OR you can use Otto library to achive this.

ionic2 project example

Install NodeJs v6.10.2

Download Project

Open CMD
npm install -g ionic@2.2.3
npm install -g cordova@6.5.0
goto project root directory -> cmd -> npm install

ionic serve - to test project in browser
ionic platform add <android/ios> - To add any platform
ionic g page myPage - to generate a new component
ionic g provider myProvider-  to generate a new provider
ionic run android --prod -> to run in android (production ready)
ionic build android --prod -> to build apk (production ready)

Wednesday 19 April 2017

Android WebView sample code

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();//to hide action bar
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://mycreativecodes.blogspot.com");
        webView.getSettings().setJavaScriptEnabled(true);// enabling javascript
        webView.setHorizontalScrollBarEnabled(false);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebViewClient(new WebViewClient() {// to show progressBar when loading page

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                findViewById(R.id.progress1).setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                findViewById(R.id.progress1).setVisibility(View.GONE);
            }

        });
    }
}

Android Hide Action Bar in AppCompactActivity

        getSupportActionBar().hide();
//add this inside activity OnCreate methode

Tuesday 18 April 2017

Android dynamically creating a list using linearlayout

 LinearLayout specListLayout = (LinearLayout) findViewById(R.id.specList);//main layout
        for(int x=0;x<10;x++){
            View child = getLayoutInflater().inflate(R.layout.row, null);
            TextView colOne = (TextView) child.findViewById(R.id.colOne);
            TextView colTwo = (TextView) child.findViewById(R.id.colTwo);

            colOne.setText("Key"+x);
            colTwo.setText("Value"+x);

            if(x%2==0){
                colOne.setBackgroundColor(Color.parseColor("#CCCCCC"));
                colTwo.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }else{
                colOne.setBackgroundColor(Color.parseColor("#FFFFFF"));
                colTwo.setBackgroundColor(Color.parseColor("#CCCCCC"));
            }
            specListLayout.addView(child);
        }

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <TextView
        android:id="@+id/colOne"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:gravity="center_vertical"
        android:layout_height="50dp"
        android:padding="5dp"
        android:text="testing"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/colTwo"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="5"
        android:background="#CCCCCC"
        android:gravity="center"
        android:padding="5dp"
        android:text="testing2"
        android:textColor="@color/black" />
</LinearLayout>

Android notification with vibration and sound alert

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("MyCreativeCodes").setContentText("You have new message")
                .setVibrate(new long[] { 100, 250 })
                .setSound(notification)
                .setAutoCancel(true);;
        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());

Android responsive square image view

package xx.xx.xx.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SqureImageView extends ImageView {
    public SqureImageView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }
}


//set this instead of imagevIew

Android Code for Latest Async HTTP (connecting to php mysql) by loopj

Get the library from http://loopj.com/android-async-http/

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;


    void getProducts(){
        // Create AsycHttpClient object
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params;
        params = new RequestParams();
        params.put("code", "101245");
        client.post(constants.PRODUCTS_URL,
                params, new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        String response = new String(responseBody);
                        System.out.println(response);
                        // prgDialog.hide();
                        try {
                            JSONObject arr = new JSONObject(response);
                            System.out.println(arr.getInt("success"));
                            if (arr.getInt("success") == 1) {


                                products = arr.getJSONArray("products");

                                for (int i = 0; i < products.length(); i++) {

                                    JSONObject c = products.getJSONObject(i);

                                    String name = c.getString("title");
                                    String path = "http://skydent.in/app-admin/"+c.getString("image");
                                    int pid = c.getInt("pid");
                                    Constants.productList.add(new HomeGridViewModel(name,"", path,pid));
                                }
                               checkLoginStatus();
                            } else {

                                Toast.makeText(getApplicationContext(),
                                        "No Slider Found!", Toast.LENGTH_LONG)
                                        .show();
                                finish();
                            }
                        } catch (JSONException e) {


                            // TODO Auto-generated catch block
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Error Occured [Server's JSON response might be invalid]!",
                                    Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        if (statusCode == 404) {

                            Toast.makeText(getApplicationContext(),
                                    "Requested resource not found",
                                    Toast.LENGTH_LONG).show();
                        } else if (statusCode == 500) {

                            Toast.makeText(getApplicationContext(),
                                    "Something went wrong at server end",
                                    Toast.LENGTH_LONG).show();
                        } else {

                            Toast.makeText(
                                    getApplicationContext(),
                                    "Please connect to Internet and try again!",
                                    Toast.LENGTH_LONG).show();
                        }
                        finish();
                    }
                });
    }

Android List Popup

private void showList(){
   List<String> partyList = new ArrayList<String>();
// Create sequence of items
final CharSequence[] party = partyList.toArray(new String[partyList
.size()]);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Select Party");
dialogBuilder.setItems(party, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String selectedText = party[item].toString(); // Selected item
partyBtn.setText(selectedText);
Constants.party=selectedText;
}
});
// Create alert dialog object via builder
AlertDialog alertDialogObject = dialogBuilder.create();
// Show the dialog
alertDialogObject.show();
// ///////////////////////
}