Sabtu, 13 Juli 2019

RadioGroup Data






<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/bg"  >

    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:app="http://schemas.android.com/apk/res-auto"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical"        android:padding="10dip" >
        <!--  View Title Label -->        <TextView            android:id="@+id/txtMarquee"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text=""            android:textSize="14sp"            android:typeface="sans"            android:textColor="#ffffff"            android:background="#000000"            android:singleLine="true"            android:ellipsize="marquee"            android:marqueeRepeatLimit="marquee_forever">
        </TextView>


        <ImageView            android:id="@+id/myGambar"            android:layout_width="fill_parent"            android:layout_height="220px"            android:layout_gravity="center_horizontal"            android:src="@drawable/c" />

    <LinearLayout        android:background="@color/white"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical"        android:padding="10dip" >
        <!--  Name Label -->
        <TextView            android:id="@+id/txtid_pengujian0"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="ID Pengujian"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <!--  telepon TextField -->        <EditText            android:id="@+id/txtid_pengujian"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textColor="#000000" />
        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Nama Chef"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <Spinner android:id="@+id/spin" android:drawSelectorOnTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <!--  telepon Label -->        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Jarak"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <RadioGroup android:id="@+id/radA"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">
            <RadioButton android:id="@+id/radA1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Dekat"                android:checked="true" />
            <RadioButton android:id="@+id/radA2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Sedang" />
            <RadioButton android:id="@+id/radA3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Jauh" />
            <RadioButton android:id="@+id/radA4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Sangat Jauh" />

        </RadioGroup>



        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Skill"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <RadioGroup android:id="@+id/radB"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">
            <RadioButton android:id="@+id/radB1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Sangat Handal"                android:checked="true" />
            <RadioButton android:id="@+id/radB2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Handal" />
            <RadioButton android:id="@+id/radB3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Cukup Handal" />
            <RadioButton android:id="@+id/radB4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Kurang Handal" />

        </RadioGroup>

        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Paket"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>
        <RadioGroup android:id="@+id/radC"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">
            <RadioButton android:id="@+id/radC1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Paket Kilat"                android:checked="true" />
            <RadioButton android:id="@+id/radC2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Paket Praktek" />
            <RadioButton android:id="@+id/radC3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Paket sampai bisa" />

        </RadioGroup>

            <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Pengalaman"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <RadioGroup android:id="@+id/radD"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">
            <RadioButton android:id="@+id/radD1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Sudah Lama"                android:checked="true" />
            <RadioButton android:id="@+id/radD2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Lama" />
            <RadioButton android:id="@+id/radD3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Cukup Lama" />
            <RadioButton android:id="@+id/radD4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Baru" />

        </RadioGroup>

            <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Tempat Usaha"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <RadioGroup android:id="@+id/radE"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">
            <RadioButton android:id="@+id/radE1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Sangat Besar"                android:checked="true" />
            <RadioButton android:id="@+id/radE2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Besar" />
            <RadioButton android:id="@+id/radE3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Cukup Besar" />
            <RadioButton android:id="@+id/radE4"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Kecil" />

        </RadioGroup>

            <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="Catatan"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold"/>

        <!--  telepon TextField -->        <EditText            android:id="@+id/txtcatatan"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textColor="#000000" />



        <!--  Link to Login Screen -->        <Button            android:id="@+id/btnproses"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="40dip"            android:textColor="#ffffff"            android:background="@color/yello"            android:textStyle="bold"            android:text="Pilih" />

        <Button            android:id="@+id/btnhapus"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dip"            android:text="Simpan"            android:textColor="#ffffff"            android:background="@color/yello"            android:textStyle="bold" />

    </LinearLayout>
    </LinearLayout>

</ScrollView>


++++

