Thursday, 31 October 2019

Android : Belajar Kotlin Untuk Pemula Part#2


Postingan kali ini saya akan melanjutkan belajar kotlin untuk pemula, setelah pada postingan Sebelumnya sudah membahan sedikit tentang apa itu kotlin dan kali ini akan coba membuat aplikasi sederhana menggunakan kotlin yaitu aplikasi untuk menghitung volume, pastinya sudah tau kan rumus untuk menghitung volume? :D

rumusnya : Volume = Panjang * Lebar * Tinggi


Ok langsung saja, kita buka android studio dan buat project baru 







Setelah mebuat project baru selesai buat layout seperti berikut :


<?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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="16dp"    tools:context=".MainActivity">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/panjang" />
    <EditText        android:id="@+id/edt_length"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="numberDecimal"        android:lines="1" />
    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/lebar" />
    <EditText        android:id="@+id/edt_width"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="numberDecimal"        android:lines="1" />
    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/tinggi" />
    <EditText        android:id="@+id/edt_height"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="numberDecimal"        android:lines="1" />
    <Button        android:id="@+id/btn_calculate"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/hitung" />
    <TextView        android:id="@+id/tv_result"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="@string/hasil"        android:textSize="24sp"        android:textStyle="bold" />

    <Button        android:id="@+id/btn_reset"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/reset" />

</LinearLayout>


 Lalu di folder values/string.xml di isi seperti berikut :


<resources>
    <string name="app_name">Menghitung Volume</string>
    <string name="panjang">Panjang</string>
    <string name="lebar">Lebar</string>
    <string name="tinggi">Tinggi</string>
    <string name="hitung">Hitung</string>
    <string name="hasil">Hasil</string>
    <string name="reset">Reset</string>
</resources>


pada MainActivity.java kita deklarasikan variable seperti berikut didalam MainActivity class :

private lateinit var edtWidth: EditText
private lateinit var edtHeight: EditText
private lateinit var edtLength: EditText
private lateinit var btnCalculate: Button
private lateinit var btnReset: Button
private lateinit var tvResult: TextView

lalu didalam onCreate tambahkan seperti berikut :

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    edtWidth = findViewById(R.id.edt_width)
    edtHeight = findViewById(R.id.edt_height)
    edtLength = findViewById(R.id.edt_length)
    btnCalculate = findViewById(R.id.btn_calculate)
    btnReset = findViewById(R.id.btn_reset)
    tvResult = findViewById(R.id.tv_result)

    btnCalculate.setOnClickListener(this)
    btnReset.setOnClickListener(this)
}

Jika ada error seperti berikut, klik pada icon error dan pilih implement interface



setelah itu, class MainActivity akan menjadi seperti berikut :



Lalu didalam onClick tambahkan source code berikut dan ganti parametr p0 menjadi v dan View? menjadi View :


val inputLength = edtLength.text.toString().trim()
val inputWidth = edtWidth.text.toString().trim()
val inputHeight = edtHeight.text.toString().trim()

if (v.id == R.id.btn_calculate) {
    var isEmptyFields = false    if (inputLength.isEmpty()) {
        isEmptyFields = true        edtLength.error = "Field ini tidak boleh kosong"    }
    if (inputWidth.isEmpty()) {
        isEmptyFields = true        edtWidth.error = "Field ini tidak boleh kosong"    }
    if (inputHeight.isEmpty()) {
        isEmptyFields = true        edtHeight.error = "Field ini tidak boleh kosong"    }
    if (!isEmptyFields) {
        val volume = inputLength.toDouble() * inputWidth.toDouble() * inputHeight.toDouble()
        tvResult.text = volume.toString()
    }
}

if(v.id == R.id.btn_reset){
    edtLength.run { setText("") }    edtHeight.setText("")
    edtWidth.setText("")
    tvResult.text = ""}

full source code  class MainActivity.kt seperti berikut :


