Monday 30 April 2018

Facebook Script


Introducing the facebook script commercial edition, a rich wall updating script and it’s a collaboration of Techieupgrader blog tutorials and some Jquery plugins.
We added and improved many new features like Login|Register,profile|Newsfeed,Autoload|Comment,Like|FriendSystem,
Message|Notifications,LiveSearch,collapsed comments, expanding urls and much more
Don't miss the video demo. Thanks!

The new facebook script contains five folders called assets,bootstrapconfig,includeszip-files with PHP files.

script download


Watch Features Demo



Database design for Facebook  script. Contains eight tables commentsfriend_requests,likes, messagesnotifications, posts,   users, and  trends,

Features
HomePage

  
Profilepage

Message
Comment

  
Search

notification

Friend Requests


Setting Page




Expanding Url



Upload Image



Delete post



Sunday 17 December 2017

Spinner DropDown List Example using PHP MySQL and Android Studio

           In this tutorial I will teach how to build a dropdwon list / menu using PHP, MySQL, and JSON
Needed:
  • PHP Editor like Notepad++ (downloaded here if you dont have it)
  • Webserver like XAMPP (downloaded here if you dont have it) or see this tutorial how to install XAMPP
  • Android Studio (downloaded here if you dont have it)
Step 1: Create Database
Go to your localhost:  http://localhost/ or  http://localhost/phpmyadmin/
and create new database named: customers
alfter that create a new table and call it  customer and  insert some data.
                   
Step 2: Create PHP
After that go to your XAMPP or WAMP or your webserver install directory
for XAMP users go to your XAMPP installation folder C:\xampp\htdocs and under the htdocs folder create a new folder and call it custmers and Create a new file call it spinner_test.php and write following code.
                      <?php
  $DB_USER='YOUR USER'; 
$DB_PASS='YOUR PASSWORD'; 
$DB_HOST='localhost';
$DB_NAME='YOUR DATABASE NAME';
                        $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
                       /* check connection */
                         if (mysqli_connect_errno())
                           {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
                          }
                          $mysqli->query("SET NAMES 'utf8'");
                          $sql="SELECT iname FROM Spinnertest";
                          $result=$mysqli->query($sql);
                          while($e=mysqli_fetch_assoc($result))
                           {
$output[]=$e; 
                           }
print(json_encode($output)); 
$mysqli->close();
                           ?>
Step 3: Gradle Files

   include android {....} brfore buildToolsVersion 
useLibrary 'org.apache.http.legacy'
   incclude dependencies{....}
 compile 'com.loopj.android:android-async-http:1.4.5'
 compile 'com.android.support:design:23.0.0'
Step 4: AndroidManifest.xml
   Include user permission in your AndroidManifest File
<uses-permission android:name="android.permission.INTERNET" />
Step 5:  Spinner_layout.xml
   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
                    android:orientation="vertical"    
                    android:layout_width="match_parent"    
                    android:layout_height="match_parent>
    <TextView        
       android:id="@+id/txt"        
       android:layout_width="match_parent"        
       android:layout_height="wrap_content"      
       android:padding="10dp"        
       android:textSize="30dp"
       android:textColor="#000000" />
</LinearLayout>
Step 6:  Activity_Main.xml
                            <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
               xmlns:tools="http://schemas.android.com/tools"    
               android:layout_width="match_parent"    
               android:layout_height="match_parent"    
               android:orientation="vertical"    
               android:padding="10dp"    
               tools:context=".MainActivity"    
               android:weightSum="1">
              <TextView
               android:layout_width="match_parent"  
               android:layout_height="wrap_content"   
               android:text="Click Spinner"    
               android:gravity="center" 
               android:textSize="25dp" />
             <Spinner 
              android:id="@+id/spinner"   
              android:layout_width="match_parent"   
              android:layout_height="wrap_content"   
              android:layout_weight="0.07">
             </Spinner>
