Showing posts with label List View 2. Show all posts
Showing posts with label List View 2. Show all posts

List View 2

List View 2

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

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

</ListView>

</LinearLayout>

Item Layout
<?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:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
>


<LinearLayout
    android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:padding="10dp"
>

<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/doctor"
android:layout_gravity="center"
/>

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

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

</LinearLayout>
</LinearLayout>

Java
package com.jubayer.webapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ListView listView;

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

listView = findViewById(R.id.listView);

MyAdapter myAdapter = new MyAdapter();

listView.setAdapter(myAdapter);


}

private class MyAdapter extends BaseAdapter{

@Override
public int getCount() {
return 10;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = layoutInflater.inflate(R.layout.item, viewGroup, false);

ImageView imageView = myView.findViewById(R.id.imageView);

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(getApplicationContext(), "Layout\nNumber:"+i, Toast.LENGTH_SHORT).show();

}
});

return myView;
}
}

}