package com.ketik.toekang.belajarkotlin1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var edtWidth: EditText
    private lateinit var edtHeight: EditText
    private lateinit var edtLength: EditText
    private lateinit var btnCalculate: Button
    private lateinit var btnReset: Button
    private lateinit var tvResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        edtWidth = findViewById(R.id.edt_width)
        edtHeight = findViewById(R.id.edt_height)
        edtLength = findViewById(R.id.edt_length)
        btnCalculate = findViewById(R.id.btn_calculate)
        btnReset = findViewById(R.id.btn_reset)
        tvResult = findViewById(R.id.tv_result)

        btnCalculate.setOnClickListener(this)
        btnReset.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.        val inputLength = edtLength.text.toString().trim()
        val inputWidth = edtWidth.text.toString().trim()
        val inputHeight = edtHeight.text.toString().trim()

        if (v.id == R.id.btn_calculate) {
            var isEmptyFields = false            if (inputLength.isEmpty()) {
                isEmptyFields = true                edtLength.error = "Field ini tidak boleh kosong"            }
            if (inputWidth.isEmpty()) {
                isEmptyFields = true                edtWidth.error = "Field ini tidak boleh kosong"            }
            if (inputHeight.isEmpty()) {
                isEmptyFields = true                edtHeight.error = "Field ini tidak boleh kosong"            }
            if (!isEmptyFields) {
                val volume = inputLength.toDouble() * inputWidth.toDouble() * inputHeight.toDouble()
                tvResult.text = volume.toString()
            }
        }

        if(v.id == R.id.btn_reset){
            edtLength.run { setText("") }            edtHeight.setText("")
            edtWidth.setText("")
            tvResult.text = ""        }
    }
}

jika sudah coba jalankan aplikasinya :D



Sekian belajar kotlin kali ini semoga bermanfaat, selanjutnya akan coba belajar membuat CRUD dengan kotlin SQlite.

Terimakasih :D

Wednesday, 30 October 2019

Android : Belajar Kotlin Untuk Pemula Part#1




Postingan kali ini akan membahas tentang android programming menggunakan kotlin.

Kotlin merupakan bahasa pemrograman yang digunakan untuk membuat aplikasi android dengan IDE Android Studio, selain menggunakan Java. Kotlin dikembangkan oleh JetBrains perusahaan dibalik IntelliJ IDEA, setelah melalui banyak perkembangan JetBrains merilis kotlin secara open source dan Google mendukung penuh kotlin untuk pengembangan aplikasi android. Untuk kompilasi dari kotlin ini berupa bytecode JVM atau JavaScript.


Beberapa fitur dari kotlin

Berikut adalah fitur-fitur yang disediakan oleh kotlin untuk memudahkan penulisan kode program :

Null Pointer Exception : dengan adanya fitur ini compiler secara otomatis akan menandai pointer yang berpotensi null.

Pemrograman Fungsional : memiliki kemampuan melakukan mapping, folding pada collection java dan mendukung lambda.

Anotasi Data : anotasi data dibuat otomatis untuk mengurangi boilerplate, seperti equals, hashCode, toString.

Penulisan Syntax Lebih Simple : kotlin menawarkan penulisan syntax yang lebih simple, membuat fungsi one-liner dan membuat JavaBeans hanya dengan satu baris, serta method setter dan getter secara otomatis.

Interpolasi String : interpolasi string memungkinkan untuk memasukkan variable kedalam string tanpa penyambung seperti pada EcmaScript.

Mendukung Pembuatan aplikasi IOS : Selain android kotlin juga bisa digunakan untuk membuat aplikasi IOS dengan kotlin native dan bisa juga digunakan untuk menulis pemrograman JavaScript melalui Kotlin JS.

itulah sedikit dari beberapa fitur baru yang dihadirkan oleh kotlin sebenarnya masih banyak lagi fitur lain yang bisa kita explore, dan satu lagi di kotlin tidak perlu lagi titik koma ( ; ).



Untuk pengembangan aplikasi android dengan kotlin mulai support dari android studio versi 2.2+, gradle 2.2+ serta plugin kotlin 1.0.6+.



sekian sekilas tentang kotlin, selanjutnya membuat project baru dengan kotlin.
:D

Tuesday, 29 October 2019

SAP ABAP - Get Selected Rows ALV Grid


*&---------------------------------------------------------------------*
*& Report ZALVGRID
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALVGRID.

TABLES ekko.

TYPES :
  
BEGIN OF t_ekko,
    ebeln 
TYPE ebeln,
    bukrs 
TYPE ekko-bukrs,
    bstyp 
TYPE ekko-bstyp,
    bsart 
TYPE ekko-bsart,
    aedat 
TYPE ekko-aedat,
    ernam 
TYPE ekko-ernam,
    bedat 
TYPE ekko-bedat,
    lifnr 
TYPE ekko-lifnr,
    name1 
TYPE lfa1-name1,
    snote 
TYPE string,
  
END OF t_ekko.

DATA :
  it_ekko 
TYPE TABLE OF t_ekko,
  wa_ekko 
TYPE t_ekko.

DATA:
  ob_cust 
TYPE REF TO cl_gui_custom_container,
  ob_grid 
TYPE REF TO cl_gui_alv_grid,
  ok_code 
TYPE sy-ucomm.

DATA:
  is_varnt 
