Assalamuallaikum wr.wb .......
Pada postingan kali ini kita akan belajar bersama mengenal apa itu Listview ,
okee,,,ListView adalah tampilan beberapa item dalam bentuk list yang dapat di scroll secara vertikal. Setiap item akan otomatis dimasukkan kedalam list menggunakan Adapter yang datanya di ambil dari array/database/json/dsb. ListView sering digunakan dalam aplikasi Android, seperti kontak, email, twitter.
Postingan ini saya buat untuk memenuhi salah satu tugas kuliah pada mata pelajaran Mobile Programming .
Disini saya coba membuat contoh Listview dari beberapa bahasa pemrogaraman
langsung saja kita mulai .....
1.Onboarding /splash screen
Java Onboarding
package com.example.yuresa_161021450043;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class onboarding extends AppCompatActivity {
private static int SplashDuration = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Intent intent = new Intent(getBaseContext(), form_login.class);//StartSlider,MainActivity
startActivity(intent);
finish();
} catch (Exception e){
Log.e("Splash error",e.getMessage());
Intent intent = new Intent(onboarding.this, form_login.class); //MainActivity.class
startActivity(intent);
}
}
}, SplashDuration);
}
}
Xml Onboarding
<?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=".onboarding"
android:background="@drawable/onboarding">
<TextView
android:id="@+id/txt01"
android:layout_width="264dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="132dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Welcome to Count Me"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.408"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/nama"
android:layout_width="165dp"
android:layout_height="76dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Yuresa"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.411"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt01" />
<TextView
android:id="@+id/nim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="161021450043"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.376"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nama" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.Form Login
Java Login
package com.example.yuresa_161021450043;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class form_login extends AppCompatActivity {
EditText id_name,id_password;
Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_login);
id_name=findViewById(R.id.id_name);
id_password=findViewById(R.id.id_password);
btnLogin=findViewById(R.id.btnLogin);
id_name.setText("Yuresa@Countme.com");
id_password.setText("123456");
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(form_login.this,calculator.class);
startActivity(i);
}
});
}
}
Xml Login
<?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=".onboarding"
android:background="@drawable/calculator">
<EditText
android:id="@+id/id_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="312dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:textColor="#FF800000"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.421"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/id_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="204dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:textColor="#FF800000"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.421"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnLogin"
android:layout_width="134dp"
android:layout_height="54dp"
android:layout_marginTop="104dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.462"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/id_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.Listview
package com.example.yuresa_161021450043;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public ListView listdata ;
// String[] listArray={"ASP","C#","C++","HTML5","Javascript","Java","Objective-C","Perl","PHP","Python","Swift",};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listdata = (ListView)findViewById(R.id.listid);
ArrayList<String> list = new ArrayList<String>();
list.add("ASP");
list.add("Java");
list.add("HTML");
list.add("C++");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listdata.setAdapter(arrayAdapter);
listdata.setOnClickListener(new AdapterView.OnItemClickListener()){
public void onItemClick(AdapterView<?> agr0, View arg1, int itemKe,
long arg3){
String itemText=(String) listdata.getItemAtPosition(itemKe);
Tost.makeText(getBaseContext(), "Anda Meng Klik " +itemText, Toast.LENGTH_LONG).show();
if(itemText.equals("ASP")){
Intent I=new Intent(arg0.getContext(),ASP.class);
startActivityForResult(I, 0);
}
}
}
XML Listview
}
}
XML Listview
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ListView
android:id="@+id/listid"
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
4.Hasil Listview
XML hasil Listview
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".listview">
</android.support.constraint.ConstraintLayout>
Semoga bermanfaat bagi kita semuaaa .........
Wassalamuaallaikum wr.wb ......
Yuresa Mutiara Aglis 08.51 New Google SEO Bandung, Indonesia