I was creating the list view like picture below
If it there is a possibility of creating new code with another way, i would satisify But unfortunately my app shows Project has stopped Here is my code below (it is a first time that i am writing my code to stack ow.) Thank you beforehand! Here in introduction_info_home.java i have provided with myLabels String array and as usual list view.
java>introduction_info_home.java
public class introduction_info_home extends Fragment {
String[] myLabels={"Introduction to IUT","Scholarships"};
ListView listView;
public introduction_info_home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View intro_info_home=inflater.inflate(R.layout.fragment_introduction_info_home, container, false);
listView=(ListView)intro_info_home.findViewById(R.id.myCustomInfoList);
MyAdapter myAdapter=new MyAdapter(intro_info_home.getContext(),myLabels);
listView.setAdapter(myAdapter);
return intro_info_home;
}
class MyAdapter extends ArrayAdapter<String> implements View.OnClickListener{
Context myContext;
String myTitles[];
public MyAdapter( Context myContext, String[] myTitles) {
super(myContext,R.layout.row, myTitles);
this.myContext = myContext;
this.myTitles = myTitles;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) view.getContext().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView= inflater.inflate(R.layout.row, null,false);
TextView txtListChild = (TextView) myView.findViewById(R.id.text1);
if(i<myTitles.length) txtListChild.setText(myTitles[i]);
if(i==0){
myView.setBackgroundColor(Color.BLUE);
}else if(i==1){
myView.setBackgroundColor(Color.RED);
}else if(i==2){
myView.setBackgroundColor(Color.BLUE);
}else if(i==3){
myView.setBackgroundColor(Color.RED);
}else if(i==4){
myView.setBackgroundColor(Color.BLUE);
}else{
myView.setBackgroundColor(Color.CYAN);
}
return myView;
}
@Override
public void onClick(View view) {
}
}
}
layout>fragment_introduction_info_home.xml
<FrameLayout 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:id="@+id/intro_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context="com.example.bekzod.inhacontest.introduction_info_home">
<ListView
android:id="@+id/myCustomInfoList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
layout>row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0073ff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="32dp"
android:id="@+id/text1"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="32sp"
android:text="Medium text"
/>
</LinearLayout>