Curved frame for viewing the list.

I am new to android and java, I need to create a simple application in android. I have an Activity page in this activity. Here is my activity page

package com.tkcmu.dev;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class tkcmu extends Activity {
    private ListView lv1;

    @Override
        public void onCreate(Bundle icicle)
        {
        super.onCreate(icicle);
        setContentView(R.layout.main);


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

        try {
            URL recentUrl = new URL(
                    "To some link");
            URLConnection tc = recentUrl.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("content"));
                }
            }

    }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        lv1=(ListView)findViewById(R.id.ListView01);
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listItems));
        }
}

and mine main.xmllooks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:background="@drawable/customshape"
        android:cacheColorHint="#FFFFFF"
        android:clickable="true"
        android:clipToPadding="true"
        android:dividerHeight="1px"
        android:drawSelectorOnTop="false"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:longClickable="true" />
</LinearLayout>

and also has customshape.xmlin drawable, which is equal to

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
</shape>

I got the result when the entire section of the user list became curved, but I want each line to have a curved background. I have placed all my exclude code here AndroidManifest.xml, so it will be appreciated if someone tells me what changes I need in this code. Any body has an idea of ​​how to implement it.

+3
source share
7 answers

add

 android:background="@drawable/customshape" 

list.xml listview, listview

+3

, . , . ( XML).

, lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listItems));

BaseAdapter, getView().

, , .

+2

. .

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="8dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/relative_card_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/country_photo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:contentDescription="@string/action_settings"
                android:scaleType="centerCrop"
                android:src="@drawable/sweden" />

            <TextView
                android:id="@+id/title_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="@string/app_name"
                android:textColor="@color/colorAccent"
                android:textSize="24sp"
                android:textStyle="bold" />

        </RelativeLayout>

        <TextView
            android:id="@+id/title_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:text="@string/app_name"
            android:layout_below="@+id/relative_card_layout"
            android:textColor="@color/colorAccent"
            android:textSize="18sp"
            android:padding="8dp"
            android:textStyle="bold" />
    </RelativeLayout>

</android.support.v7.widget.CardView>
</LinearLayout>
+1

, . . , . -, , : + B android.R.layout.simple_list_item_1. XML- , Android. , (, , ).

+1

- xml, , my_list_item :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:background="@drawable/customshape"
   android:layout_height="wrap_content">
</TextView>

:

lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.my_list_item , listItems));

:

customshape. - , Android (Axtrom, Asus). - :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="10dp"/>
   <solid android:color="#ffffff"/>
</shape>

- xml. textcolor, textize, gravity ..

0

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="5dp"/>
   <solid android:color="#ff0000"/>
</shape>

listView

 android:background="@drawable/yourLayoutName" 
0

CardView. , : com.android.support:cardview-v7:27.0.2. build.gradle

, simple_list_item_1.xml, XML, , ListView !

XML CardView , , simple_list_item_1.xml:

<RelativeLayout
    android:id="main_layout"
    android:width="match_parent"
    android:height="wrap_content">

    ...
    //Other layouts and normal code here.
    ...

</RelativeLayout>

:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:id="main_layout"
        android:width="match_parent"
        android:height="wrap_content">

        ...
        //Other layouts and normal code here.
        ...

    </RelativeLayout>

</android.support.v7.widget.CardView>

card_view:cardCornerRadius CardView, !

P.S. card_view:cardElevation, , , , -!

, ;)

, :

1)

2) A ( RecyclerView)

3) Also disclaimer: traditionally CardViewused only with RecyclerView , this should lead to the fact that a CardViewcan only be used with RecyclerViewand nothing else. This is a common misconception, CardViewis a design element and has nothing to do with RecyclerView, they can be used in any way you want!

This is how rounded would look CardView(minus ListViewi.e. CardViewby itself). See one of my projects.

0
source

All Articles