Skip to content

Commit

Permalink
add autocomplete, add horizontallayout
Browse files Browse the repository at this point in the history
  • Loading branch information
c0deslinger committed Nov 5, 2019
1 parent 7c22c3b commit 286582b
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.paperplay.myformbuilder.model.AutocompleteData;
import com.paperplay.myformbuilder.view.HorizontalLayout;
import com.paperplay.myformbuilder.view.MyAutocomplete;
import com.paperplay.myformbuilder.view.MyCheckbox;
import com.paperplay.myformbuilder.view.MyEdittext;
import com.paperplay.myformbuilder.view.MyEdittextMultiple;
Expand Down Expand Up @@ -73,7 +77,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
cityList.add(new SpinnerData(3, "3", "Jakarta"));
MySpinner spinCity = new MySpinner.Builder(ExampleJavaActivity.this)
.setTitle("City").setItem(cityList).setDefaultSelectedValue("Jakarta").setFormLayout(formLayout).create();
// spinCity.hideItemById(2);

//create checkbox
ArrayList<String> educationList = new ArrayList<>();
Expand All @@ -85,6 +88,27 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.setFormLayout(formLayout)
.setOnCheckedListener(selected -> toast("selected: "+selected)).create();

//create horizontal layout
MySpinner spinBaru = new MySpinner.Builder(ExampleJavaActivity.this)
.setTitle("Baru Spin").setItem(cityList).setDefaultSelectedValue("Jakarta").create();
MyEdittext edtBaru = new MyEdittext.Builder(ExampleJavaActivity.this).setTitle("Baru Edit").create();
ArrayList<View> viewList = new ArrayList<>();
viewList.add(spinBaru.getView());
viewList.add(edtBaru.getView());
HorizontalLayout horizontalLayout = new HorizontalLayout.Builder(ExampleJavaActivity.this)
.setHorizontalMargin(25)
.setViewList(viewList)
.setFormLayout(formLayout)
.create();

//create spinner data & view
ArrayList<AutocompleteData> atcList = new ArrayList<>();
atcList.add(new AutocompleteData(1, "1", "Satu"));
atcList.add(new AutocompleteData(1, "1", "Satu Dua"));
atcList.add(new AutocompleteData(3, "3", "Tiga"));
MyAutocomplete myAutocomplete = new MyAutocomplete.Builder(ExampleJavaActivity.this)
.setTitle("Select").setItem(atcList).setFormLayout(formLayout).create();

//create textview
MyTextView myTextView = new MyTextView.Builder(ExampleJavaActivity.this)
.setTitle("Note:").setTitleFont("fonts/Roboto-Bold.ttf")
Expand All @@ -98,7 +122,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
+ " No: " + edtMultiple.getValue("No")
+ " Zip: " + edtMultiple.getValue("Zip")
+ " City: " + spinCity.getSelectedValue() +" - "+spinCity.getSelectedId()+" - "+spinCity.getSelectedSecondaryId()
+ " Education: "+myCheckboxView.getAllChecked()));
+ " Education: "+myCheckboxView.getAllChecked()
+ " Autocomplete: "+myAutocomplete.getSelectedValue()+" "+myAutocomplete.getSelectedId()));

}catch (CloneNotSupportedException e){
e.printStackTrace();
Expand Down
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();
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
* Created by Ahmed Yusuf on 22/08/19.
*/
public class SpinnerAdapter extends ArrayAdapter<SpinnerData>{
ArrayList<SpinnerData> dataList;
Context context;
private ArrayList<SpinnerData> dataList;


public SpinnerAdapter(@NonNull Context context, int resource, ArrayList<SpinnerData> dataList) {
super(context, resource, dataList);
this.context = context;
this.dataList = dataList;
}

Expand Down Expand Up @@ -51,8 +49,4 @@ public View getDropDownView(int position, @Nullable View convertView, @NonNull V
txtItem.setText(dataList.get(position).getValue());
return txtItem;
}

public void setDataList(ArrayList<SpinnerData> dataList) {
this.dataList = dataList;
}
}
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;
}
}
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);
}
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);
}
}
}
Loading

0 comments on commit 286582b

Please sign in to comment.