Selasa, 22 Maret 2022

RADIOBUTTON SELECTED

 PRAKTISNYA SBB

radPa.setChecked(true);
ATAU
radioGroup1.check(R.id.radPa);
RESMINYA SBB:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mode Pengiriman"
android:textColor="#000000"
android:textSize="16sp" />

<RadioGroup android:id="@+id/txtmkirim" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/rad1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kirim Barang" android:checked="true" />
<RadioButton android:id="@+id/rad2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Datang ke Toko" />
</RadioGroup>
RadioGroup radioGroup1;
RadioButton rb1 ,rb1 ;
GETVALUE:
radioGroup1 = (RadioGroup) findViewById(R.id.txtmkirim);
int selectedId1 = radioGroup1.getCheckedRadioButtonId();
RadioButton radVirtual= (RadioButton) findViewById(selectedId1);
String isiData= radVirtual.getText().toString();
SETSELECTED
rb1 = (RadioButton) findViewById(R.id.rad1);
rb2 = (RadioButton) findViewById(R.id.rad2);
rb1.setChecked(true);
if(kirim.equalsIgnoreCase("Datang ke Toko")){
rb2.setChecked(true);
}





XMLDINAMIS

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">


<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rating"
android:background="#fff"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/txtheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textStyle="bold"
android:text="Info Nota"
android:textColor="@color/colorPrimaryDark"
android:textSize="20dp" />

</LinearLayout>

<LinearLayout
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cabang"
android:textColor="#000000"
android:textSize="16sp" />

<EditText
android:id="@+id/txtcabang"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorBorder"
android:enabled="false"
android:textColor="#000000"
android:textSize="16sp" />


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nama Pelanggan"
android:textColor="#000000"
android:textSize="16sp" />

<EditText
android:id="@+id/txtnama"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorBorder"
android:enabled="false"
android:textColor="#000000"
android:textSize="16sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nominal Tagihan."
android:textColor="#000000"
android:textSize="16sp" />

<EditText
android:id="@+id/txtnominal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorBorder"
android:enabled="false"
android:textColor="#000000"
android:textSize="16sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Alamat Pengiriman"
android:textColor="#000000"
android:textSize="16sp" />

<EditText
android:id="@+id/txtalamat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textColor="#000000"
android:textSize="16sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mode Pengiriman"
android:textColor="#000000"
android:textSize="16sp" />

<RadioGroup android:id="@+id/txtmkirim" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/rad1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kirim Barang" android:checked="true" />
<RadioButton android:id="@+id/rad2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Datang ke Toko" />
</RadioGroup>


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mode Pengambilan"
android:textColor="#000000"
android:textSize="16sp" />

<RadioGroup android:id="@+id/txtmambil" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/rad3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Diambil Sendiri" android:checked="true" />
<RadioButton android:id="@+id/rad4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dikirimkan" />
</RadioGroup>
</LinearLayout>

<LinearLayout
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnupdate"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="UPDATE"
android:background="@drawable/round_button"
android:textColor="@color/black"
android:textStyle="bold" />

<Button
android:id="@+id/btnhapus"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/round_button"
android:textColor="@color/black"
android:layout_margin="5dip"
android:textStyle="bold"
android:text="CANCEL" />
<Button
android:id="@+id/btnbayar"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/round_button"
android:textColor="@color/black"
android:layout_margin="5dip"
android:textStyle="bold"
android:text="CHECKOUT" />
</LinearLayout>


<LinearLayout
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="300dp"/>
</LinearLayout>


</LinearLayout>


</ScrollView>


</RelativeLayout>











Minggu, 20 Maret 2022

UPLOAD IMAGE TO SERVER

 

PHP: uploadserver.php

<?php 
    $file_path = "ypathfile/";
$IMG=$_FILES['uploaded_file']['name'];
    $file_path = $file_path . basename( $IMG);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
}
 ?>

XML:uploadserver.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgx">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/imageView" />

<TableLayout android:id="@+id/TableLayout01"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_marginTop="10dip"
android:layout_height="wrap_content">

<TableRow
android:id="@+id/TableRow00"
android:layout_width="wrap_content"
android:layout_height="62dp">

<Button
android:id="@+id/btnpilih"
android:layout_width="364dp"
android:layout_height="48dp"
android:layout_gravity="left"
android:layout_marginTop="15dp"
android:background="@drawable/backgroundbutton"
android:text="Choose File"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20dp"
app:backgroundTint="@android:color/holo_red_light" />
</TableRow>
</TableLayout>

<TableLayout android:id="@+id/TableLayout02"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_marginTop="3dip"
android:layout_height="wrap_content">

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

<Button
android:id="@+id/btncamera"
android:layout_width="364dp"
android:layout_height="48dp"
android:layout_gravity="left"
android:layout_marginTop="15dp"
android:background="@drawable/backgroundbutton"
android:text="Camera"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20dp"
app:backgroundTint="@android:color/holo_red_light" />
</TableRow>
</TableLayout>

<TableLayout android:id="@+id/TableLayout03"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_marginTop="3dip"
android:layout_height="wrap_content">

<TableRow
android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<Button
android:id="@+id/uploadButton"
android:layout_width="364dp"
android:layout_height="48dp"
android:layout_gravity="left"
android:layout_marginTop="25dp"
android:background="@drawable/backgroundbutton"
android:text="Click To Upload File"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20dp"
app:backgroundTint="@android:color/holo_orange_dark" />
</TableRow>
</TableLayout>


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/messageText"
android:textColor="#000000"
android:textStyle="bold"
/>
</LinearLayout>


