Dec 28, 2021

Inverted half pyramid

 * * * * *

* * * *

* * * 

* *

*


public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}
1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
public class Main {

  public static void main(String[] args) {
    int rows = 5, k = 0;

    for (int i = 1; i <= rows; ++i, k = 0) {
      for (int space = 1; space <= rows - i; ++space) {
        System.out.print("  ");
      }

      while (k != 2 * i - 1) {
        System.out.print("* ");
        ++k;
      }

      System.out.println();
    }
  }
}


* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for(int i = rows; i >= 1; --i) {
      for(int space = 1; space <= rows - i; ++space) {
        System.out.print("  ");
      }

      for(int j=i; j <= 2 * i - 1; ++j) {
        System.out.print("* ");
      }

      for(int j = 0; j < i - 1; ++j) {
        System.out.print("* ");
      }

      System.out.println();
    }
  }
}

Pyramid Number

 1

1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = 1; i <= rows; ++i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

Pyramid and Pattern

*
* *
* * *
* * * *
* * * * *


 public class Main {


  public static void main(String[] args) {
    int rows = 5;

    for (int i = 1; i <= rows; ++i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}

Kotlin RecyclerView

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation("androidx.recyclerview:recyclerview:1.2.1")
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'


APIService

interface APIService {

@GET("?results=5")
fun fetchUser(): Call<UserResponseNew>

}


RestClient


object RestClient {

private val BASE_URL = "https://randomuser.me/api/"
private var mRetrofit: Retrofit? = null

val client: Retrofit
get() {
if (mRetrofit == null) {
mRetrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return this.mRetrofit!!
}
}


ExampleAdapter


class ExampleAdapter(private val userList: List<ResultsItem>) :
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.item_row, parent,
false
)
return ExampleViewHolder(itemView)
}

override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = userList[position]
holder.textView.text = currentItem.email
Log.d("TAG", "DATA:: " + currentItem.location?.street?.name)
//holder.textView.text = currentItem.results?.get(0)?.email
Glide.with(holder.itemView.context).load(currentItem.picture?.thumbnail).into(holder.imageView);
holder.tvCity.text = currentItem.location?.street?.name
}

override fun getItemCount() = userList.size

class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.tv_item_name)
val imageView: ImageView = itemView.findViewById(R.id.pic)
val tvCity: TextView = itemView.findViewById(R.id.tv_item_city)
}
}
MainActivity
class MainActivity : AppCompatActivity() {

private val TAG = MainActivity::class.java.simpleName

private lateinit var recyclerView: RecyclerView
private var mApiService: APIService? = null


//private var userResponse = UserResponseNew()

private var mUserList: MutableList<ResultsItem> = ArrayList()


//private var categoryList: List<ResultsItem>? = null

//var list = ArrayList<ResultsItem>()
var list: List<ResultsItem> = listOf()

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

//val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView = findViewById<RecyclerView>(R.id.recycler_view)

mApiService = RestClient.client.create(APIService::class.java)

userListAPI()
}

private fun userListAPI() {
val call = mApiService!!.fetchUser()

call.enqueue(object : retrofit2.Callback<UserResponseNew?> {
override fun onResponse(
call: Call<UserResponseNew?>, response: Response<UserResponseNew?>) {

val userResponse = response.body()!!
Log.d(TAG, "data_size:: " + userResponse.results?.size)

mUserList.addAll(userResponse.results!!)

//RecyclerView
recyclerView.layoutManager = LinearLayoutManager(
this@MainActivity,
RecyclerView.VERTICAL, false
)
val exampleAdapter = ExampleAdapter(mUserList)
recyclerView.adapter = exampleAdapter






val responseBody = response.body()!!

//Toast
Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()

//Single record print
Log.d(TAG, "email_1:: " + responseBody.results?.get(0)?.email)
//Log.d(TAG, "title_1:: "+responseBody.results?.get(0)?.name?.title)

//All record print using for loop
for (myData in responseBody.results!!) {
//if (myData != null) {
Log.d(TAG, "email:: " + myData.email)
//}
}
}

override fun onFailure(call: Call<UserResponseNew?>, t: Throwable) {
//Toast
Toast.makeText(this@MainActivity, "Fail", Toast.LENGTH_SHORT).show()
}
})
}

}

Jan 19, 2017

Custom List View

Open your activity_main and paste below code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ravindra.customlistviewdemo.MainActivity">
<ListView
android:id="@+id/customlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Open your single_row and paste below code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_singlerow"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
android:layout_margin="20dp"/>
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_icon"
android:layout_toEndOf="@+id/iv_icon"
android:layout_marginTop="25dp"
android:text="CupCake"/>
</RelativeLayout>
view raw single_row.xml hosted with ❤ by GitHub
Open your CustomListAdapter and paste below code.
package com.ravindra.customlistviewdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Ravindra on 19-Jan-17.
*/
public class CustomListAdapter extends BaseAdapter {
private Context context;
String [] title={"CupCake","Donut","Eclairs","Froyo","GingerBread","HoneyComb","IceCreamSandwitch","JellyBean"};
int [] icon={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
CustomListAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return 8;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row,parent,false);
ImageView imageview= (ImageView) row.findViewById(R.id.iv_icon);
TextView textview= (TextView) row.findViewById(R.id.txt_name);
imageview.setBackgroundResource(icon[position]);
textview.setText(title[position]);
}else{
row = convertView;
}
return row;
}
}
Open your MainActivity and paste below code.
package com.ravindra.customlistviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview= (ListView) findViewById(R.id.customlist);
listview.setAdapter(new CustomListAdapter(this));
}
}

Output :


Array vs ArrayList

*****Array*****

String item[]={"Cupcake","Donut",
            "Eclairs","Froyo","GingerBread","HoneyComb",
            "IceCreamSandWitch","Jellybean","Kitkat",
            "Lollipop", "Marshmallow", "Nougat"};



*****Aray List*****

ArrayList<String> list = new ArrayList<String>();

list.add(textview.getText().toString());
list.add("B");
list.add("C");



*****Array Adapter****

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);



*****Set Adapter*****
listview.setAdapter(adapter);

Dec 16, 2016

Simple List View

Open your activity_main and paste below code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ravindra.listviewdemo.MainActivity">
<ListView
android:id="@+id/simplelist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Open your MainActivity and paste below code.
package com.ravindra.listviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
//String Array for list item
String []item={"Cupcake"," Donut",
" Eclairs"," Froyo"," GingerBread"," HoneyComb",
" IceCreamSandWitch"," Jellybean"," Kitkat",
" Lollipop", "Marshmallow", "Nougat"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview= (ListView) findViewById(R.id.simplelist);
// arrayadapter for listview
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
//set adaapter to listview
listview.setAdapter(adapter);
}
}

Output :