Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Android] Code Snippet

[Android] Code Snippet

Code Snippet for android developers

I'm use Android Studio IDE for code android applications
Download full source code basic listview and customize listview from here

 

 

1. Basic ListView:








Code Snippet Basic ListView
Code Snippet Basic ListView

Code Snippet Basic ListView
Code Snippet Basic ListView


Source code:

package com.example.vilh.demobasiclistview;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class BasicLView extends AppCompatActivity {
    
        Button btnClicked;
        ListView lstView;
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_basic_lview);
    
            lstView = (ListView)findViewById(R.id.listView);
            btnClicked = (Button)findViewById(R.id.buttonClick);
    
            btnClicked.setOnClickListener(new View.OnClickListener() {
                @Override            public void onClick(View v) {
                    Intent main = new Intent(BasicLView.this, MainActivity.class);
                    startActivity(main);
                }
            });
    
    
            final ArrayList<String> arMonHoc = new ArrayList<String>();
            arMonHoc.add("Game Phong Thần");
            arMonHoc.add("Game Võ Lâm 1");
            arMonHoc.add("Game Kiếm Thế");
            arMonHoc.add("Game Võ Lâm 2");
            arMonHoc.add("Game Thiên Long");
            arMonHoc.add("Game Hoa Thiên Cốt");
    
    
            ArrayAdapter adapter = new ArrayAdapter(
                    BasicLView.this,
                    android.R.layout.simple_list_item_1,
                    arMonHoc
            );
    
    
            lstView.setAdapter(adapter);
    
            lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(
                            getApplicationContext(),
                            arMonHoc.get(position),
                            Toast.LENGTH_SHORT                ).show();
                }
            });
    
    
        }
    }
    


2. Custom ListView


Code Snippet Customize ListView
Code Snippet Customize ListView

Use basic ListView:

Code Snippet Customize ListView

Drag 2 pictures to drawable folder
Choose Proejcts before do that!

Code Snippet Customize ListView



Create activity_dong_food.xml (Interface)

Code Snippet Customize ListView



Source code:

clsFood.java

package com.example.vilh.democustomelistview;
    
    /** * Created by Zidane-huuvi168@gmail.com on 4/19/2016. */public class clsFood {
        public String Name;
        public Integer Money;
        public Integer Position;
    
        public clsFood(String name, Integer m, Integer pos) {
            Name = name;
            Money = m;
            Position = pos; /* position of image */
        }
    }
    


ListAdapter.java

package com.example.vilh.democustomelistview;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class ListAdapter extends ArrayAdapter<clsFood> {
    
          
    
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }
    
        // context màn hinh đang đứng    
        // resource = layout đang đứng    
    
    
        public ListAdapter(Context context, int resource, List<clsFood> items) {
            super(context, resource, items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = convertView;
    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.activity_dong_food, null);
            }
    
            clsFood p = getItem(position);
    
            if (p != null) {
                // Anh xa + Gan gia tri        
                TextView tt1 = (TextView) v.findViewById(R.id.textViewNameCourses);
                tt1.setText(p.Name);
    
                TextView tt2 = (TextView) v.findViewById(R.id.textViewYear);
                tt2.setText(String.valueOf(p.Money));
    
    
                ImageView im1 = (ImageView)v.findViewById(R.id.imageViewDescription);
                im1.setImageResource(p.Position);
    
            }
    
            // giao diện từng dòng        return v;
        }
    
    }
    



package com.example.vilh.happyfull;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    
    public class CustomLView extends AppCompatActivity {
    
        Button btnBacked;
        ListView lstCourses;
    
        @Override    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_lview);
    
            lstCourses = (ListView)findViewById(R.id.listViewCourses);
            btnBacked = (Button)findViewById(R.id.buttonBack);
    
            btnBacked.setOnClickListener(new View.OnClickListener() {
                @Override            public void onClick(View v) {
                    Intent main = new Intent(CustomLView.this, MainActivity.class);
                    startActivity(main);
                }
            });
    
            ArrayList<clsFood> arFood = new ArrayList<clsFood>();
            arFood.add(new clsFood("Cơm Xào", 20000, R.drawable.ma1));
            arFood.add(new clsFood("Nước Mía", 5000,  R.drawable.ma2));
            arFood.add(new clsFood("Hủ Tiếu Mì", 25000, R.drawable.ma1));
            arFood.add(new clsFood("Bún Huế", 30000, R.drawable.ma1));
            arFood.add(new clsFood("Phở Hùng", 35000, R.drawable.ma1));
            arFood.add(new clsFood("Combo Cơm + nước", 38000, R.drawable.ma2));
    
    
            ListAdapter adapter = new ListAdapter(
                    CustomLView.this,
                    R.layout.activity_dong_food, // layout tu build 
                    arFood
            );
    
            lstCourses.setAdapter(adapter);
    
        }
    }
    



🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com