Step 7:  MainActivity.java
                          package com.spinnerexample;
             import android.app.ProgressDialog;
             import android.os.AsyncTask;
             import android.support.v7.app.ActionBarActivity;
             import android.os.Bundle;
             import android.support.v7.app.AppCompatActivity;
             import android.view.View;
             import android.widget.AdapterView;
             import android.widget.ArrayAdapter;
             import android.widget.Spinner;
             import android.widget.Toast;
             import org.apache.http.HttpEntity;
             import org.apache.http.HttpResponse;
             import org.apache.http.client.HttpClient;
             import org.apache.http.client.methods.HttpPost;
             import org.apache.http.impl.client.DefaultHttpClient;
             import org.json.JSONArray;
             import org.json.JSONException;
             import org.json.JSONObject;
             import java.io.BufferedReader;
             import java.io.IOException;
             import java.io.InputStream;
             import java.io.InputStreamReader;
             import java.util.ArrayList;
             public class MainActivity extends AppCompatActivity{
             ArrayList<String> listItems=new ArrayList<>();
             ArrayAdapter<String> adapter;
            Spinner sp;
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sp=(Spinner)findViewById(R.id.spinner);
       adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
       sp.setAdapter(adapter);
       sp.setOnItemSelectedListener(
       new AdapterView.OnItemSelectedListener() {
       public void onItemSelected(
       AdapterView<?> parent, View view, int position, long id) {
       Toast.makeText(getApplicationContext(),listItems.get(position),
       Toast.LENGTH_LONG).show();
                    }
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
           }
            public void onStart(){
        super.onStart();
        BackTask bt=new BackTask();
        bt.execute();
    }
        private class BackTask extends AsyncTask<Void,Void,Void> {
        ProgressDialog loading;
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
       loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
            list=new ArrayList<>();
        }
        protected Void doInBackground(Void...params){
            InputStream is=null;
            String result="";
            try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost= new HttpPost("http://krishscs.esy.es/Spinner_test.php");
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
        // Get our response as a String.                is = entity.getContent();
            }catch(IOException e){
                e.printStackTrace();
            }
//convert response to string            try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    result+=line;
                }
                is.close();
                //result=sb.toString();            }catch(Exception e){
                e.printStackTrace();
            }
// parse json data            try{
                JSONArray jArray =new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    JSONObject jsonObject=jArray.getJSONObject(i);
 // add interviewee name to arraylist  list.add(jsonObject.getString("iname"));
                }
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            return null;
        } protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
            loading.dismiss();
  Toast.makeText(getApplicationContext(),"Loading Completed",Toast.LENGTH_LONG).show();
        }
    }
}

Wednesday 29 November 2017

Android RecycleView Example Using MySQL and include SearchView


This tutorial demonstrates how to deal with search view  in android using PHP as server side scripting language and MySQL as backend.  User enters search query into search view/search bar to search for particular information, the query is sent to php file and result from php file is displayed on Android ListView / RecyclerView.
 Gradle

compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.android.support:design:24.2.1'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:cardview-v7:+'
 compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")
MainActivity.java
package com.recyclerviewexample;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Filter;
import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private static final String TAG = "RecyclerViewExample";
    private List<FeedItem> feedsList;
    private RecyclerView mRecyclerView;
    private MyRecyclerViewAdapter adapter;
    private ProgressBar progressBar;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        String url = "http://krishscs.esy.es/RecycleViewExample/RecycleViewExample.php";
        mSearchView = (SearchView) findViewById(R.id.search_view);
        setupSearchView();
        new DownloadTask().execute(url);
}
   public class DownloadTask extends AsyncTask<String, Void, Integer> {
        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
        }