JAVA 
UploadToServer.java

package com.example.apps_jasasepatu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;


public class UploadToServer extends Activity {
String mnu="", myFILE="",PK;
JSONParser jsonParser = new JSONParser();

private static final int PICK_IMAGE = 1;
private static final int PICK_Camera_IMAGE = 2;
private ImageView imgView;
private Button upload,cancel;
private Bitmap bitmap;
Uri imageUri;
MediaPlayer mp=new MediaPlayer();

String URL="",NFUpload="";
TextView messageText;
Button uploadButton,btnpilih,btncamera;
int serverResponseCode = 0;
ProgressDialog dialog = null;

String upLoadServerUri = null;

/********** File Path *************/
final String uploadFilePath = "/mnt/sdcard/";
String uploadFileName = "myfile.jpg";

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.uploadserver);

Intent i = getIntent();
mnu = i.getStringExtra("mnu");
PK = i.getStringExtra("pk");

imgView = (ImageView) findViewById(R.id.imageView);
uploadButton = (Button)findViewById(R.id.uploadButton);
messageText = (TextView)findViewById(R.id.messageText);

btnpilih = (Button)findViewById(R.id.btnpilih);
btncamera = (Button)findViewById(R.id.btncamera);

messageText.setText(uploadFilePath+"/"+uploadFileName+"'");

/************* Php script path ****************/
URL=jsonParser.getIP()+"uploadserver.php";
upLoadServerUri = URL;

uploadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(UploadToServer.this, "", "Uploading file...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
messageText.setText("uploading started.....");
}
});
String AL=messageText.getText().toString();
File f=new File(AL);
myFILE=f.getName();
uploadFiles(AL);
}
}).start();
}
});

btnpilih.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gintent, "Select Picture"),PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});


btncamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fileName = "new-photo-name.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});
}

public int uploadFiles(String sourceFileUri) {
String fileName = sourceFileUri;
NFUpload=fileName;
uploadFileName=NFUpload;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);

if (!sourceFile.isFile()) {

dialog.dismiss();

Log.d("uploadFile", "Source File not exist :" +uploadFilePath + "" + uploadFileName);

runOnUiThread(new Runnable() {
public void run() {
messageText.setText("Source File not exist :"+uploadFilePath + "" + uploadFileName);
} });

return 0;
}
else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
java.net.URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);

dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();

Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
String msg = "File Upload Completed:" + uploadFileName;
messageText.setText(msg);
Toast.makeText(UploadToServer.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
}
});
}

fileInputStream.close();
dos.flush();
dos.close();

if(mnu.equalsIgnoreCase("Jasa")) {
Intent i = new Intent(getApplicationContext(), DetailJasa.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("pk", PK);
i.putExtra("gambar_sepatu", myFILE);
startActivityForResult(i, 100);
finish();
}
else if(mnu.equalsIgnoreCase("Keranjang")) {
Intent i = new Intent(getApplicationContext(), DetailKeranjang.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("pk", PK);
i.putExtra("gambar_sepatu", myFILE);
startActivityForResult(i, 100);
finish();
}

} catch (MalformedURLException ex) {

dialog.dismiss();
ex.printStackTrace();

runOnUiThread(new Runnable() {
public void run() {
messageText.setText("MalformedURLException Exception : check script url.");
Toast.makeText(UploadToServer.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
}
});

Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {

dialog.dismiss();
e.printStackTrace();

runOnUiThread(new Runnable() {
public void run() {
messageText.setText("Got Exception : see logcat ");
Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",Toast.LENGTH_SHORT).show();
}
});

}
dialog.dismiss();
return serverResponseCode;
} // End else block
}



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
selectedImageUri = data.getData();
}
break;
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
selectedImageUri = imageUri;
/*Bitmap mPic = (Bitmap) data.getExtras().get("data");
selectedImageUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), mPic, getResources().getString(R.string.app_name), Long.toString(System.currentTimeMillis())));*/
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}

if(selectedImageUri != null){
try {
String filemanagerstring = selectedImageUri.getPath();
String selectedImagePath = getPath(selectedImageUri);
messageText.setText(selectedImagePath);

if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}

if (filePath != null) {
decodeFile(filePath);
} else {
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}

}



class ImageGalleryTask extends AsyncTask<Void, Void, String> {
@SuppressWarnings("unused")
@Override
protected String doInBackground(Void... unsued) {
InputStream is;
BitmapFactory.Options bfo;
Bitmap bitmapOrg;
ByteArrayOutputStream bao ;

bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
//bitmapOrg = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);

bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
// String ba1 = Base64.encodeBytes(ba);

String ba1 = Base64.encodeToString(ba,1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("cmd","image_android"));
Log.v("log_tag", System.currentTimeMillis()+".jpg");
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
// Here you need to put your server file address


HttpPost(URL);//"http://www.picsily.com/upload_photo.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v("log_tag", "In the try Loop" );
}catch(Exception e){
Log.v("log_tag", "Error in http connection "+e.toString());
}
return "Success";
// (null);
}

@Override
protected void onProgressUpdate(Void... unsued) {

}

@Override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}

}

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}

public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 1024;

// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);

imgView.setImageBitmap(bitmap);

}

}