I have a listview with a header image , there is extra space top and bottom of the image . I want to remove the space from it ,
My layout is LinierLayout if anyone knows please help ?
my xml files is down
this is the code for list items 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">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:textColor="#android:color/white"
android:id="#+id/tvHeader"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvHeader"
android:orientation="horizontal"
android:paddingLeft="10dp">
<ImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="0dp"
android:layout_marginTop="10dp"
android:paddingBottom="0dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="10dp"/>
<!-- img -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/fname"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_weight="1"/>
<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_marginLeft="10dp"/>
</LinearLayout>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
android:textSize="10sp"
android:paddingTop="10px" />
<TextView
android:text="yyyyyyyyyyyyyyyyy"
android:layout_width="match_parent"
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" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
this is adapter file
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.parse.*;
import com.squareup.picasso.*;
import org.w3c.dom.Text;
import java.io.*;
import java.net.*;
import java.util.*;
public class IndividualsAdaptor extends ArrayAdapter {
private static final int TYPE_SECTION_HEADER = 0;
private static final int TYPE_LIST_ITEM = 1;
protected Context mContext;
ArrayList<Integer> mListHeader = new ArrayList<>();
// 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 int getViewTypeCount() { return 2; }
#Override
public int getItemViewType(int position)
{
if (mListHeader.contains(position)){
return TYPE_SECTION_HEADER;
} else {
return TYPE_LIST_ITEM;
}
}
#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.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader);
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);
// Header
if(getItemViewType(position) == TYPE_SECTION_HEADER){
holder.tvHeader.setVisibility(View.VISIBLE);
holder.tvHeader.setText(String.valueOf(status.charAt(0)));
}else{
holder.tvHeader.setVisibility(View.GONE);
}
// 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("file 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))
.transform(new Rounded( ))
.into(((ImageView) convertView
.findViewById(R.id.imageView)));
return convertView;
}
public static class ViewHolder {
TextView tvHeader;
TextView usernameHomepage;
TextView statusHomepage;
TextView orgName;
TextView pposition;
ImageView logo;
}
#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, FilterResults filterResults) {
clear();
addAll((List<ParseObject>) filterResults.values);
}
};}
public void updateBackupList(List newList){
mBackupList.clear();
mBackupList.addAll(newList);
}
public void updateHeaderList(ArrayList<HashMap> newHeaderList){
for(int i=0; i<newHeaderList.size(); i++){
mListHeader.add(Integer.parseInt((String)newHeaderList.get(i).get("position")));
}
Log.d("Test", mListHeader.toString());
}
}
java file
<LinearLayout
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:orientation="vertical">
<RelativeLayout
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="110dp"
/>
<TextView android:id="#+id/tvEmpty"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#083266"/>
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#color/FBC_RED"
android:layout_below="#id/tvEmpty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="0dp">
</SearchView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_below="#+id/ser1">
<ListView
android:id="#id/android:list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.89" />
<ListView
android:id="#+id/listIndex"
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="none"
android:layout_weight="0.08">
</ListView>
</LinearLayout>
</LinearLayout>
This should work:
ListView listView = getListView();
ImageView mListHeader = new ImageView(getContext());
mListHeader.setImageResource(R.drawable.individuals_img);
mListHeader.setScaleType(ImageView.ScaleType.FIT_XY);
mListHeader.setLayoutParams(new AbsListView.LayoutParams(1400,974));
mListHeader.requestLayout();
listView.addHeaderView(mListHeader);
Inflate a custom layout as your listview header and add below imageview to it
code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.list);
View header = getLayoutInflater().inflate(R.layout.header, list, false); //custom layout
list.addHeaderView(header, null, false);
}
try this: in your header.xml add the header image
<ImageView
android:id="#+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:src="your_source"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:paddingBottom="0dp"
android:scaleType="fitXY" />
Related
I want to be able to add multiple values on a list but properly style the list view.
For example right now the list view will look like this:
namepostcodedate
which is because of the following code
ListArray.add(jobs.get(finalJ).customer.getName() + job.getPostcode() + job.getDate());
The way that I am currently adding the values in the ListArray doesn't seem ideal but I am not sure if there is another way to do this and display the list formated?
This is my list_item file
<?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:orientation="horizontal"
android:padding="5dip">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="#+id/rows2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ddOrangeLight"
android:showDividers="middle">
<!-- android:divider="?android:attr/dividerHorizontal"-->
<TextView
android:id="#+id/customerNameView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="4sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="4sp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</RelativeLayout>
My adapter class
public class SearchableAdapter extends BaseAdapter implements Filterable {
private List<String>originalData = null;
private List<String>filteredData = null;
private final LayoutInflater mInflater;
private final ItemFilter mFilter = new ItemFilter();
public SearchableAdapter(Context context, List<String> data) {
this.filteredData = data ;
this.originalData = data ;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unnecessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.uName = (TextView) convertView.findViewById(R.id.customerNameView);
// holder.uPostCode = (TextView) convertView.findViewById(R.id.postCode);
// holder.UDateTime = (TextView) convertView.findViewById(R.id.dateTimeView);
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// If weren't re-ordering this you could rely on what you set last time
// holder.text.setText(filteredData.get(position));
holder.uName.setText(filteredData.get(position));
// holder.uPostCode.setText(filteredData.get(position));
// holder.UDateTime.setText(filteredData.get(position));
return convertView;
}
static class ViewHolder {
TextView uName;
// TextView uPostCode;
// TextView UDateTime;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
Fragment class
public class CompletedJobsFragment extends Fragment {
AppActivity a;
String search;
TableLayout tableLayout;
Vehicle vehicle;
ListView listView;
SearchableAdapter arrayAdapter;
EditText searchInput;
List<String> ListArray = new ArrayList<String>();
ArrayList<ListItem> results = new ArrayList<>();
ListItem repairDetails = new ListItem();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_completed_jobs,container,false);
a = (AppActivity) getActivity();
assert a != null;
tableLayout = (TableLayout) v.findViewById(R.id.completedJobsTable);
Button clear = v.findViewById(R.id.btnClearTextCarReg);
searchInput = v.findViewById(R.id.txtEditSearchCarReg);
listView = v.findViewById(R.id.list__View);
search = searchInput.getText().toString().trim();
clear.setOnClickListener(av -> searchInput.setText(""));
// Button searchButton = v.findViewById(R.id.btnSearchVehicleReg);
arrayAdapter = new SearchableAdapter(getContext(),ListArray);
listView.setAdapter(arrayAdapter);
listView.setClickable(true);
searchInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
arrayAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
JobRepository jobRepository = new JobRepository(a.getApplication());
VehicleRepository vehicleRepository = new VehicleRepository(a.getApplication());
jobRepository.findCompleted().observe(getViewLifecycleOwner(), jobs -> {
for (int j = 0; j < jobs.size(); j++) {
if (jobs.get(j).job == null) {
continue;
}
int finalJ = j;
vehicleRepository.findByJob(jobs.get(j).job.getUuid()).observe(getViewLifecycleOwner(), vehicles -> {
for (int vh = 0; vh < vehicles.size(); vh++) {
if (vehicles.get(vh).vehicle == null) {
continue;
}
vehicle = vehicles.get(vh).vehicle;
Job job = jobs.get(finalJ).job;
ListArray.add(jobs.get(finalJ).customer.getName() + job.getPostcode() + job.getDate());
View viewToAdd = arrayAdapter.getView(finalJ, null, null);
TableRow[] tableRows = new TableRow[jobs.size()];
tableRows[finalJ] = new TableRow(a);
tableRows[finalJ].setId(finalJ + 1);
tableRows[finalJ].setPadding(0, 20, 0, 20);
tableRows[finalJ].setBackgroundResource(android.R.drawable.list_selector_background);
tableRows[finalJ].setBackgroundResource(R.drawable.table_outline);
tableRows[finalJ].setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
));
tableRows[finalJ].addView(viewToAdd);
tableLayout.addView(tableRows[finalJ], new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT
));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
// Toast.makeText(getContext(), "asd", Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString(JOB_ID, job.getUuid().toString());
bundle.putString(CUSTOMER_NAME, jobs.get(finalJ).customer.getName());
// bundle.putString(JOB_DATE, sdf.format(job.getDate()));
Fragment fragment = new ViewCustomerInformationFragment();
fragment.setArguments(bundle);
FragmentTransaction transaction = requireActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
}
});
}
});
return v;
}
Fragment xml file
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageViewBGhm"
android:scaleType="center"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:id="#+id/txtTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="180dp"
android:text="#string/completed_jobs"
android:textColor="#color/ddGrey"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.375"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/txtEditSearchCarReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:ems="12"
android:inputType="textPersonName"
android:paddingStart="5dp"
android:paddingEnd="10dp"
android:paddingBottom="22dp"
android:textAlignment="textStart"
android:textSize="18sp" />
<TableRow
android:id="#+id/tableTitleRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="6dp"
android:background="#color/lightGrey"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle"
android:visibility="visible">
<TextView
android:id="#+id/txtCustomerNameTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="4sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="4sp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/customer_name_hc"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPostcodeTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/postcode_placeholder"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtDateTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/date"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtTimeTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginEnd="20sp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/enquiry_vat_value"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<ListView
android:id="#+id/list__View"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/tableTitleRow"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:visibility="visible" />
<TableLayout
android:id="#+id/completedJobsTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_marginStart="2dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="2dp"
android:stretchColumns="0,1,2"
android:visibility="gone">
</TableLayout>
<Button
android:id="#+id/btnClearTextCarReg"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignTop="#+id/txtEditSearchCarReg"
android:layout_alignParentEnd="true"
android:layout_marginStart="175dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
android:background="#drawable/ic_outline_cancel_24"
android:backgroundTint="#color/ddOrange"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground" />
<Button
android:id="#+id/btnSearchVehicleReg"
style="#style/DDButtons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/txtEditSearchCarReg"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="14dp"
android:background="#drawable/custom_buttons"
android:drawableStart="#drawable/ic_outline_search_24"
android:drawablePadding="12dp"
android:letterSpacing="0.2"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:singleLine="true"
android:text="#string/search_postcode"
android:textAlignment="textStart"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"/>
</RelativeLayout>
I use fragment and I want to set listview in m fragment.I have error in this field.
I want to use ListView of Foldingcell, but I think I have an error in my adapter.
i can not find error.
any body can't help me?
what should I do?
this is my error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.a700daneh.a700daneh.loanFragment.onCreateView(loanFragment.java:49)
this is my element for using in listView
public class Item {
String family1;
String cost1;
String family2;
String cost2;
String explain;
String code;
int Icon;
private View.OnClickListener requestBtnClickListener;
public Item() {
}
public Item(String family1, String cost1, String family2, String cost2, String explain, String code, int Icon) {
this.family1 = family1;
this.cost1 = cost1;
this.family2 = family2;
this.cost2 = cost2;
this.explain = explain;
this.code = code;
this.Icon = Icon;
//this.time = time;
}
public String getFamily1() {
return family1;
}
public void setFamily1(String family1) {
this.family1 = family1;
}
public String getCost1() {
return cost2;
}
public void setCost1(String cost2) {
this.cost2 = cost2;
}
public String getFamily2() {
return family2;
}
public void setFamily2() {
this.family2 = family2;
}
public String getCost2() {
return cost2;
}
public void setCost2() {
this.cost2 = cost2;
}
public String getExplain() {
return explain;
}
public void setExplain() {
this.explain = explain;
}
public String getCode() {
return code;
}
public void setCode() {
this.code = code;
}
public int getIcon() {
return Icon;
}
/**
* #return List of elements prepared for tests
*/
public static ArrayList<Item> getTestingList() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
items.add(new Item("خانواده x:", "500,000 تومان", "خانواده x:", "500,000 تومان", "توضیحات:", "کد: 12345678", R.drawable.family));
return items;
}
}
this is my adapter called FoldingCellListAdapter
public class FoldingCellListAdapter extends ArrayAdapter<Item> {
private HashSet<Integer> unfoldedIndexes = new HashSet<>();
private View.OnClickListener defaultRequestBtnClickListener;
public FoldingCellListAdapter(Context context, List<Item> objects) {
super(context, 0, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get item for selected view
Item item = getItem(position);
// if cell is exists - reuse it, if not - create the new one from resource
FoldingCell cell = (FoldingCell) convertView;
ViewHolder viewHolder;
if (cell == null) {
viewHolder = new ViewHolder();
LayoutInflater vi = LayoutInflater.from(getContext());
cell = (FoldingCell) vi.inflate(R.layout.cell, parent, false);
// binding view parts to view holder
viewHolder.family1 = (TextView) cell.findViewById(R.id.family1);
viewHolder.code = (TextView) cell.findViewById(R.id.code);
viewHolder.family2 = (TextView) cell.findViewById(R.id.family2);
viewHolder.cost2 = (TextView) cell.findViewById(R.id.cost2);
viewHolder.explain = (TextView) cell.findViewById(R.id.explain);
viewHolder.cost1 = (TextView) cell.findViewById(R.id.cost1);
viewHolder.payment = (TextView) cell.findViewById(R.id.pay);
viewHolder.familyImage1 = (ImageView) cell.findViewById(R.id.familyImage1);
viewHolder.pinkback = (ImageView) cell.findViewById(R.id.pinkback);
viewHolder.familyImage2 = (ImageView) cell.findViewById(R.id.familyImage2);
cell.setTag(viewHolder);
} else {
// for existing cell set valid valid state(without animation)
if (unfoldedIndexes.contains(position)) {
cell.unfold(true);
} else {
cell.fold(true);
}
viewHolder = (ViewHolder) cell.getTag();
}
// bind data from selected element to view through view holder
viewHolder.family1.setText(item.getFamily1());
//viewHolder.time.setText(item.getTime());
viewHolder.code.setText(item.getCode());
viewHolder.family2.setText(item.getFamily2());
viewHolder.cost2.setText(item.getCost2());
viewHolder.explain.setText(String.valueOf(item.getExplain()));
viewHolder.cost1.setText(item.getCost1());
viewHolder.familyImage1.setImageResource(item.getIcon());
viewHolder.familyImage2.setImageResource(item.getIcon());
viewHolder.pinkback.setImageResource(item.getIcon());
return cell;
}
// simple methods for register cell state changes
public void registerToggle(int position) {
if (unfoldedIndexes.contains(position))
registerFold(position);
else
registerUnfold(position);
}
public void registerFold(int position) {
unfoldedIndexes.remove(position);
}
public void registerUnfold(int position) {
unfoldedIndexes.add(position);
}
// View lookup cache
private static class ViewHolder {
TextView family1;
TextView payment;
TextView cost1;
TextView family2;
TextView cost2;
TextView explain;
TextView code;
ImageView familyImage1;
ImageView pinkback;
ImageView familyImage2;
}
}
this is my codes in loanFragment
public class loanFragment extends Fragment {
public loanFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.cell, container, false);
ListView lvlist = (ListView) view.findViewById(R.id.lvlist);
final ArrayList<Item> items = Item.getTestingList();
// create custom adapter that holds elements and their state (we need hold a id's of unfolded elements for reusable elements)
final FoldingCellListAdapter adapter = new FoldingCellListAdapter(getContext(), items);
// set elements to adapter
lvlist.setAdapter(adapter);
// set on click event listener to list view
lvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
// toggle clicked cell state
((FoldingCell) view).toggle(false);
// register in adapter that state for selected cell is toggled
adapter.registerToggle(pos);
}
});
return view;
}
}
I had a layout called cell_content_layout for foldingcell and another layout called cell_title_layout and include these layouts in one layout called cell.
cell_title_content
<?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="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/cell_title_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/cell_content_view"
android:layout_toRightOf="#+id/cell_content_view">
<FrameLayout
android:id="#+id/familyFrame"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#dd0f70">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<ImageView
android:id="#+id/familyImage1"
app:srcCompat="#drawable/family"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:visibility="visible" />
</FrameLayout>
<FrameLayout
android:id="#+id/textFrame1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3d8dd">
<LinearLayout
android:id="#+id/textLinear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/family1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="18dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="۱. خانواده x"
android:textSize="18dp" />
<TextView
android:id="#+id/cost1"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginRight="18dp"
android:gravity="center_vertical"
android:text="۵۰۰,۰۰۰ تومان"
android:textSize="18dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
cell_content_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/cell_content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:orientation="vertical"
>
<FrameLayout
android:id="#+id/textFrame2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#d3d8dd">
<ImageView
android:id="#+id/pinkback"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#dd0f70"
android:visibility="visible" />
<ImageView
android:id="#+id/familyImage2"
android:layout_width="100dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_gravity="left"
android:layout_weight="1"
android:visibility="visible"
app:srcCompat="#drawable/family" />
<LinearLayout
android:id="#+id/textLinear2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/family2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="18dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="۱. خانواده x"
android:textSize="18dp" />
<TextView
android:id="#+id/cost2"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginRight="18dp"
android:gravity="center_vertical"
android:text="۵۰۰,۰۰۰ تومان"
android:textSize="18dp" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/containFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/frameContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<TextView
android:id="#+id/explain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="18dp"
android:layout_marginTop="15dp"
android:layout_weight="1.00"
android:gravity="right"
android:text="توضیحات:" />
<TextView
android:id="#+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="18dp"
android:layout_weight="0.99"
android:gravity="right"
android:text="کد: ۱۲۳۴۵۶۷۸۹" />
<Button
android:id="#+id/pay"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/button_style2"
android:text="پرداخت"
android:textColor="#color/white"
android:textSize="16dp" />
</LinearLayout>
</FrameLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
cell
<?xml version="1.0" encoding="utf-8"?>
<com.ramotion.foldingcell.FoldingCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/folding_cell_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:clipChildren="false"
android:clipToPadding="false">
<include
android:id="#+id/include"
layout="#layout/cell_content_layout"
android:visibility="gone" />
<include
layout="#layout/cell_title_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/include" />
</com.ramotion.foldingcell.FoldingCell>
It looks like your error is in the setAdapter not the onItemClickListener.
My guess is that ListView lvlist = (ListView) view.findViewById(R.id.lvlist); return null;
Check if "R.id.lvlist" is realy the id of your listView in the xml file.
Also, Check if "lvlist" is null after the findViewById line of code
I'm trying to launch a activity from a button inside my gridview item, meaning that when user click on gridview Item it should do one function and when the user click on the button inside the gridview item it should do another function, let me elaborate, here is my single item layout that is being populated inside of my gridview;
<android.support.v7.widget.CardView 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="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/single_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgFood"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:civ_border_color="#ffffff"
app:civ_border_width="2dp"
app:civ_shadow="true"
app:civ_shadow_color="#000000"
app:civ_shadow_radius="10" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imgFood">
<TextView
android:id="#+id/txtName"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currentid"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_below="#+id/txtName"
android:text="Current ID : "
android:visibility="visible"
/>
<TextView
android:id="#+id/studentid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
android:layout_below="#+id/txtName"
android:layout_toRightOf="#+id/currentid"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
/>
</RelativeLayout>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/editit"
android:background="#android:drawable/btn_dialog"/>
</RelativeLayout>
here is how the above layout looks:
and here is my adapter getview from where I try to do the intent/actions from;
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.txtName = (TextView) row.findViewById(R.id.txtName);
holder.txtPrice = (TextView) row.findViewById(R.id.studentid);
holder.imageView = (CircularImageView) row.findViewById(R.id.imgFood);
holder.dit = (Button) row.findViewById(R.id.editit);
final ViewHolder finalHolder = holder;
holder.dit.setOnClickListener(new View.OnClickListener() {
String getname = finalHolder.txtName.getText().toString();
String gethandicap = finalHolder.txtPrice.getText().toString();
#Override
public void onClick(View v) {
// Do something
Intent editintent = new Intent(context, MainActivity.class);
editintent.putExtra("studentname", getname);
editintent.putExtra("studentid", getID);
context.startActivity(editintent);
}
});
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Sudentdb student = studentsList.get(position);
holder.txtName.setText(student.getName());
holder.txtPrice.setText(student.getID());
if (student.getImage() != null && student.getImage().length > 0) {
Glide.with(context)
.load(student.getImage())
.into(holder.imageView);
} else {
holder.imageView.setImageBitmap(null);
}
return row;
}
The problem I'm having is that the button inside the gridview item is not performing any actions, in log it only says ACTION_DOWN and since I added a button inside the item the item onclick also doesn't work.
EDIT; ADDING GRIDVIEW ONCLICK AND LAYOUT
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new StudentListAdapter(this, R.layout.food_items, list);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String dataFromGrid;
dataFromGrid = ((TextView)(view.findViewById(R.id.txtName))).getText().toString();
Intent i = new Intent(getApplicationContext(), Student.class);
i.putExtra("unfromstudent",dataFromGrid);
startActivity(i);
gridview layout (I have tried focusable and clickable)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
>
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:id="#+id/gridView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:columnWidth="120dp"
android:gravity="center"
android:layout_margin="2dp"
android:numColumns="1"
/>
</RelativeLayout>
Change you XML, i have tested below xml its works fine with
GridView
<android.support.v7.widget.CardView 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="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/imgFood"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="10dp"
app:civ_border_color="#ffffff"
app:civ_border_width="2dp"
app:civ_shadow="true"
app:civ_shadow_color="#000000"
app:civ_shadow_radius="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/currentid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtName"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:text="Current ID : "
android:visibility="visible" />
<TextView
android:id="#+id/studentid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtName"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:layout_toRightOf="#+id/currentid"
android:text="ID"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="30dp"
android:layout_height="30dp">
<Button
android:id="#+id/editit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="#android:drawable/btn_dialog"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Your Grid View XML will be Like this.
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true">
</GridView>
I don't have enough reputation so i can't comment your question, why don't you use a recyclerview with a gridLayoutManager? You can see a basic example here .
After that you can implement your recyclerview adapter this way:
public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.ViewHolder> {
private final OnStudentItemClickListener mListener;
private Context mContext;
private List<Student> mStudents;
public StudentsAdapter(Context context, List<Student> items, OnStudentItemClickListener listener) {
mContext = context;
mStudents =items;
mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_student, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Student student = mStudents.get(position);
holder.mTextName.setText(student.getName());
holder.mTextPrice.setText(student.getID());
if (student.getImage() != null && student.getImage().length > 0) {
Glide.with(context)
.load(student.getImage())
.into(holder.imageView);
} else {
holder.imageView.setImageBitmap(null);
}
}
#Override
public int getItemCount() {
return mStudents.size();
}
public interface OnStudentItemClickListener {
void onStudentItemClick(Student student);
void onStudentButtonClick(Student student);
}
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
TextView mTextName;
TextView mTextPrice;
CircleImageView mImageView;
Button mDit;
public ViewHolder(View view) {
super(view);
mTextName = (TextView) view.findViewById(R.id.txtName);
mTextPrice = (TextView) view.findViewById(R.id.studentid);
mImageView = (CircleImageView) view.findViewById(R.id.imgFood);
mDit = (Button) view.findViewById(R.id.editit);
mDit.setOnClickListener(view1 -> {
if (null != mListener) {
mListener.onStudentButtonClick(mStudents.get(getAdapterPosition()));
}
});
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (null != mListener) {
mListener.onStudentItemClick(mStudents.get(getAdapterPosition()));
}
}
}
}
I am working on an Android project in which I am creating UI for editing a Note object in the app. For that, I will be replicating the UI which is designed with JS,HTML,CSS. Unfortunately, android has discrete elements like ListView, TextView and all. As you can see in the screenshot, the top horizontal bar is just a representation and inside it contains a photo.
Also, there is a tab like option in the activity itself. I don't know how to create both these things. The choice of colour I am planning to create with a List and get the element clicked in the List. I have basic functionality working, and working on UI part now.
I don't know what all is required for creating such fancy UI's. Any help would be nice.
edit_note.xml :
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/noteTagEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal" />
<EditText
android:id="#+id/noteTextEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.97"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/noteDate"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/noteNumber"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/attachCount"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<GridView
android:id="#+id/attachmentDisplay"
android:layout_width="match_parent"
android:layout_height="132dp"
android:layout_gravity="center_horizontal"
android:numColumns="2"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
>
<Button
android:id="#+id/saveNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/saveNote" />
<Button
android:id="#+id/cancelEditButton"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
/>
</RelativeLayout>
</LinearLayout>
Java code :
public class EditNoteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
}
public class getNoteByItsId extends AsyncTask<Integer, Void, ResponseEntity<RestNote>> {
#Override
protected void onPostExecute(ResponseEntity<RestNote> params) {
restNote = params.getBody();
restAttachmentList = restNote.getNotesAttachments();
final ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<>();
for (RestAttachment restAttachment : restAttachmentList) {
HashMap<String, String> restAttachmentDisplay = new HashMap<>();
restAttachmentDisplay.put(fileName, restAttachment.getFileName());
restAttachmentDisplay.put(fileSize, readableFileSize(restAttachment.getFileSize()));
restSectionArrayList.add(restAttachmentDisplay);
}
attachmentsGridView = (GridView) findViewById(R.id.attachmentDisplay);
noteDate = (TextView) findViewById(R.id.noteDate);
noteDate.setText(restNote.getNoteDate());
noteNumber = (TextView) findViewById(R.id.noteNumber);
String noteNos = "#" + String.valueOf(restNote.getNoteNumber());
noteNumber.setText(noteNos);
attachCount = (TextView) findViewById(R.id.attachCount);
attachCount.setText(String.valueOf(restNote.getAttachCount()));
editNoteAttachments = new EditNoteAttachments(editNoteActivity, restSectionArrayList);
attachmentsGridView.setAdapter(editNoteAttachments);
}
public class EditNoteAttachments extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public EditNoteAttachments(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.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 vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.attachment_rows, parent, false);
TextView fileName = (TextView) vi.findViewById(R.id.fileName);
TextView fileSize = (TextView) vi.findViewById(R.id.fileSize);
HashMap<String, String> conversationList;
conversationList = data.get(position);
fileName.setText(conversationList.get(EditNoteActivity.fileName));
fileSize.setText(conversationList.get(EditNoteActivity.fileSize));
return vi;
}
}
Kindly let me know. Thank you.
For the top horizontal bar you definitely need a compound custom view like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="#+id/background
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/profileImageView"
android:layout_marginLeft="16dp"
/>
<ImageView
android:id="#+id/profileImageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
/>
</merge>
With code like this:
public class TopBarView extends RelativeLayout {
private TextView title;
private ImageView profileImage;
public TopBarView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.TopBarView, 0, 0);
String titleText = a.getString(R.styleable.TopBarView_titleText);
a.recycle();
setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_top_bar, this, true);
title = (TextView) findViewById(R.id.title);
title.setText(titleText);
profileImage = (ImageView) findViewById(R.id.profileImageView);
profileImage.setBackgroundColor(R.color.yellow);
profileImage.setImageResource(R.drawable.myProfileDrawable);
setBackgroundColor(Color.TRANSPARENT);
}
For tab options you could use android.support.design.widget.TabLayout from design support library.
I am inserting my List in a ListView, Which has some constant image and dynamic images with text.
So I wrote my CustomListViewAdapter, but when I am calling this I am getting NULL pointer exception at
holder.desc.setText(EventItems.getDesc());
in CustomListViewAdapterForEvent.
Here is my complete code.
Events.xml
<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:background="#drawable/drop_shadow"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/imageView2"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="343dp"
android:cacheColorHint="#515151"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:fadeScrollbars="false"
android:fastScrollEnabled="true"
android:scrollX="0px"
android:scrollY="8px"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="20dip"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:smoothScrollbar="true" >
</ListView>
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/cat_header" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView2"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:layout_alignTop="#+id/imageView2" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/menu_icon1" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/add_friend" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/message_icon" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/dollar_3d" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/friend_icons" />
</LinearLayout>
</RelativeLayout>
EventList.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="fill_parent"
android:background="#android:color/transparent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/eventImage"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:contentDescription="#drawable/ic_launcher3"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/sweetIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:contentDescription="#drawable/ic_launcher3"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/aweet_icon" />
<TextView
android:id="#+id/sweetNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="sweetNo" />
<ImageView
android:id="#+id/facebookIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher1" />
<ImageView
android:id="#+id/googleIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher1" />
<ImageView
android:id="#+id/tweeterIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher1" />
<ImageView
android:id="#+id/pIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher1" />
</LinearLayout>
<TextView
android:id="#+id/eventDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="38dp"
android:text="Event Description" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iAmThereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.25"
android:scaleType="fitXY"
android:src="#drawable/i_m_there_icon" />
<ImageView
android:id="#+id/maybeIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:scaleType="fitXY"
android:src="#drawable/may_be_icon" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
CustomListViewAdapterForEvent.java
public class CustomListViewAdapterForEvent extends ArrayAdapter<EventItems> {
Context context;
public CustomListViewAdapterForEvent(Context context, int resourceId,
List<EventItems> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView eventImage;
TextView sweetNo;
TextView desc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.e("TAG ", "1111111111111111");
EventItems EventItems = getItem(position);
Log.e("TAG ", "2222222222222222222");
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
Log.e("TAG ", "3333333333333333333");
if (convertView == null) {
Log.e("TAG ", "444444444444444444444");
convertView = mInflater.inflate(R.layout.event_list, null);
holder = new ViewHolder();
holder.desc = (TextView) convertView.findViewById(R.id.desc);
holder.sweetNo = (TextView) convertView.findViewById(R.id.sweetNo);
holder.eventImage = (ImageView) convertView.findViewById(R.id.eventImage);
Log.e("TAG ", "5555555555555555");
convertView.setTag(holder);
} else{
Log.e("TAG ", "6666666666666666");
holder = (ViewHolder) convertView.getTag();
}
Log.e("TAG ", "77777777777777777777777");
holder.desc.setText(EventItems.getDesc());
Log.e("TAG ", "88888888888888888");
holder.sweetNo.setText(EventItems.getSweetNo());
holder.eventImage.setImageBitmap(EventItems.getEventImage());
return convertView;
}
}
EventItems.java
import android.graphics.Bitmap;
public class EventItems {
private Bitmap eventImage;
private String sweetNo;
private String desc;
public EventItems(Bitmap eventImage, String sweetNo, String desc) {
this.eventImage = eventImage;
this.sweetNo = sweetNo;
this.desc = desc;
}
public Bitmap getEventImage() {
return eventImage;
}
public void setEventImage(Bitmap eventImage) {
this.eventImage = eventImage;
}
public String getSweetNo() {
return sweetNo;
}
public void setSweetNo(String sweetNo) {
this.sweetNo = sweetNo;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Events.java
public class Events extends Activity {
ListView listView;
List<EventItems> rowItems;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final AlertDialog ad=new AlertDialog.Builder(this).create();
rowItems = new ArrayList<EventItems>();
Bitmap bm =getImageBitmap("http://192.168.1.5/Upload/Thumbnail/1/iqygbbfn.jpg");
EventItems item = new EventItems(bm,"hi", "ji");
rowItems.add(item);
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapterForEvent adapter = new CustomListViewAdapterForEvent(this,
R.layout.event_list, rowItems);
listView.setAdapter(adapter);
}
//Function for getting image from URL
private Bitmap getImageBitmap(String url)
{
Bitmap bm = null;
try
{
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e)
{
Log.e("TAG", "Error getting bitmap", e);
}
return bm;
}
}
Take a look on your EventList.xml, you have no element with id desc there, that is why you get null here (but you have eventDesc instead):
holder.desc = (TextView) convertView.findViewById(R.id.desc);
I believe it's just a typo and the correct line should be as following:
holder.desc = (TextView) convertView.findViewById(R.id.eventDesc);
P.S. Please read how to use debugger, it will make your life easier - placing a lot of Log objects is not the best solution for finding error.
Change this
holder.desc = (TextView) convertView.findViewById(R.id.desc);
To
holder.desc = (TextView) convertView.findViewById(R.id.eventDesc);
in your getView(....) into CustomListViewAdapterForEvent