@Override
        protected Integer doInBackground(String... params) {
            Integer result = 0;
            HttpURLConnection urlConnection;
            try {
                URL url = new URL(params[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                int statusCode = urlConnection.getResponseCode();

                // 200 represents HTTP OK
                if (statusCode == 200) {
                    BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null) {
                        response.append(line);
                    }
                    parseResult(response.toString());
                    result = 1; // Successful
                } else {
                    result = 0; //"Failed to fetch data!";
                }
            } catch (Exception e) {
                Log.d(TAG, e.getLocalizedMessage());
            }
            return result; //"Failed to fetch data!";
        }

        @Override
        protected void onPostExecute(Integer result) {
            progressBar.setVisibility(View.GONE);

            if (result == 1) {
                adapter = new MyRecyclerViewAdapter(MainActivity.this, feedsList);
                mRecyclerView.setAdapter(adapter);
            } else {
                Toast.makeText(MainActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void parseResult(String result) {
        try {
            feedsList = new ArrayList<FeedItem>();
            JSONObject response = new JSONObject(result);
            JSONArray posts = response.optJSONArray("result");
            for (int i = 0; i < posts.length(); i++) {
                JSONObject post = posts.optJSONObject(i);
                FeedItem item = new FeedItem();
                item.setTitle(post.optString("name"));
                item.setThumbnail(post.optString("image"));
                feedsList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void setupSearchView() {
        // mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(this);
//        adapter.setFilter(feedsList);
        // mSearchView.setSubmitButtonEnabled(true);
        mSearchView.setQueryHint("Search here....");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Take appropriate action for each action item click
int id = item.getItemId();
        if (id == R.id.search) {
            // Handle the camera action
            final SearchView searchView = (SearchView)  MenuItemCompat.getActionView(item);
            searchView.setOnQueryTextListener(this);
        }
        return true;
    }
    @Override
    public boolean onQueryTextChange(String newText) {
        final List<FeedItem> filteredModelList = filter(feedsList, newText);
        adapter.setFilter(filteredModelList);
        return true;
    }
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }
    private List<FeedItem> filter(List<FeedItem> models, String query) {
        query = query.toLowerCase();final List<FeedItem> filteredModelList = new ArrayList<>();
        for (FeedItem model : models) {
            final String name = model.getTitle().toLowerCase();
            if (name.contains(query)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }
}
MyRecyclerViewAdapter.java
package com.recyclerviewexample;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.ArrayList;
import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {
    private List<FeedItem> feedItemList;
    private Context mContext;
    //private OnItemClickListener onItemClickListener;
    View view;
    public MyRecyclerViewAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
        //this.feedItemList = new ArrayList<FeedItem>();
        // we copy the original list to the filter list and use it for setting row values
        //this.feedItemList.addAll(this.feedItemList);
    }
    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
        final FeedItem feedItem = feedItemList.get(i);
        //Download image using picasso library
        if (!TextUtils.isEmpty(feedItem.getThumbnail())) {
            Glide.with(mContext).load(feedItem.getThumbnail()).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(customViewHolder.imageView);
        }

        //Setting text view title
        customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
        customViewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), feedItem.getTitle(), Toast.LENGTH_SHORT).show();            }
        });
      /*  final View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onItemClickListener.onItemClick(feedItem);
                Toast.makeText(view.getContext(),feedItem.getTitle(), Toast.LENGTH_SHORT).show();
            }
        };*/
        /*customViewHolder.imageView.setOnClickListener(listener);
        customViewHolder.textView.setOnClickListener(listener);*/
    }
    class CustomViewHolder extends RecyclerView.ViewHolder {
        protected ImageView imageView;
        protected TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
            this.textView = (TextView) view.findViewById(R.id.title);
        }
    }


    @Override
    public int getItemCount() {
        return feedItemList.size();
    }

    public void setFilter(List<FeedItem> countryModels) {
        feedItemList = new ArrayList<>();
        feedItemList.addAll(countryModels);
        notifyDataSetChanged();
    }
}

FeedItem.java

package com.recyclerviewexample;
public class FeedItem {
    private String title;
    private String thumbnail;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getThumbnail() {
        return thumbnail;
    }
    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }
}
 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.recyclerviewexample">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"   
 xmlns:tools="http://schemas.android.com/tools"   
 android:id="@+id/activity_main"   
 android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="#f1f1f1"    
android:padding="12dp"    tools:context=".MainActivity">
    <android.support.v7.widget.SearchView   android:layout_width="match_parent"      
  android:layout_height="wrap_content"       
 android:id="@+id/search_view"       
 android:focusable="false"       
 android:focusableInTouchMode="false">
    </android.support.v7.widget.SearchView>
    <ProgressBar   android:id="@+id/progress_bar"   
     android:layout_width="wrap_content"     
   android:layout_height="wrap_content"  
   android:layout_centerInParent="true"/>

    <android.support.v7.widget.RecyclerView      
  android:id="@+id/recycler_view"      

  android:layout_width="match_parent"    
    android:layout_height="match_parent"     
   android:layout_alignParentLeft="true"     
   android:layout_alignParentStart="true"      
  android:layout_below="@+id/search_view" />
</RelativeLayout>
searchview_in_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="search............."
        app:showAsAction="collapseActionView|ifRoom" />
</menu> 
list_row.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    cardview:cardCornerRadius="8dp"
    cardview:cardElevation="3dp"
    cardview:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_alignParentTop="true"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:hint="Name"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="#444"
            android:textSize="18dp"
            android:textStyle="bold"/>


    </RelativeLayout>
</android.support.v7.widget.CardView>
PHP’s
 RecycleViewExample.php

<?php
 require_once('db_Connect.php');
$sql = "select * from RecycleViewExample";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'image'=>$row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
db_Connect.php
<?php
 define('HOST','mysql.hostinger.in');
 define('USER','u293140154_user');
 define('PASS','******');
 define('DB','u293140154_test');
  $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
 ?>