Mohd Aqib Mohd Aqib - 6 months ago 64
Android Question

Listview get Custom Toolbar

This image shows the proplem exactly what it is?Every time I get my toolbar at every text place in my listview. I don't know how to resolve this. I paste my layout file and My java file below. Is possible to generate an OTP of any contact number like 1234567890.
The first question is important.

my xml file is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FF6E26"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name"
android:textColor="#ffffff"
android:typeface="sans"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_vertical" />
</android.support.v7.widget.Toolbar>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/textViewList"
android:layout_width="match_parent"
android:layout_height="30dp"
android:textColor="#ffff66"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />




My code is:

public class ContactList extends Activity {

String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_contact);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_contact, R.id.textViewList, mobileArray);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}

Answer

When you asign a layout for the adapter this repeat the entire layout for each item in the actual layout there is a toolbar widget remove this and keep y the main.xml create a new xml whitout the toolbar activity_main.xml

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#FF6E26"
        android:minHeight="?attr/actionBarSize" />



    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/toolbar"/>

</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3c5b79">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="@string/app_name"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/textViewList"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:textColor="#ffff66"
        android:textSize="20sp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

and in your main activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.textViewList, mobileArray);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
}

finally you have to set in the res/values/styles.xml the attrib parent to

Theme.AppCompat.Light.NoActionBar

NoActionBar