LIKE disvariant,
  is_lyout 
TYPE lvc_s_layo,
  is_print 
TYPE lvc_s_prnt.

DATA:
  it_fcatl 
TYPE lvc_t_fcat,          "----- For Field Catalog 1 ALV -----
  wa_fcatl 
LIKE LINE OF it_fcatl,
  it_excld 
TYPE ui_functions.

DATAvi_fclen TYPE i,
      vs_fcfld 
TYPE string.

DATAindex_rows TYPE lvc_t_row,
      
index      LIKE LINE OF index_rows.

SELECT-OPTIONS :
  s_ebeln 
FOR ekko-ebeln,
  s_aedat 
FOR ekko-aedat,
  s_lifnr 
FOR ekko-lifnr.

START-OF-SELECTION.
  
PERFORM f_getdata.
  
CALL SCREEN 9000.

FORM f_getdata.
  
REFRESH it_ekko.
  
SELECT
    ekko
~ebeln
    ekko
~bukrs
    ekko
~bstyp
    ekko
~bsart
    ekko
~aedat
    ekko
~ernam
    ekko
~bedat
    ekko
~lifnr
    lfa1
~name1
    
FROM ekko INNER JOIN lfa1
    
ON ekko~lifnr lfa1~lifnr
    
INTO CORRESPONDING FIELDS OF TABLE it_ekko
      
WHERE ekko~ebeln IN s_ebeln AND ekko~aedat IN s_aedat
      
AND ekko~lifnr IN s_lifnr AND ekko~bstyp 'F' AND ekko~loekz ''.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
  
SET PF-STATUS 'PF900'.
  
SET TITLEBAR  'TL900'.

  
PERFORM f_inital.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
  
CASE sy-ucomm.
    
WHEN 'BACK' OR 'EXIT' OR 'CNCL'.
      
LEAVE TO SCREEN 0.
    
WHEN 'CHANGE'.
      
IF ekko-bedat IS NOT INITIAL.
        
CLEAR index_rows.  REFRESH index_rows.
        
CALL METHOD ob_grid->get_selected_rows
          
IMPORTING
            et_index_rows 
index_rows.

        
IF index_rows[] IS INITIAL.
          
MESSAGE 'No data selected' TYPE 'S' DISPLAY LIKE 'E'.
        
ELSE.
          
LOOP AT index_rows INTO index.
            
CLEAR wa_ekko.
            
READ TABLE it_ekko INTO wa_ekko INDEX index-index.
            
IF sy-subrc 0.
              
CLEAR wa_ekko-bedat.
              wa_ekko
-bedat ekko-bedat.
              wa_ekko
-snote 'Document date updated'.
              
MODIFY it_ekko INDEX index-index FROM wa_ekko
                
TRANSPORTING bedat snote"WHERE ebeln = wa_ekko-ebeln.

              
UPDATE ekko SET bedat ekko-bedat
                
WHERE ebeln wa_ekko-ebeln.
              
COMMIT WORK AND WAIT.
            
ENDIF.
          
ENDLOOP.
        
ENDIF.
      
ELSE.
        
MESSAGE 'Please input document date' TYPE 'I' DISPLAY LIKE 'E'.
      
ENDIF.

    
WHEN 'SALL'.
    
WHEN 'DALL'.
  
ENDCASE.
ENDMODULE.

FORM f_inital.

  
CHECK ok_code <> 'EXIT'.
  
IF ob_cust IS INITIAL.
    
CREATE OBJECT ob_cust
      
EXPORTING
        container_name 
'CONTAINER'.

    
CREATE OBJECT ob_grid
      
EXPORTING
        i_parent 
ob_cust.
  
ENDIF.

  
PERFORM to_varnt.
  
PERFORM to_lyout.
  
PERFORM f_fieldcat.
  
PERFORM to_excld CHANGING it_excld.
  
PERFORM alv_displ_out.

  
CALL METHOD ob_grid->refresh_table_display.

ENDFORM.

*----------------------------------------------------------------------*
FORM to_varnt .
  
CLEAR is_varnt.
  is_varnt
-report     sy-repid.
  is_varnt
-username   sy-uname.
ENDFORM.

*----------------------------------------------------------------------*
FORM to_lyout .
  
CLEAR is_lyout.
  is_lyout
-sel_mode     'D'.
  is_lyout
-zebra        'X'.
  is_lyout
-cwidth_opt   'X'.
  is_lyout
-no_rowins    'X'.
  is_lyout
-stylefname   'CSTYL'.
  is_lyout
-info_fname   'CLINE'.
  is_lyout
-ctab_fname   'CCELL'.
ENDFORM.