package com.example.win10.cooking;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class cDetailADD extends Activity {
   String ip="",id="",nama="";
   String id_pengujian;
   String id_detail;
   String id_detail0="";

   EditText txtid_pengujian;
   TextView txtid_pengujian0;

   Spinner txtid_chef;
   RadioGroup txtc1;
   RadioGroup txtc2;
   RadioGroup txtc3;
   RadioGroup txtc4;
   RadioGroup txtc5;
   EditText txtcatatan;

String[]arID;
   String[]arNama;
int jd=0;

   Button btnProses;
   Button btnHapus;
   JSONParser jParser = new JSONParser();
   JSONArray myJSON = null;
   private ProgressDialog pDialog;
   JSONParser jsonParser = new JSONParser();

   private static final String TAG_SUKSES = "sukses";
   private static final String TAG_record = "record";

   private static final String TAG_id_pengujian = "id_pengujian";
   private static final String TAG_id_chef = "id_chef";
   private static final String TAG_c1 = "c1";
   private static final String TAG_c2 = "c2";
   private static final String TAG_c3 = "c3";
   private static final String TAG_c4 = "c4";
   private static final String TAG_c5 = "c5";
   private static final String TAG_catatan = "catatan";

   @Override   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.cdetailadd);


      SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(cDetailADD.this);
      Boolean Registered = sharedPref.getBoolean("Registered", false);
      if (!Registered) {
         //finish();      } else {
         id = sharedPref.getString("id", "");
         nama= sharedPref.getString("nama", "");
         id_pengujian= sharedPref.getString("id_pengujian", "");
      }

      ip=jsonParser.getIP();
      callMarquee();
      txtid_pengujian0= (TextView) findViewById(R.id.txtid_pengujian0);
      txtid_pengujian= (EditText) findViewById(R.id.txtid_pengujian);
      txtid_chef= (Spinner) findViewById(R.id.spin);
      txtc1= (RadioGroup) findViewById(R.id.radA);
      txtc2 = (RadioGroup) findViewById(R.id.radB);
      txtc3= (RadioGroup) findViewById(R.id.radC);
      txtc4= (RadioGroup) findViewById(R.id.radD);
      txtc5 = (RadioGroup) findViewById(R.id.radE);
      txtcatatan= (EditText) findViewById(R.id.txtcatatan);

      btnProses= (Button) findViewById(R.id.btnproses);
      btnHapus = (Button) findViewById(R.id.btnhapus);


      Intent i = getIntent();
      id_detail0 = i.getStringExtra("pk");id_detail=id_detail0;
      id_pengujian = i.getStringExtra("id_pengujian");

      if(id_pengujian.length()<3){
         txtid_pengujian0.setVisibility(View.GONE);
         txtid_pengujian.setVisibility(View.GONE);
      }
      else{
         txtid_pengujian0.setVisibility(View.VISIBLE);
         txtid_pengujian.setVisibility(View.VISIBLE);
         txtid_pengujian0.setText("IDPengujian");
         txtid_pengujian.setText(id_pengujian);
      }


      btnProses.setOnClickListener(new View.OnClickListener() {
         @Override         public void onClick(View arg0) {
            int pilA= txtc1.getCheckedRadioButtonId();
            RadioButton rA = (RadioButton) findViewById(txtc1.getCheckedRadioButtonId());
              String c1=rA.getText().toString();

            RadioButton rB= (RadioButton) findViewById(txtc2.getCheckedRadioButtonId());
            String c2=rB.getText().toString();

            RadioButton rC = (RadioButton) findViewById(txtc3.getCheckedRadioButtonId());
            String c3=rC.getText().toString();

            RadioButton rD = (RadioButton) findViewById(txtc4.getCheckedRadioButtonId());
            String c4=rD.getText().toString();

            RadioButton rE = (RadioButton) findViewById(txtc5.getCheckedRadioButtonId());
            String c5=rE.getText().toString();

            String lid_chef= txtid_chef.getSelectedItem().toString();
         if(lid_chef.length()<1){lengkapi("CHEF");}
            else if(c1.length()<1){lengkapi("Jarak");}
            else if(c2.length()<1){lengkapi("Skill");}
            else if(c3.length()<1){lengkapi("Paket");}
            else if(c4.length()<1){lengkapi("Pengalaman");}
            else if(c5.length()<1){lengkapi("Tempat Usaha");}
            else{
            new save().execute();
            }//else
         }});

      btnHapus.setText("Kembali");
      btnHapus.setOnClickListener(new View.OnClickListener() {
         @Override         public void onClick(View arg0) {
            finish();
            //new del().execute();         }});

        new getchef().execute();
   }

   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



   class getchef extends AsyncTask<String, String, String> {
      @Override      protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(cDetailADD.this);
         pDialog.setMessage("Load data. Silahkan Tunggu...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(false);
         pDialog.show();
      }
      protected String doInBackground(String... args) {
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         JSONObject json = jParser.makeHttpRequest(ip+"chef/chef_show.php", "GET", params);
         Log.d("show: ", json.toString());
         try {
            int sukses = json.getInt(TAG_SUKSES);
//          id_pengujian = json.getString("id_pengujian");            if (sukses == 1) {
               myJSON = json.getJSONArray(TAG_record);

               jd=myJSON.length();
               arID=new String[jd];
               arNama=new String[jd];

               for (int i = 0; i < jd; i++) {
                  JSONObject c = myJSON.getJSONObject(i);
                  String id_chef= c.getString(TAG_id_chef);
                  String nama_chef = c.getString("nama_chef");
                  arID[i]=id_chef;
                  arNama[i]=nama_chef;

               }
            } else {
               // Toast.makeText(ChefList.this,"Data Tidak Tersedia",Toast.LENGTH_LONG);            }
         }
         catch (JSONException e) {e.printStackTrace();}
         return null;
      }

      protected void onPostExecute(String file_url) {
         pDialog.dismiss();
         runOnUiThread(new Runnable() {
            public void run() {

   tambahkan();
//             ArrayAdapter<String> adapter = new ArrayAdapter<String>(cDetailADD.this,android.R.layout.simple_spinner_item, arNama);//             txtid_chef.setAdapter(adapter);
            }
         });}
   }


void tambahkan(){
   final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(cDetailADD.this);
   SharedPreferences.Editor editor = sharedPref.edit();
   editor.putString("id_pengujian", id_pengujian);
   editor.apply();

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(cDetailADD.this,android.R.layout.simple_spinner_item, arNama);
   txtid_chef.setAdapter(adapter);

}


   class save extends AsyncTask<String, String, String> {
      @Override      protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(cDetailADD.this);
         pDialog.setMessage("Menyimpan data ...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(true);
         pDialog.show();
      }
      protected String doInBackground(String... args) {
         String catatan=txtcatatan.getText().toString();
         int pilA= txtc1.getCheckedRadioButtonId();
         RadioButton rA = (RadioButton) findViewById(txtc1.getCheckedRadioButtonId());
         String c1=rA.getText().toString();

         RadioButton rB= (RadioButton) findViewById(txtc2.getCheckedRadioButtonId());
         String c2=rB.getText().toString();

         RadioButton rC = (RadioButton) findViewById(txtc3.getCheckedRadioButtonId());
         String c3=rC.getText().toString();

         RadioButton rD = (RadioButton) findViewById(txtc4.getCheckedRadioButtonId());
         String c4=rD.getText().toString();

         RadioButton rE = (RadioButton) findViewById(txtc5.getCheckedRadioButtonId());
         String c5=rE.getText().toString();

         String lid_chef= txtid_chef.getSelectedItem().toString();


         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("id_pengujian", id_pengujian));
         params.add(new BasicNameValuePair("id_chef", lid_chef));
         params.add(new BasicNameValuePair("id", id));
         params.add(new BasicNameValuePair("c1", c1));
         params.add(new BasicNameValuePair("c2", c2));
         params.add(new BasicNameValuePair("c3", c3));
         params.add(new BasicNameValuePair("c4", c4));
         params.add(new BasicNameValuePair("c5", c5));
         params.add(new BasicNameValuePair("catatan", catatan));

         String url=ip+"detail/detail_add.php";
         Log.v("ADD",url+"?id_pengujian="+id_pengujian+"&id="+id+"&id_chef="+lid_chef+"&c1="+c1+"&c2="+c2+"&c3="+c3+"&c4="+c4+"&c5="+c5+"&catatan="+catatan);
         JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
         Log.d("add", json.toString());
         try {
            int sukses = json.getInt(TAG_SUKSES);
            if (sukses == 1) {
               id_pengujian= json.getString("id_pengujian");
            } else {
               // gagal update data            }
         } catch (JSONException e) {
            e.printStackTrace();
         }
         return null;
      }

      protected void onPostExecute(String file_url) {pDialog.dismiss();


         final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(cDetailADD.this);
         SharedPreferences.Editor editor = sharedPref.edit();
         editor.putString("id_pengujian", id_pengujian);
         editor.apply();



         String lid_chef= txtid_chef.getSelectedItem().toString();
         sukses(lid_chef);
      }
   }
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
   public void lengkapi(String item){
      new AlertDialog.Builder(this)
            .setTitle("Lengkapi Data")
            .setMessage("Silakan lengkapi data "+item +" !")
            .setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dlg, int sumthin) {
                  finish();
               }}).show();
   }

   public void sukses(String item){
      new AlertDialog.Builder(this)
            .setTitle("Pemilihan Chef Berhasil")
            .setMessage("Terimakasih Anda telah memilih "+item +" pada "+id_pengujian)
            .setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dlg, int sumthin) {
                  finish();
               }}).show();
   }

   void callMarquee(){
      Calendar cal = Calendar.getInstance();
      int jam = cal.get(Calendar.HOUR);
      int menit= cal.get(Calendar.MINUTE);
      int detik= cal.get(Calendar.SECOND);

      int tgl= cal.get(Calendar.DATE);
      int bln= cal.get(Calendar.MONTH)+1;
      int thn= cal.get(Calendar.YEAR);

      String stgl=String.valueOf(tgl)+"-"+String.valueOf(bln)+"-"+String.valueOf(thn);
      String sjam=String.valueOf(jam)+":"+String.valueOf(menit)+":"+String.valueOf(detik);

      TextView  txtMarquee=(TextView)findViewById(R.id.txtMarquee);
      txtMarquee.setSelected(true);
      String kata="Selamat Datang di Aplikasi Android LP2MARAY  "+stgl+"/"+sjam+" #";
      String kalimat=String.format("%1$s",TextUtils.htmlEncode(kata));
      txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat));
   }

   public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
         finish();
         return true;
      }
      return super.onKeyDown(keyCode, event);
   }

}


