Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
bahram committed Apr 19, 2022
1 parent 0fdc1db commit 50de8b7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 57 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 29 additions & 54 deletions SearchView/src/main/java/com/mrnadimi/searchview/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
Expand All @@ -34,8 +33,6 @@ public class SearchView extends RelativeLayout {

private EditText editText;
private ImageView search, back;
private ProgressBar progressBar;
private RelativeLayout endLayout;
private SearchViewListener<?> searchListener;

public SearchView(Context context) {
Expand Down Expand Up @@ -75,7 +72,7 @@ private void init(Context context , AttributeSet attributeSet , Integer defStyle
//end

//background
int backButtonResource = a.getResourceId(R.styleable.SearchView_sv_backButtonIcon , R.drawable.ic_arrow_back_serach_view_24dp);
int backButtonResource = a.getResourceId(R.styleable.SearchView_sv_backButtonIcon , R.drawable.ic_arrow_back_search_view_24dp);
back.setImageResource(backButtonResource);
//end

Expand Down Expand Up @@ -111,15 +108,6 @@ private void init(Context context , AttributeSet attributeSet , Integer defStyle
}
//end

/*
* Show progressbar icon
*/
boolean showProgressbar = a.getBoolean(R.styleable.SearchView_sv_showProgressbar, false);
if (!showProgressbar){
this.progressBar.setVisibility(GONE);
}
//end



a.recycle();
Expand All @@ -128,51 +116,46 @@ private void init(Context context , AttributeSet attributeSet , Integer defStyle
private void initDefault(Context context){
setGravity(CENTER_VERTICAL);
createBackBtn(context);
createEndLayout(context);
createEditText(context);
createSearchBtn(context );
createProgressBar(context);
createEditText(context);

}

//Fix height
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(getToolBarHeightInPixel(), MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


private void createBackBtn(Context context){
this.back = new ImageView(context);
LayoutParams params = new LayoutParams(getDimenInPixel(R.dimen.size_35dp)
,getDimenInPixel(R.dimen.size_35dp));
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, getToolBarHeightInPixel());
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
int backPadding = getDimenInPixel(R.dimen.size_5dp);
int backPadding = getDimenInPixel(R.dimen.size_10dp);
this.back.setPadding( backPadding,backPadding,backPadding,backPadding );
params.setMarginStart(getDimenInPixel(R.dimen.size_15dp));
params.setMarginStart(getDimenInPixel(R.dimen.size_5dp));
this.back.setLayoutParams(params);
this.back.setId(View.generateViewId());
this.back.setBackgroundResource(getSelectableBackground());
addView(this.back);
}

private void createEndLayout(Context context) {
this.endLayout = new RelativeLayout(context);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
,getToolBarHeightInPixel());
params.setMarginEnd(getDimenInPixel(R.dimen.size_15dp));
params.addRule(RelativeLayout.ALIGN_PARENT_END, 1 );
this.endLayout.setGravity(CENTER_VERTICAL);
this.endLayout.setLayoutParams(params);
this.endLayout.setId(View.generateViewId());
addView(this.endLayout);
}



private void createEditText(Context context){
this.editText = new EditText(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.WRAP_CONTENT);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT
, getToolBarHeightInPixel() );
params.setMarginStart(getDimenInPixel(R.dimen.size_10dp));
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.END_OF, this.back.getId() );
params.addRule(RelativeLayout.START_OF, this.endLayout.getId() );
params.addRule(RelativeLayout.START_OF, this.search.getId() );
this.editText.setBackgroundResource(R.color.sv_transparent);
this.editText.setGravity(Gravity.START);
this.editText.setGravity(Gravity.START|Gravity.CENTER_VERTICAL);
this.editText.setLayoutParams(params);
this.editText.setId(View.generateViewId());
/*
Expand All @@ -197,26 +180,19 @@ private void createEditText(Context context){

private void createSearchBtn(Context context) {
this.search = new ImageView(context);
LayoutParams params = new LayoutParams(getDimenInPixel(R.dimen.size_35dp)
,getDimenInPixel(R.dimen.size_35dp));
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
,getToolBarHeightInPixel());
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
int searchPadding = getDimenInPixel(R.dimen.size_5dp);
params.setMarginEnd(getDimenInPixel(R.dimen.size_5dp));
params.addRule(RelativeLayout.ALIGN_PARENT_END, 1 );
int searchPadding = getDimenInPixel(R.dimen.size_10dp);
this.search.setPadding( searchPadding,searchPadding,searchPadding,searchPadding );
this.search.setLayoutParams(params);
this.search.setId(View.generateViewId());
this.search.setBackgroundResource(getSelectableBackground());
this.endLayout.addView(this.search);
addView(this.search);
}

private void createProgressBar(Context context) {
this.progressBar = new ProgressBar(context);
LayoutParams params = new LayoutParams(getDimenInPixel(R.dimen.size_35dp)
,R.dimen.size_35dp);
this.progressBar.setLayoutParams(params);
this.progressBar.setId(View.generateViewId());
params.addRule(RelativeLayout.END_OF, this.search.getId() );
this.endLayout.addView(this.progressBar);
}

private int getDimenInPixel(@DimenRes int id){
return (int) getResources().getDimension(id);
Expand Down Expand Up @@ -264,12 +240,6 @@ public void showSearchIcon(boolean show){
this.search.setVisibility(show ? VISIBLE : GONE);
}

/*
* If there is not text icon then it will showing
*/
public void showProgressBar(boolean show){
this.progressBar.setVisibility(show ? VISIBLE : GONE);
}

public EditText getEditText(){
return this.editText;
Expand Down Expand Up @@ -323,5 +293,10 @@ public void setSearchListener(SearchViewListener<?> searchListener){
}


public void clearText(){
editText.setText("");
}



}
1 change: 0 additions & 1 deletion SearchView/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<attr name="sv_hintColor" format="integer" />
<attr name="sv_showBackButton" format="boolean" />
<attr name="sv_showSearchIcon" format="boolean" />
<attr name="sv_showProgressbar" format="boolean" />
</declare-styleable>

</resources>
2 changes: 2 additions & 0 deletions SearchView/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<dimen name="size_10dp">10dp</dimen>
<dimen name="size_12dp">12dp</dimen>
<dimen name="size_13dp">13dp</dimen>
<dimen name="size_14dp">14dp</dimen>
<dimen name="size_15dp">15dp</dimen>
<dimen name="size_16dp">16dp</dimen>
<dimen name="size_18dp">18dp</dimen>
<dimen name="size_20dp">20dp</dimen>
<dimen name="size_22dp">22dp</dimen>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center" />
android:layout_gravity="center"
android:text="Hello World!" />

</RelativeLayout>

0 comments on commit 50de8b7

Please sign in to comment.