FORM f_fieldcat.

  
REFRESH it_fcatl.

  
PERFORM alv_field_cat "NOUT ICON NSUM DSUM EDIT
    
USING :
*          ' XX  ' 1   5   'ICONS'       ''          ''        ''  ''      'Status',
          
'  X  ' 1 30  'EBELN'       ''      ''            ''  ''      'Purchasing Doc',
          
'  X  ' 2 30  'BUKRS'       ''      ''            ''  ''      'Company Code',
          
'  X  ' 3 30  'BSTYP'       ''      ''            ''  ''      'Doc. Category',
          
'  X  ' 4 30  'BSART'       ''      ''            ''  ''      'Document Type',
          
'  X  ' 5 30  'AEDAT'       ''      ''            ''  ''      'Create On',
          
'  X  ' 6 30  'ERNAM'       ''      ''            ''  ''      'Create By',
          
'  X  ' 7 30  'BEDAT'       ''      ''            ''  ''      'Document Date',
          
'  X  ' 8 30  'LIFNR'       ''      ''            ''  ''      'Vendor',
          
'  X  ' 9 30  'NAME1'       ''      ''            ''  ''      'Vendor Name',
          
'  X  ' 10 30  'SNOTE'       ''      ''            ''  ''      'Notes'.



ENDFORM.

FORM alv_field_cat USING p_fset p_cpos p_olen p_fild p_rtab p_rfld p_ftyp p_qcif p_text.
  
CLEAR wa_fcatl.

  wa_fcatl
-no_out           p_fset+0(1).  "01
  wa_fcatl
-icon             p_fset+1(1).  "02
  wa_fcatl
-no_sum           p_fset+2(1).  "03
  wa_fcatl
-do_sum           p_fset+3(1).  "04
  wa_fcatl
-edit             p_fset+4(1).  "05

  
IF p_olen > 2.
    wa_fcatl
-outputlen      p_olen.       "06
  
ENDIF.

  vs_fcfld 
p_fild.
  
TRANSLATE vs_fcfld TO UPPER CASE.
  wa_fcatl
-fieldname        vs_fcfld.     "07

  vs_fcfld 
p_rfld.
  
TRANSLATE vs_fcfld TO UPPER CASE.
  wa_fcatl
-ref_field        vs_fcfld.     "09

  vs_fcfld 
p_rtab.
  
TRANSLATE vs_fcfld TO UPPER CASE.
  wa_fcatl
-ref_table        vs_fcfld.     "08

  vs_fcfld 
p_qcif.
  
TRANSLATE vs_fcfld TO UPPER CASE.

  
IF p_ftyp 'C'.
    wa_fcatl
-cfieldname     vs_fcfld.   "10
  
ELSEIF p_ftyp 'Q'.
    wa_fcatl
-qfieldname     vs_fcfld.   "11
  
ENDIF.

  vi_fclen 
strlenp_text ).
  
IF vi_fclen > 10.
    wa_fcatl
-scrtext_l      p_text.
  
ELSE.
    wa_fcatl
-scrtext_m      p_text.
    wa_fcatl
-scrtext_s      p_text.
  
ENDIF.

  wa_fcatl
-col_pos          p_cpos.

  
APPEND wa_fcatl TO it_fcatl.
ENDFORM.

FORM alv_displ_out.
  
CALL METHOD ob_grid->set_table_for_first_display
    
EXPORTING
      is_variant                    
is_varnt      
      i_save                        
'A'           
      i_default                     
'X'           
      is_layout                     
is_lyout      
      is_print                      
is_print      
      it_toolbar_excluding          
it_excld
    
CHANGING
      it_outtab                     
it_ekko
      it_fieldcatalog               
it_fcatl      
    
EXCEPTIONS
      invalid_parameter_combination 
1
      program_error                 
2
      too_many_lines                
3
      
OTHERS                        4.
ENDFORM.

FORM to_excld CHANGING pt_exclude TYPE ui_functions.
  
DATA ls_exclude TYPE ui_func.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_check.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_refresh.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_loc_cut.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_loc_copy.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_loc_paste.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_loc_undo.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_mb_sum.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_mb_subtot.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_print.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_graph.
  
APPEND ls_exclude TO pt_exclude.
  ls_exclude 
cl_gui_alv_grid=>mc_fc_info.
  
APPEND ls_exclude TO pt_exclude.
ENDFORM.

Belajar SAP ABAP RAP

  Belajar SAP ABAP RAP: Pengenalan dan Konsep Dasar Restful ABAP Programming Model Kalau kamu seorang ABAPer yang mulai terjun ke dunia SAP...