-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add autocomplete, add horizontallayout
- Loading branch information
1 parent
7c22c3b
commit 286582b
Showing
11 changed files
with
682 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
myformbuilder/src/main/java/com/paperplay/myformbuilder/adapter/AutocompleteAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.paperplay.myformbuilder.adapter; | ||
|
||
import android.app.Activity; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Filter; | ||
import android.widget.TextView; | ||
|
||
import com.paperplay.myformbuilder.R; | ||
import com.paperplay.myformbuilder.model.AutocompleteData; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by Ahmed Yusuf on 22/08/19. | ||
*/ | ||
public class AutocompleteAdapter extends ArrayAdapter<AutocompleteData>{ | ||
private ArrayList<AutocompleteData> dataList, tempDataList, suggestions; | ||
private Activity activity; | ||
private int resourceId; | ||
|
||
public AutocompleteAdapter(@NonNull Activity activity, int resource, ArrayList<AutocompleteData> dataList) { | ||
super(activity.getBaseContext(), resource, dataList); | ||
this.activity = activity; | ||
this.resourceId = resource; | ||
this.dataList = dataList; | ||
tempDataList = new ArrayList<>(dataList); | ||
suggestions = new ArrayList<>(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | ||
View view = convertView; | ||
try{ | ||
if(convertView == null){ | ||
LayoutInflater inflater = activity.getLayoutInflater(); | ||
view = inflater.inflate(resourceId, parent, false); | ||
} | ||
AutocompleteData autocompleteData = getItem(position); | ||
TextView itemTitle = view.findViewById(R.id.txtRowAtcTitle); | ||
itemTitle.setText(autocompleteData.getValue()); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return view; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public AutocompleteData getItem(int position) { | ||
return dataList.get(position); | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return dataList.size(); | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public Filter getFilter() { | ||
return dataFilter; | ||
} | ||
|
||
private Filter dataFilter = new Filter() { | ||
|
||
@Override | ||
public CharSequence convertResultToString(Object resultValue) { | ||
AutocompleteData autocompleteData = (AutocompleteData) resultValue; | ||
return autocompleteData.getValue(); | ||
} | ||
|
||
@Override | ||
protected FilterResults performFiltering(CharSequence charSequence) { | ||
if(charSequence != null) { | ||
suggestions.clear(); | ||
for (AutocompleteData autocompleteData : tempDataList) { | ||
if(autocompleteData.getValue().contains(" ")){ | ||
String[] split = autocompleteData.getValue().toLowerCase().split(" "); | ||
for(String sub : split){ | ||
if (sub.startsWith(charSequence.toString().toLowerCase())) { | ||
suggestions.add(autocompleteData); //find for each word | ||
} | ||
} | ||
}else if (autocompleteData.getValue().toLowerCase().startsWith(charSequence.toString().toLowerCase())) { | ||
suggestions.add(autocompleteData); | ||
} | ||
} | ||
FilterResults filterResults = new FilterResults(); | ||
filterResults.values = suggestions; | ||
filterResults.count = suggestions.size(); | ||
return filterResults; | ||
}else{ | ||
return new FilterResults(); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
protected void publishResults(CharSequence charSequence, FilterResults results) { | ||
ArrayList<AutocompleteData> tempValues = (ArrayList<AutocompleteData>)results.values; | ||
if(results != null && results.count > 0){ | ||
clear(); | ||
for (AutocompleteData autocompleteData : tempValues){ | ||
add(autocompleteData); | ||
notifyDataSetChanged(); | ||
} | ||
}else{ | ||
clear(); | ||
notifyDataSetChanged(); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
myformbuilder/src/main/java/com/paperplay/myformbuilder/model/AutocompleteData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.paperplay.myformbuilder.model; | ||
|
||
/** | ||
* Created by Ahmed Yusuf on 22/08/19. | ||
*/ | ||
public class AutocompleteData { | ||
int id; | ||
String secondaryId; | ||
String value; | ||
boolean hidden; | ||
|
||
public AutocompleteData(int id, String secondaryId, String value) { | ||
this.id = id; | ||
this.secondaryId = secondaryId; | ||
this.value = value; | ||
this.hidden = false; | ||
} | ||
|
||
public AutocompleteData(int id, String secondaryId, String value, boolean hidden) { | ||
this.id = id; | ||
this.secondaryId = secondaryId; | ||
this.value = value; | ||
this.hidden = hidden; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getSecondaryId() { | ||
return secondaryId; | ||
} | ||
|
||
public void setSecondaryId(String secondaryId) { | ||
this.secondaryId = secondaryId; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
public boolean isHidden() { | ||
return hidden; | ||
} | ||
|
||
public void setHidden(boolean hidden) { | ||
this.hidden = hidden; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
myformbuilder/src/main/java/com/paperplay/myformbuilder/modules/LayoutBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.paperplay.myformbuilder.modules; | ||
|
||
import android.view.View; | ||
import android.widget.LinearLayout; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by Ahmed Yusuf on 20/05/19. | ||
*/ | ||
public interface LayoutBuilder<T> { | ||
T setVerticalMargin(int margin); | ||
T setHorizontalMargin(int margin); | ||
T setViewList(ArrayList<View> viewList); | ||
T setFormLayout(LinearLayout formLayout); | ||
} |
82 changes: 82 additions & 0 deletions
82
myformbuilder/src/main/java/com/paperplay/myformbuilder/view/HorizontalLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.paperplay.myformbuilder.view; | ||
import android.content.Context; | ||
import android.view.View; | ||
import android.widget.LinearLayout; | ||
import com.paperplay.myformbuilder.modules.LayoutBuilder; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by Ahmed Yusuf on 2019-11-04. | ||
*/ | ||
public class HorizontalLayout { | ||
|
||
public static class Builder implements LayoutBuilder<Builder>, Cloneable{ | ||
int horizontal_margin = 0, vertical_margin = 0; | ||
ArrayList<View> viewList; | ||
Context context; | ||
LinearLayout formLayout; | ||
|
||
public Builder(Context context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public Builder setVerticalMargin(int margin) { | ||
this.vertical_margin = margin; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder setHorizontalMargin(int margin) { | ||
this.horizontal_margin = margin; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder setViewList(ArrayList<View> viewList) { | ||
this.viewList = viewList; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder setFormLayout(LinearLayout formLayout) { | ||
this.formLayout = formLayout; | ||
return this; | ||
} | ||
|
||
public HorizontalLayout create(){ | ||
return new HorizontalLayout(this); | ||
} | ||
|
||
public Object clone() throws CloneNotSupportedException{ | ||
return super.clone(); | ||
} | ||
} | ||
|
||
private HorizontalLayout(Builder builder){ | ||
LinearLayout horizontalLayout = new LinearLayout(builder.context); | ||
horizontalLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, | ||
LinearLayout.LayoutParams.WRAP_CONTENT)); | ||
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL); | ||
|
||
for(int n=0;n<builder.viewList.size();n++){ | ||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( | ||
LinearLayout.LayoutParams.MATCH_PARENT, | ||
LinearLayout.LayoutParams.MATCH_PARENT, | ||
1.0f | ||
); | ||
if(n==0 && builder.viewList.size()>1){ | ||
layoutParams.setMargins(0, builder.vertical_margin,builder.horizontal_margin, builder.vertical_margin); | ||
}else if(builder.viewList.size()>1){ | ||
layoutParams.setMargins(builder.horizontal_margin, builder.vertical_margin, 0, builder.vertical_margin); | ||
} | ||
builder.viewList.get(n).setLayoutParams(layoutParams); | ||
horizontalLayout.addView(builder.viewList.get(n)); | ||
} | ||
if(builder.formLayout!=null){ | ||
LinearLayout formLayout = builder.formLayout; | ||
formLayout.addView(horizontalLayout); | ||
} | ||
} | ||
} |
Oops, something went wrong.