Minggu, 19 Mei 2019

Detail galeri



galeridetail.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#fff"  >

    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical"        android:padding="6dip" >
        <TextView            android:id="@+id/txtMarquee"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text=""            android:textSize="14sp"            android:typeface="sans"            android:textColor="#ffffff"            android:background="#000000"            android:singleLine="true"            android:ellipsize="marquee"            android:marqueeRepeatLimit="marquee_forever">
        </TextView>

        <ImageView            android:id="@+id/myGambar"            android:gravity="center"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_centerHorizontal="true"            android:layout_centerVertical="true"            android:src="@drawable/gallery" />
             />

        <ZoomControls            android:layout_marginTop="10dip"            android:id="@+id/simpleZoomControl"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#000" />


        <TextView            android:id="@+id/title"            android:layout_width="fill_parent"            android:layout_marginTop="6dip"            android:layout_height="wrap_content"            android:text="Judul"            android:textColor="#000000"            android:textSize="20sp"            android:textStyle="bold" />

        <TextView            android:id="@+id/title2"            android:layout_width="fill_parent"            android:layout_marginTop="6dip"            android:layout_height="wrap_content"            android:text="Keterangan"            android:textColor="#000000"            android:textSize="20sp" />

        <TextView            android:id="@+id/title3"            android:layout_width="fill_parent"            android:layout_marginTop="6dip"            android:layout_height="wrap_content"            android:text="Tgl"            android:textColor="#000000"            android:textSize="20sp" />

        <TextView            android:id="@+id/title4"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="6dip"            android:text="Pengriim"            android:textColor="#000000"            android:textSize="20sp"/>

        <TableRow            android:id="@+id/TableRow02"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center">

            <Button                android:id="@+id/btnShare"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Share"                android:textColor="#000000"                android:textStyle="bold" />

            <Button                android:id="@+id/btnsave"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="Download"                android:textColor="#000000"                android:textStyle="bold" />

        </TableRow>
        <TextView
        android:layout_width="fill_parent"        android:layout_marginTop="6dip"        android:layout_height="wrap_content"        android:text=""        android:textColor="#000000"        android:textSize="20sp"        android:textStyle="bold" />
    </LinearLayout>

