Wednesday, December 13, 2023

Android Studio Hedgehog | 2023.1.1: Java RecyclerView & Fragment

Android Studio Hedgehog | 2023.1.1

Note: For personal reference using RecyclerView and Fragment

Create new Project (No Activity)

Name Recycler View Tutorial
Package Name com.dedetok.recyclerviewtutorial

Create Activity -> Empty View Activity

Activity Name: Main Activity
Layout Name activity_main
Laucher Activity checked

build.gradle.kts(Module :app)

plugins {
    id("com.android.application")
}

android {
    namespace = "com.dedetok.recyclerviewtutorial"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.dedetok.recyclerviewtutorial"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
,,,

Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/frag_container"/>
</androidx.constraintlayout.widget.ConstraintLayout>

my_single_column.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/first_column" />

</androidx.appcompat.widget.LinearLayoutCompat>

 my_recycler_view_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/r_view_main_layout"/>

</androidx.appcompat.widget.LinearLayoutCompat>

Java

MyAdapter.java

package com.dedetok.recyclerviewtutorial;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter {

    private String[] mDataSet;

    // constructor
    public MyAdapter(String[] mDataSet) {
        this.mDataSet = mDataSet;
    }

    @NonNull
    @Override
    //public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Original
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_single_column, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyViewHolder) holder).getTextView().setText(mDataSet[position]);
    }


    @Override
    public int getItemCount() {
        return mDataSet.length;
    }

    ///////////////////
    /*
     * class MyViewHolder extends from  RecyclerView.ViewHolder {
     */
    private class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public MyViewHolder(View v) {
            super(v);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("Element " + getAdapterPosition() + " clicked.");
                }
            });
            textView = (TextView) v.findViewById(R.id.first_column);
        }

        public TextView getTextView() {
            return textView;
        }
    }

    //////////////////


}

RecyclerViewFragment.java

package com.dedetok.recyclerviewtutorial;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewFragment extends Fragment {

    // initial data
    String[] mDataSet  = {"ace", "boom", "crew", "dog", "eon"};
    protected RecyclerView mRecyclerView;
    protected MyAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_recycler_view_layout, container, false); // conteain androidx.recyclerview.widget.RecyclerView

        // get androidx.recyclerview.widget.RecyclerView
        mRecyclerView = rootView.findViewById(R.id.r_view_main_layout);

        // Define Layout for androidx.recyclerview.widget.RecyclerView
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(mDataSet);
        // Set CustomAdapter as the adapter for RecyclerView.
        mRecyclerView.setAdapter(mAdapter);
        // END_INCLUDE(initializeRecyclerView)


        return rootView;
    }

}

MainActivity.java

package com.dedetok.recyclerviewtutorial;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            RecyclerViewFragment fragment = new RecyclerViewFragment();
            transaction.replace(R.id.frag_container, fragment);
            transaction.commit();
        }
    }
}


Thursday, November 16, 2023

Android Studio | 2022/2023 Layout 2022-2024

Recommended (modern) Layout

  1. ConstraintLayout
  2. LinearLayout (Horizontal & Vertical)
  3. FrameLayout
  4. TableLayout
  5. TableRow
  6. Space

Legacy Layout:

  1. GridLayout
  2. ListView
  3. TabHost
  4. RelaltiveLayout
  5. GridView

LinearLayout

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it occupies on the screen. A larger weight value lets it expand to fill the remaining space in the parent view. Child views can specify a weight value, and any remaining space in the view group is assigned to children proportionately, based on their declared weight. The default weight is zero.
Equal space

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" for a vertical layout, or the android:layout_width of each view to "0dp" for a horizontal layout. Then set the android:layout_weight of each view to "1".

Unequal space

You can also create linear layouts where the child elements use different amounts of space on the screen. Consider the following examples:

  •     Suppose you have three text fields: two with a weight value of 1, and a third with the default weight value of 0. The third text field, with the weight value of 0, occupies only the area required by its content. The other two text fields, with the weight value of 1, expand equally to fill the space that remains after the contents of all three fields are measured.
  •     If instead you have three text fields where two have a weight value of 1 and the third has a weight of 2, then the space that remains after the contents of all three fields are measured is allocated as follows: half to the field with the weight value of 2, and half divided equally between the fields with the weight value of 1.

Example Linear Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/send" />
</LinearLayout>

Examples ConstraintLayout and LinearLayout (Vertical)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:ems="10"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <EditText
        android:id="@+id/editTextText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        app:layout_constraintLeft_toRightOf="@id/tv1"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="Fill Name"
        android:hint="Fill Name"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv1" />
    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text just"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Just"
            />
    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.constraintlayout.widget.ConstraintLayout> 

Note: android studio will warning Hardcoded for String, you may ignore them or put your string on string resource. All the properties are mandatory. if properties does not provided, android studio will give error messages.

Working on: Android Studio Giraffe | 2022.3.1 Patch 3 maybe change in future releases

