I have a ListView created inside a fragment, and it has a search filter, the problem is XML layout showing fine in android studio but when running in the emulator or phone it's showing differently (not properly as I aligned) and also when I click the SearchView it goes under the tab navigation. Can anyone tell how to fix this?
This is the fragment:
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Individuals extends android.support.v4.app.ListFragment
implements FindCallback<ParseObject> {
private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
SearchView sv;
IndividualsAdaptor adaptor;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.individuals, container, false);
}
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
sv = (SearchView) view.findViewById(R.id.ser1);
adaptor = new IndividualsAdaptor(getActivity(), mOrganization);
setListAdapter(adaptor);
ParseQuery.getQuery("_User").findInBackground(this);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adaptor.getFilter().filter(text);
return true;
}
});
}
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " _User");
mOrganization.clear();
mOrganization.addAll(scoreList);
((IndividualsAdaptor) getListAdapter()).updateBackupList(mOrganization);
((IndividualsAdaptor) getListAdapter()).notifyDataSetChanged();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
}
This is the adapter:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import com.parse.ParseObject;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class IndividualsAdaptor extends ArrayAdapter {
protected Context mContext;
// Code for Custom Filter.
protected List mBackupList = new ArrayList();
public IndividualsAdaptor(Context context, List status) {
super(context, R.layout.t3, status);
mContext = context;
// Code for Custom Filter.
mBackupList.addAll(status);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.t3, null);
holder = new ViewHolder();
holder.usernameHomepage = (TextView) convertView.findViewById(R.id.fname);
holder.statusHomepage = (TextView) convertView.findViewById(R.id.lname);
holder.pposition = (TextView) convertView.findViewById(R.id.idposition);
holder.orgName = (TextView) convertView.findViewById(R.id.organizationname);
holder.logo = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject statusObject = (ParseObject) getItem(position);
// title
String username = statusObject.getString("firstname");
holder.usernameHomepage.setText(username);
// content
String status = statusObject.getString("lastname");
holder.statusHomepage.setText(status);
// content
String positions = statusObject.getString("position");
holder.pposition.setText(positions);
// content
String org = statusObject.getString("organizationName");
holder.orgName.setText(org);
// logo
URL url = null;
Bitmap bmp = null;
try {
url = new URL("img hosting location" + statusObject.getString("image"));
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
}catch (IOException e) {
}
holder.logo.setImageBitmap(bmp);
Picasso.with(mContext)
.load(String.valueOf(url))
.into(((ImageView) convertView
.findViewById(R.id.imageView)));
return convertView;
}
public static class ViewHolder {
TextView usernameHomepage;
TextView statusHomepage;
TextView orgName;
TextView pposition;
ImageView logo;
}
// Code for Custom Filter.
#Override
public Filter getFilter() {return new Filter(){
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String queryString = charSequence.toString().toLowerCase();
List<ParseObject> filteredList = new ArrayList<>();
ParseObject tmpItem;
String tmpUsername, tmpStatus, tmpPositions, tmpOrg;
for(int i=0; i<mBackupList.size(); i++){
tmpItem = (ParseObject) mBackupList.get(i);
tmpUsername = tmpItem.getString("firstname").toLowerCase();
tmpStatus = tmpItem.getString("lastname").toLowerCase();
tmpPositions = tmpItem.getString("position").toLowerCase();
tmpOrg = tmpItem.getString("organizationName").toLowerCase();
// The matching condition
if(tmpUsername.contains(queryString)||tmpStatus.contains(queryString)||
tmpPositions.contains(queryString)||tmpOrg.contains(queryString)){
filteredList.add(tmpItem);
}
}
FilterResults filterResults = new FilterResults();
filterResults.count = filteredList.size();
filterResults.values = filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, Filter.FilterResults filterResults) {
clear();
addAll((List<ParseObject>) filterResults.values);
}
};}
public void updateBackupList(List newList){
mBackupList.clear();
mBackupList.addAll(newList);
}
}
The ListView layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp">
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="335dp"
android:text="No data"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="56dp"
android:layout_toLeftOf="#+id/android:list"
android:layout_toStartOf="#+id/android:list" />
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#android:color/holo_red_dark"
android:layout_marginTop="60dp"
android:layout_alignTop="#+id/android:empty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</SearchView>
<ListView android:id="#id/android:list"
android:layout_width="263dp"
android:layout_height="241dp"
android:layout_marginTop="34dp"
android:layout_below="#+id/android:empty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/individuals_img"
android:id="#+id/imageView3"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/android:empty"
android:layout_toEndOf="#+id/android:empty"/>
</RelativeLayout>
The item layout for the ListView:
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_height="110dp"
android:layout_width="110dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="19dp"
/>
<!-- img -->
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
android:textSize="10sp"
android:layout_below="#+id/idposition"
android:layout_alignLeft="#+id/idposition"
android:layout_alignStart="#+id/idposition"
android:paddingTop="10px" />
<TextView
android:text="yyyyyyyyyyyyyyyyy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idposition"
android:textSize="10sp"
android:layout_below="#+id/fname"
android:layout_alignLeft="#+id/fname"
android:layout_alignStart="#+id/fname" />
<TextView
android:id="#+id/fname"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="13dp"
android:fontFamily="sans-serif" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lname"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_above="#+id/idposition"
android:layout_toRightOf="#+id/organizationname"
android:layout_toEndOf="#+id/organizationname" />
</RelativeLayout>
Main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="app.com.anew.fbcapplication.AppHome">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="40dp"
android:background="#drawable/nav"
android:id="#+id/button2"
android:layout_weight="1"
android:layout_width="40dp"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:paddingLeft="25dp"
tools:layout_marginLeft="20dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/i"
android:id="#+id/button"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
app:srcCompat="#drawable/logo"
android:id="#+id/imageView2"
android:layout_weight="1"
tools:layout_marginLeft="15dp"
android:layout_width="280dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_height="50dp" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/TabTextAppearance"
/>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
I get error when trying your ListView (I think that it is individuals.xml), e.g. TextView android:id="#id/android:empty" has an attribute android:layout_below="#+id/button3" but I can't find button3 in your layout.
This may be the layout that you want, individuals.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/individuals_img"
android:src="#drawable/individuals_img"
android:id="#+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/imageView3"
android:layout_toStartOf="#+id/imageView3" >
</SearchView>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No data"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/ser1" />
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ser1" />
</RelativeLayout>
If this is not what you are looking for, please also show what your layout looks like in Android Studio layout editor. Hope it help :)
Related
This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Hello I am developing an app that fetches data from the server the code is shown below
XML:-
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImageQuiz">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e2e2e2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer these questions"
android:textSize="10dp"
android:textAlignment="center"
android:padding="5dp"
android:textColor="#fff"
android:background="#05af43"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recylcerViewImage"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Submit"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textColor="#585858"
android:background="#drawable/rounded_corner"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
ImageQuizAdapter.java
package com.accolade.eventify;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import com.bumptech.glide.Glide;
import java.util.List;
public class ImageQuizAdapter extends RecyclerView.Adapter<ImageQuizAdapter.ImageQuizViewHolder> {
private Context mCtx;
private List<ImageQuizModel> quizList;
public ImageQuizAdapter (Context mCtx, List<ImageQuizModel> quizList) {
this.mCtx = mCtx;
this.quizList = quizList;
}
#Override
public ImageQuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_recycler_image_quiz, null);
return new ImageQuizViewHolder(view);
}
#Override
public void onBindViewHolder(ImageQuizViewHolder holder, int position) {
ImageQuizModel imageQuizModel=quizList.get(position);
Glide.with(mCtx)
.load(imageQuizModel.getImage())
.into(holder.imageView);
//here i used only image
}
#Override
public int getItemCount() {
return quizList.size();
}
public class ImageQuizViewHolder extends RecyclerView.ViewHolder {
RadioButton r1,r2,r3,r4;
ImageView imageView;
public ImageQuizViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView2);
r1 = itemView.findViewById(R.id.radio_button1);
r2 = itemView.findViewById(R.id.radio_button2);
r3 = itemView.findViewById(R.id.radio_button3);
r4 = itemView.findViewById(R.id.radio_button4);
}
}
}
ImagaQuiz.java
package com.accolade.eventify;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ImageQuiz extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://accoladetest.cf/MyApi/MyApiQuizPic.php";
RecyclerView recyclerView;
private Toolbar mTopToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_quiz);
mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar1);
setSupportActionBar(mTopToolbar);
recyclerView = findViewById(R.id.recylcerViewImage);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadProducts();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_favorite) {
Toast.makeText(this, "Add Feature", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadProducts() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
List<ImageQuizModel> data=new ArrayList<>();
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for(int i=0;i< array .length();i++){
JSONObject json_data = array .getJSONObject(i);
ImageQuizModel product = new ImageQuizModel();
product.id= json_data.getInt("id");
product.image= json_data.getString("image");
product.op1= json_data.getString("op1");
product.op2= json_data.getString("op2");
product.op3= json_data.getString("op3");
product.op4= json_data.getString("op4");
data.add(product);
}
//creating adapter object and setting it to recyclerview
ImageQuizAdapter adapter = new ImageQuizAdapter(ImageQuiz.this, data);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
ImageQuizModel.java
package com.accolade.eventify;
public class ImageQuizModel {
public int id;
public String image;
public String op1;
public String op2;
public String op3;
public String op4;
public ImageQuizModel(){
}
public String getImage() {
return image;
}
public int getId(){
return id;
}
public String getOp1() {
return op1;
}
public String getOp2() {
return op2;
}
public String getOp3() {
return op3;
}
public String getOp4() {
return op4;
}
}
layout_recycler_image_guiz.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:elevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button1"
android:text=" Option 1"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button2"
android:text="Option 2"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button3"
android:text="Option 3"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button4"
android:text="Option 4"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
But here the problem is recycler view is displaying in half screen as the image below, I want its width to matchparent
enter image description here
I removed ConstraintLayout and instead I used RelativeLayout as a parent and it is working, Thank you guys for responding.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
</LinearLayout>
</RelativeLayout>
Go to your imageview which you are using and make sure attributes layout height and width both are set to match_parent. Hope this will help.
You should not use match_parent for widgets within a ConstraintLayout. From the documentation:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
If you want a widget to span 100% across its parent, the way to do that in ConstraintLayout is to set the width of the widget to 0dp and specify a start and end constraint as follows for the RelativeLayout which is a child of the ConstraintLayout:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#e2e2e2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
In short, all children of ConstraintLayout should be constrained vertically and horizontally and match_parent should not be used.
I am having a Recyclerview. like this
When a user clicks a row that particular row will get highlighted with the primary color, but when the user takes the finger out it get back to transparent.
But what i want is , i want to maintain the last clicked row highlighted even after the user takes the finger out.
Profile list Row
<?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:layout_marginBottom="10dp"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="#drawable/recyclerview_selector"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/tv_companyName"
android:textColor="#color/colorHeaderText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/txt_bar"
android:layout_toStartOf="#+id/txt_bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="|"
android:id="#+id/txt_bar"
android:textColor="#color/colorHeaderBorder"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/tv_helpDeskName"
android:textColor="#color/colorHeaderText"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txt_bar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp" />
</RelativeLayout>
Recycler_view_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#color/colorPrimaryDark" />
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:state_activated="true"
android:drawable="#color/colorPrimaryDark" />
</selector>
activity_profile
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/colorWhite"
tools:context="com.example.baman.zupportdesk.CompanyProfile">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/company_profile"
android:id="#+id/textView4"
android:textColor="#color/colorBlack"
android:textSize="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:layout_alignTop="#+id/textView4"
android:layout_alignParentLeft="true"
android:background="#color/colorWhite"
android:layout_alignParentStart="true"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/recycler_view_round_corners">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/company_name"
android:layout_marginLeft="18dp"
android:id="#+id/tv_companyName"
android:textColor="#color/colorHeaderText"
android:textStyle="bold"
android:textSize="18dp"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="|"
android:id="#+id/txt_bar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="18dp"
android:textColor="#color/colorHeaderBorder"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/help_desk_name"
android:id="#+id/tv_helpDeskName"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txt_bar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textColor="#color/colorHeaderText"
android:textSize="18dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="292dp"
android:background="#drawable/recycler_view_bottom_corners"
android:scrollbars="vertical"
android:layout_weight="0.57" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_style"
android:textColor="#color/colorWhite"
android:text="#string/submit"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold" />
</RelativeLayout>
Adapter
/**
* Created by baman on 6/25/16.
*/
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.baman.zupportdesk.R;
import java.util.ArrayList;
import java.util.List;
public class CompanyProfileAdapter extends RecyclerView.Adapter<CompanyProfileAdapter.MyViewHolder>{
private List<CompanyProfileData> companyProfileDataList;
private SparseBooleanArray selectedItems;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView comName, hdName;
public MyViewHolder(View view) {
super(view);
comName = (TextView) view.findViewById(R.id.tv_companyName);
hdName = (TextView) view.findViewById(R.id.tv_helpDeskName);
comName.setOnClickListener(this);
hdName.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Log.d("clicked Position", String.valueOf(position));
Log.d("clicked company name",String.valueOf(comName.getText().toString()));
Log.d("clicked hd name",String.valueOf(hdName.getText().toString()));
}
}
public CompanyProfileAdapter(List<CompanyProfileData> companyList) {
this.companyProfileDataList = companyList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.company_profile_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CompanyProfileData CPData = companyProfileDataList.get(position);
holder.comName.setText(CPData.getComName());
holder.hdName.setText(CPData.gethdName());
}
#Override
public int getItemCount() {
return companyProfileDataList.size();
}
}
Profiledata
/**
* Created by baman on 6/25/16.
*/
public class CompanyProfileData {
/* comName - Company Name
* hdName - Help Desk Name */
private String comName, hdName;
public CompanyProfileData(String comName, String hdName) {
this.comName = comName;
this.hdName = hdName;
}
public String getComName(){
return comName;
}
public String gethdName(){
return hdName;
}
public void setcomName(String name){
this.comName = name;
}
public void sethdName(String hdname){
this.hdName = hdname;
}
}
Activity_profile
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.example.baman.zupportdesk.Data.CompanyProfileAdapter;
import com.example.baman.zupportdesk.Data.CompanyProfileData;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class CompanyProfile extends Activity {
private List<CompanyProfileData> CompanyProfileList = new ArrayList<>();
private RecyclerView recyclerView;
private CompanyProfileAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_profile);
Intent intent = getIntent();
String company_data = intent.getStringExtra("companyData");
Log.d("Company data", company_data);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new CompanyProfileAdapter(CompanyProfileList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
try {
prepareCompanyProfileData(company_data);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void prepareCompanyProfileData(String company_data) throws JSONException {
CompanyProfileData CompanyData ;
JSONArray jsonArray = new JSONArray(company_data);
int count = jsonArray.length();
for(int i = 0; i< jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
CompanyData = new CompanyProfileData(jsonObject.getString("CompanyName"), jsonObject.getString("CompanyName"));
CompanyProfileList.add(CompanyData);
}
mAdapter.notifyDataSetChanged();
}
}
First instantiate you selectedItemsin your constructor as below.
public CompanyProfileAdapter(List<CompanyProfileData> companyList) {
this.companyProfileDataList = companyList;
selectedItems = new SparseBooleanArray();
}
Then create a method call selectLastItem() or with whatever name you like as below.
public void selectLastItem(int pos){
selectedItems.clear();
selectedItems.put(pos, true);
notifyDataSetChanged();
}
And then change your onBindViewHolder() method as below.
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CompanyProfileData CPData = companyProfileDataList.get(position);
holder.comName.setText(CPData.getComName());
holder.hdName.setText(CPData.gethdName());
holder.itemView.setBackgroundColor(selectedItems.get(position, false) ? ResourcesCompat.getColor(holder.itemView.getResources(), R.color.colorPrimary, null) : Color.TRANSPARENT);
}
Finally call the selectLastItem method in onClick() as below.
#Override
public void onClick(View v) {
selectLastItem(getAdapterPosition());
}
I think this will solve your problem. Thanks..
Inside your adapter's public MyViewHolder(View view)
write this:
comName.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
comName.setPressed(true);
return true;
}
});
I am new to android and making an with toolbar, drawer, navigation list. and I used CustomlistviewAdapter to fill my list with its data.
The code has no errors however when I run the program close once it starts. the problem is coming from the Listview/Adapter part because when I remove it the program run the main activity successfully (without the list).
would anyone help and figure the problem please.
Previously Thanks for your help.
Main Activity Code:
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
private Toolbar Toolbar;
private ListView Mylist;
private CustomListViewAdapter customListViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar = (Toolbar) findViewById(R.id.toolbar);
assert Toolbar != null;
Toolbar.setLogo(R.drawable.logo);
Toolbar.setNavigationIcon(R.drawable.ericsson);
setSupportActionBar(Toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,Toolbar,R.string.app_name,R.string.app_name);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
final String[] titles = new String[] {"Home" ,"Cabinet Viewer", "Node Viewer", "Connectivity Viewer", "Connection Tracker"};
ArrayList<HashMap<String, String>> itemslist= new ArrayList<>();
for (int i = 0; i < 5; i++){
HashMap<String, String> data = new HashMap<>();
data.put("title", titles[i]);
itemslist.add(data);
}
Mylist = (ListView) findViewById(R.id.navigation_list);
customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), itemslist);
Mylist.setAdapter(customListViewAdapter);
Mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int newposition = position;
String itemClickedId = Mylist.getItemAtPosition(newposition).toString();
Toast.makeText(getApplicationContext(), "ID Clicked: " + itemClickedId, Toast.LENGTH_LONG).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.exit) {
finish();
return true;
}
if (id == R.id.home) {
drawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
}
My CustomListViewUdapter Code:
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;
import java.util.ArrayList;
import java.util.HashMap;
public class CustomListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<HashMap<String, String>> Items;
private static LayoutInflater inflater = null;
public CustomListViewAdapter (Context context, ArrayList<HashMap<String, String>> data){
mContext = context;
Items = data;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null){
view = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)view.findViewById(R.id.listiitemtitle);
ImageView icon = (ImageView)view.findViewById(R.id.listitemicon);
HashMap<String, String> mitems = new HashMap<>();
mitems = Items.get(position);
title.setText(mitems.get("title"));
icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ericsson, null));
}
return null;
}
}
My Main Activity 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.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/navigation"
android:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="esmviewer.myandroid.com.esmviewer.MainActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#color/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Light"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
</LinearLayout>
<ListView
android:layout_width="220dp"
android:layout_height="match_parent"
android:id="#+id/navigation_list"
android:layout_gravity= "start"
android:choiceMode="singleChoice"
android:divider="#color/colorPrimary"
android:dividerHeight="1dp"
android:background="#B9F6CA"/>
</android.support.v4.widget.DrawerLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="esmviewer.myandroid.com.esmviewer.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="#EF5350"
android:id="#+id/Welcome"
android:layout_above="#+id/esm"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You can explore our Datacenters
by clicking on the main menu"
android:textSize="20sp"
android:textAlignment="center"
android:id="#+id/welcome_description"
android:layout_marginBottom="124dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ericsson Services Manager"
android:textSize="25sp"
android:textStyle="bold"
android:id="#+id/esm"
android:textColor="#1A237E"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Viewer Version"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:id="#+id/viewer_version"
android:layout_below="#+id/esm"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageView"
android:background="#drawable/etisalat"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
My CustomList XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:padding="5dp"
android:orientation="horizontal">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iconid"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/listitemicon"
android:src="#drawable/ericsson"
/>
</android.support.v7.widget.LinearLayoutCompat>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item"
android:id="#+id/listiitemtitle"
android:textStyle="bold"
android:textSize="25sp"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
/>
</RelativeLayout>
I can see one mistake in your getView()
It must return the view, not null
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
...
return view; }
The EditText android:id="#+id/adView" works well.
The EditText android:id="#+id/editnumber" often lost focus when I switch Input setting.
For example, the focus will disappear when I change Input setting to handwriting.
sms_step_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:background="#drawable/border_ui"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/adView"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/adView"
android:ems="10" >
<requestFocus />
</EditText>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginTop="5dp"
android:layout_below="#+id/tvTitle"
android:layout_above="#+id/linearLayout1"
android:textSize="16sp" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_alignParentBottom="true"
android:background="#DCDCDC"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnBack"
style="#style/myTextAppearance"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/BtnBack" />
<Button
android:id="#+id/btnNext"
style="#style/myTextAppearance"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/BtnNext" />
<Button
android:id="#+id/btnCancel"
style="#style/myTextAppearance"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/BtnCancel" />
</LinearLayout>
</RelativeLayout>
sms_list_phone_number.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingTop="3dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingBottom="8dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/editnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:layout_weight="1"
android:ems="10" >
</EditText>
<Button
android:id="#+id/btnAddress"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnAddressMin" />
<Button
android:id="#+id/btnDelete"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnDeleteMin" />
</LinearLayout>
</LinearLayout>
StepList.java
package ui;
import info.dodata.smsforward.R;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
public class StepList extends ListActivity{
private ListNumberAdapter mListNumberAdapter=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_step_list);
ListView lv=this.getListView();
List<String> temp=new ArrayList<String>();
temp.add("");
mListNumberAdapter=new ListNumberAdapter(this,temp);
lv.setAdapter(mListNumberAdapter);
}
}
ListNumberAdapter.java
package ui;
import info.dodata.smsforward.R;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ListNumberAdapter extends BaseAdapter {
public final static int ForResult=50;
private LayoutInflater mInflater;
private List<String> mListNumber;
private Context mContext;
public ListNumberAdapter(Context context, List<String> listNumber){
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mListNumber=listNumber;
mContext=context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mListNumber.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.sms_list_phone_number, null);
holder.tvContactName = (TextView) convertView.findViewById(R.id.tvname);
holder.editNumber = (EditText) convertView.findViewById(R.id.editnumber);
holder.btnAddress=(Button)convertView.findViewById(R.id.btnAddress);
holder.btnDelete=(Button)convertView.findViewById(R.id.btnDelete);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
class ViewHolder {
int id;
TextView tvContactName;
EditText editNumber;
Button btnAddress;
Button btnDelete;
}
#ckpatel' answer for Issue with EditText in Listview softkeyboard worked for me.
First write down android:windowSoftInputMode="adjustPan" in Activity in Manifest file to set listview and Edittext and delete android:inputType="numberSigned" from Edittext is help to When you click EditText keyboard open with Alphabetic..