</ScrollView>


package com.kriptograph.mygaleri;

import android.Manifest;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;

public class DetailGaleri extends AppCompatActivity {
    JSONParser jsonParser = new JSONParser();

    String tkode="",tnama="",tmail="",thp="",tlati="",tlongi="",talamat="",tangkatan="",tstatus="",tgambar="",waktu="";
    String skodelogin="",snamalogin="",smaillogin="";
    String myLati="-6.352953";
    String myLongi="106.831389";
    String myPosisi="UI";
    ZoomControls simpleZoomControls;
    ImageView myGambar;

String ip="";
    private ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    String title,title2, title3, title4, gambar;

    String currentImage = "";

    private static final int MY_PERMISSION_REQUEST = 1;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galeridetail);
        myGambar=(ImageView) findViewById(R.id.myGambar);

        simpleZoomControls = (ZoomControls) findViewById(R.id.simpleZoomControl);
        simpleZoomControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                float x=simpleZoomControls.getScaleX();
                float y=simpleZoomControls.getScaleY();
                myGambar.setScaleX((float) (x+1));
                myGambar.setScaleY((float) (y+1));
            }
        });

        simpleZoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                float x=simpleZoomControls.getScaleX();
                float y=simpleZoomControls.getScaleY();
                myGambar.setScaleX((float) (x-1));
                myGambar.setScaleY((float) (y-1));
            }
        });

        //meminta izin penyimpanan        if (ContextCompat.checkSelfPermission(DetailGaleri.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(DetailGaleri.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(DetailGaleri.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
            } else {
                ActivityCompat.requestPermissions(DetailGaleri.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
            }
        } else {
            //DO NOTHING        }

        ip=jParser.getIP();

         title = getIntent().getStringExtra("title");
      
        //int gb= getIntent().getIntExtra("gambar",R.drawable.ic_produk);        //Bitmap bitmap = getIntent().getParcelableExtra("image");
        String arUrlFoto=ip+"ypathfile/"+gambar;
        new DownloadImageTask(myGambar).execute(arUrlFoto);


        Button    btnShare=(Button)findViewById(R.id.btnShare);
        btnShare.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(Intent.EXTRA_TEXT, "Judul :"+title+"\n\nKet :"+title3+"\n\nWaktu :"+waktu+"\n\nPengirim :"+title4);
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                startActivity(Intent.createChooser(sharingIntent, "Share using"));

            }});


        Button    btnsave=(Button)findViewById(R.id.btnsave);btnsave.setText("Download");
        btnsave.setVisibility(View.VISIBLE);
        btnsave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                savepicture();
            }});
        callMarquee();
    }




    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            }
            catch (Exception e) {
                Log.e("Error", e.getMessage());e.printStackTrace();}
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {bmImage.setImageBitmap(result); }
    }


    void callMarquee(){
        Calendar cal = Calendar.getInstance();
        int jam = cal.get(Calendar.HOUR);
        int menit= cal.get(Calendar.MINUTE);
        int detik= cal.get(Calendar.SECOND);

        int tgl= cal.get(Calendar.DATE);
        int bln= cal.get(Calendar.MONTH)+1;
        int thn= cal.get(Calendar.YEAR);

        String stgl= String.valueOf(tgl)+"-"+ String.valueOf(bln)+"-"+ String.valueOf(thn);
        String sjam= String.valueOf(jam)+":"+ String.valueOf(menit)+":"+ String.valueOf(detik);

        TextView txtMarquee=(TextView)findViewById(R.id.txtMarquee);
        txtMarquee.setSelected(true);
        String kata=jsonParser.getNama()+stgl+"/"+sjam+" #";
        String kalimat= String.format("%1$s", TextUtils.htmlEncode(kata));
        txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat));
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void savepicture() {
        AlertDialog.Builder ad = new AlertDialog.Builder(DetailGaleri.this);
        ad.setTitle("Save Picture");
        ad.setMessage(title+" \n"+title3+"\n"+title4);

        ad.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {

                View content = findViewById(R.id.myGambar);
                Bitmap bitmap = getScreenShot(content);
//                currentImage = "image" + System.currentTimeMillis() + ".jpg";                currentImage = title+" \n"+title3+"\n"+title4+".jpg";
                store(bitmap, currentImage);
//                b_share.setEnabled(true);               // finish();            }
        });
        ad.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            }
        });
        ad.show();
    }

    private static Bitmap getScreenShot(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    private void store(Bitmap bm, String fileName) {
        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/FILEPROJEK";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dirPath, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            Toast.makeText(this, "Saved to "+dirPath +"..!", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }



    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 0, "Profile");
        menu.add(0, 2, 0, "Call");
        menu.add(0, 3, 0, "Mail");
        menu.add(0, 4, 0, "Message");
        menu.add(0, 5, 0, "Location");
        menu.add(0, 6, 0, "Direction");
        menu.add(0, 7, 0, "Activity");
        menu.add(0, 8, 0, "Back");
        return true;
    }

    final Context c = this;
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Intent i1 = new Intent(getApplicationContext(), Galeri.class);
                i1.putExtra("pk",tkode);
                startActivityForResult(i1, 100);
                return true;
            case 2:
                String toDial="tel:"+thp;
                startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));
                return true;
            case 3:
                //Mail                LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c);
                View mView = layoutInflaterAndroid.inflate(R.layout.user_input_dialog_box, null);
                AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(c);
                alertDialogBuilderUserInput.setView(mView);

                final EditText userInputDialogEditText = (EditText) mView.findViewById(R.id.userInputDialog);
                alertDialogBuilderUserInput
                        .setCancelable(false)
                        .setPositiveButton("Send", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogBox, int id) {

                                //========================
                                String pesan=userInputDialogEditText.getText().toString();
                                if(pesan.length()>0) {
                                    String to = tmail;
                                    String subject = "Kirim Pesan Via APP-M8 ";
                                    String message = pesan + "\nTertanda:" + snamalogin + " /" + skodelogin + " (" + smaillogin + ")";

                                    Intent email = new Intent(Intent.ACTION_SEND);
                                    email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
                                    //email.putExtra(Intent.EXTRA_CC, new String[]{ to});                                    //email.putExtra(Intent.EXTRA_BCC, new String[]{to});                                    email.putExtra(Intent.EXTRA_SUBJECT, subject);
                                    email.putExtra(Intent.EXTRA_TEXT, message);

                                    //need this to prompts email client only                                    email.setType("message/rfc822");

                                    startActivity(Intent.createChooser(email, "Fr:" + smaillogin));
                                    new AlertDialog.Builder(DetailGaleri.this)
                                            .setTitle("Mail to "+tnama)
                                            .setMessage("Sukses Kirim mail ke "+tmail+" /"+tnama+" ("+tkode+")")
                                            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dlg, int sumthin) {
                                                    finish();
                                                }}).show();
                                }
                                else{
                                    LayoutInflater inflater = getLayoutInflater();
                                    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout_id));
                                    ImageView image = (ImageView) layout.findViewById(R.id.image);
                                    image.setImageResource(R.drawable.logochat);
                                    TextView text = (TextView) layout.findViewById(R.id.text);
                                    text.setText("Silakan isi pesan Mail Anda");

                                    Toast toast = new Toast(getApplicationContext());
                                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                                    toast.setDuration(Toast.LENGTH_LONG);
                                    toast.setView(layout);
                                    toast.show();
                                }
                                //========================                            }
                        })

                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialogBox, int id) {
                                        dialogBox.cancel();
                                    }
                                });

                AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
                alertDialogAndroid.show();


                return true;
            case 4:
                //Inbox message                Intent i2 = new Intent(getApplicationContext(), Galeri.class);
                i2.putExtra("tkode",tkode);
                i2.putExtra("tnama",tnama);
                startActivityForResult(i2, 100);
                return true;
            case 5:
                //Location                Intent i3 = new Intent(getApplicationContext(), Galeri.class);
                i3.putExtra("tkode",tkode);
                i3.putExtra("tnama",tnama);
                i3.putExtra("myLati",myLati);
                i3.putExtra("myLongi",myLongi);
                i3.putExtra("myPosisi",myPosisi);
                i3.putExtra("myLati2",tlati);
                i3.putExtra("myLongi2",tlongi);
                i3.putExtra("myPosisi2",talamat);

                startActivityForResult(i3, 100);
                return true;
            case 6:
                //direction                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://maps.google.com/maps?saddr="+myLati+","+myLongi+"&daddr="+tlati+","+tlongi));
                startActivity(intent);
                return true;
            case 7:
                //activity                Intent i4 = new Intent(getApplicationContext(), Galeri.class);
                i4.putExtra("tkode",tkode);
                i4.putExtra("tnama",tnama);
                i4.putExtra("tangkatan",tangkatan);
                i4.putExtra("tstatus",tstatus);
                i4.putExtra("tgambar",tgambar);
                startActivityForResult(i4, 100);
                return true;
            case 8:
                //back                finish();
                return true;

        }
        return false;
    }
}


user_input_dialog_box.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/custom_dialog_layout_design_user_input"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="16dp">

    <TextView        android:id="@+id/dialogTitle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Send Mail"        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText        android:id="@+id/userInputDialog"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Pesan Mail"        android:inputType="text" />
</LinearLayout>



custom_toast.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/custom_toast_layout_id"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#FFF"    android:orientation="horizontal"    android:padding="5dp" >

    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_marginRight="5dp" />

    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:textColor="#000" />

</LinearLayout>