References: https://developer.android.com/develop/ui/views/layout/linear

Monday, November 6, 2023

repair grub after windows updating bios on lenovo ideaPad gaming 3 AMD 7535h (15”)

Insert Debian installer DVD/CD or USB and change boot order

  1. Keep press shift + restart windows -> troubleshoot -> UEFI Firmware Setting -> Restart
  2. Change boot order to Debian installer media

Boot into Debian

  1. Boot into Debian installer DVD/CD or USB, then press c to enter grub command. If the screen goes blank (for CD boot older then existing grub version), press ESC until you get prompt grub>
  2. Get Debian partition using grub> ls and find Filesystem type ext* or your existing filesystem (see the picture below)
  3. Get Debian grab from Debian partition e.q grub> ls (hd0,gpt7)/boot and find Debian grub folder  (see the picture below) 
  4. Run command to start existing Debian e.q
grub> set root=(hd0,gpt7)
grub> set prefix=(hd0,gpt7)/boot/grub
grub> insmod normal
grub> normal
 
Your Debian will appear in boot option

Repair grub using 

# grub-install
# update-grub

Every time you update your firmware via windows, your grub boot will lost. It's so annoying. Unfortunately, there is not much tool available to automatically check and update bios for Debian. Debian still stable using old bios, but not in windows.  

Monday, August 28, 2023

Pendirian PT Perorangan Usaha Mikro dan NIB secara mandiri melalui AHU & OSS online

Untuk memulai merintis usaha baru dalam bentuk PT Perorangan berupa usaha Mikro secara resmi diperlukan langkah berikut:

  1. Buat email resmi yang akan digunakan untuk PT Perorangan.
  2. Pendirian PT Perorangan.
  3. Nomor Ijin Berusaha (NIB) Berbasis Resiko. 

Bagian 1 Buat email resmi yang akan digunakan untuk PT Perorangan

Buat email melalui salah satu penyedia email seperti yahoo, outlook, yandex, google yang diyakini keamanannya.

Bagian 2 Pendirian PT Perorangan

Mendirikan PT Peorangan dapat dilakukan secara online melalui https://ptp.ahu.go.id/beranda. Kita perlu membuat akun terlebih dahulu. PT Perorangan diatur dan menggunakan dasar hukum UU No. 11/2020 tentang Citpta Kerja dan PP No. 7/2021 tentang Kemudahan, Pelindungan, dan Pemberdayaan Koperasi dan Usaha Mikro, Kecil, dan Menengah.

Setelah akun aktif, persiapkan hal-hal sebagai berikut:

  1. Siapkan Nama PT Perorangan, diwajibkan berbahasa Indonesia dan tidak boleh sama dengan yang sudah ada.
  2. Pemilik PT Perorangan KTP/NPWP
  3. Alamat domisili PT Perorangan
  4. Modal PT Perorangan
  5. Klasifikasi Baku Lapangan Usaha Indonesia (KBLI) toko online yang menggunakan layanan pihak lain spt bukalapak, tokopedia dll, KBLI 4791

Setelah hal-hal diatas disiapkan, pilih menu "Pendirian" dan klik Belum punya kode voucher? Klik di sini.Anda akan diarahkan untuk memdapatkan Voucher PNPB tagihan sebesar Rp. 50.000,-. Bayar dan konfirmasi pembayaran. Pembayaran pada internet banking biasanya tersedia dalam menu Pajak dan masukan kode voucher.

Setelah pembayaran tervalidasi, kembali ke AHU, dan lengkapi data dengan sebenar-benarnya. Preview Pernyataan Pendirian PT Perorangan. Bilamana sudah benar semua, konfirmasi data sudah benar.

Cetak Sertifikat Pendaftaran dan Surat Pernyataan Pendirian melalui menu Daftar Transaksi.

Bilamana dikemudian hari ada kesalah ketik (typo), misalnya alamat PT, lakukan perbaikan melalui menu perubahan. Setiap perbaikan memerlukan Voucher PNPB sebesar Rp 50.000,- seperti langkah diatas pembelian Voucher.

Bagian 3 Nomor Ijin Berusaha (NIB) Berbasis Resiko 

Untuk dapat berusaha, dibutuhkan NIB (Nomor Induk Berusaha) dari OSS yang langkah-langkahnya sebagai berikut:

  1. Buat akun oss melalui https://ui-login.oss.go.id/register.
  2. Setelah validasi berhasil, cek email dan login. 
  3. Selanjutnya, pilih menu PERIZINAN BERUSAHA -> PERMOHONAN BARU.
  4. Lengkapi data sesuai dengan tujuan yang telah diisi di AHU.
  5. Cetak Nomor Ijin Berusaha (NIB) Berbasis Resiko.

Hasil cetak pada bagian atas tengah akan berisi:

PERIZINAN BERUSAHA BERBASIS RISIKO

NOMOR INDUK BERUSAHA: xxxxxxx


Semoga Kita semua diberi kelancaran dalam berusaha