customise autocompletetextview drop-down background - android

how to customise autocompletetextview drop-down background like this image
currently i'm using recyclerViewand custom adapter with filterable but i need to visible and hide recyclerview and it manage to hard that's why i finding solution for customise autocompletetextview dropdown background , any help can appreciative,Thank you in advance

Here is the relevant code that I used.
Custom Typeface
The trick to this one is that I had to set my font to both mAutoCompleteTextView and tvAutocompleteListItem in my Activity.
Remove Shadow
I set the background of mAutoCompleteTextView to be R.drawable.autocomplete_dropdown. In that drawable the important line is
<stroke
android:width="0dip"
android:color="#color/cp_green" />
Radius
The radius was set in R.drawable.autocomplete_dropdown like this:
<corners
android:radius="20dip"/>
MainActivity.java
private void setAutoCompleteListener() {
mAutoCompleteTextView.setDropDownBackgroundDrawable(
mContext.getResources().getDrawable(R.drawable.autocomplete_dropdown));
mAutoCompleteTextView.setAdapter(
new AutoCompleteAdapter(mContext, R.layout.autocomplete_list_item, mLatLng));
mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String autoCompleteText = (String) adapterView.getItemAtPosition(i);
mAutoCompleteTextView.setText(autoCompleteText);
initiateSearch();
hideKeyboard();
}
});
mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
if (editable.length() > 0) {
mClearTextIcon.setVisibility(View.VISIBLE);
} else {
mClearTextIcon.setVisibility(View.INVISIBLE);
}
}
});
}
public void applyFonts() {
Log.d(TAG, "Applying Fonts.");
FontHelper.applyFont(findViewById(R.id.rlMap), mContext);
font = Font.getInstance(getApplicationContext());
mAutoCompleteTextView.setTypeface(font.mAvenirLTStandardLight);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.autocomplete_list_item, null);
TextView tvAutocompleteListItem = (TextView) view.findViewById(R.id.tvAutocompleteListItem);
tvAutocompleteListItem.setTypeface(font.mAvenirLTStandardLight);
}
autocomplete_dropdown.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/white" />
<stroke
android:width="0dip"
android:color="#color/cp_green" />
<corners
android:radius="20dip"/>
<padding
android:left="25dip"
android:top="10dip"
android:right="25dip"
android:bottom="10dip" />
</shape>
autocomplete_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textColor="#color/gray_text"
android:textSize="14sp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/tvAutocompleteListItem"/>
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search"
android:id="#+id/search"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#drawable/search_bar"
android:id="#+id/search_bar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logo_image"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="0dp"
android:id="#+id/ibLogoImage"
android:contentDescription="#string/logo"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/search_icon"
android:layout_centerVertical="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="15dp"
android:layout_alignParentEnd="true"
android:id="#+id/ibSearch"
android:contentDescription="#string/search_hint"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibClearText"
android:layout_toStartOf="#id/ibSearch"
android:background="#drawable/clear_text"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
android:contentDescription="#string/clear" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/actvSearch"
android:hint="#string/search_or_enter_address"
android:background="#color/transparent_white"
android:textSize="14sp"
android:textColor="#color/black"
android:completionThreshold="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/ibLogoImage"
android:layout_toStartOf="#id/ibClearText"
android:dropDownAnchor="#id/search_bar"
android:dropDownVerticalOffset="10dp" />
</RelativeLayout>
</RelativeLayout>

You can use the following function to Change the background of dropdown
setDropDownBackgroundResource();

Related

How can I adjust the size deppending on the visible content in an android view?

I'm creating a list view in an android app and each item expands and collapses.
Here is each item's code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/alarme_layout"
android:backgroundTint="#color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="#+id/textView_aHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="00:00"
android:textSize="26sp"
android:textColor="#color/colorOnPrimary"
android:layout_marginBottom="10dp"/>
<Switch
android:id="#+id/switch_aEstado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="#+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="25dp">
</LinearLayout>
<ImageView
android:id="#+id/imageView_arrowDown"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_detalhesExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora">
<CheckBox
android:id="#+id/checkbox_repetir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/repetir"
android:textColor="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"/>
<LinearLayout
android:id="#+id/layout_diasRepetirButtons"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_repetir"
android:layout_alignStart="#+id/checkbox_repetir"
android:layout_marginTop="10dp"
android:visibility="invisible">
</LinearLayout>
<LinearLayout
android:id="#+id/ultimaLinhaExpanded"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/editText_mensagem"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="wrap_content"
android:hint="#string/mensagemHint"
android:inputType="text"
android:backgroundTint="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"
android:textColor="#color/colorSecondary"
android:shadowColor="#color/colorSecondary"
android:textColorHighlight="#color/colorSecondaryLight" />
<ImageView
android:id="#+id/imageView_arrowUp"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_up_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
And here is the "getView()" method of list view:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.alarme, null);
final Alarme alarme = alarmes.get(position);
//main view
TextView hora = convertView.findViewById(R.id.textView_aHora);
Switch estado = convertView.findViewById(R.id.switch_aEstado);
LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir);
final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown);
//detalhes
final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded);
CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir);
LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons);
EditText editText = convertView.findViewById(R.id.editText_mensagem);
final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
hora.setText(alarme.getHora());
estado.setChecked(alarme.isLigado());
cbRepetir.setChecked(alarme.isRepete());
arrowDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowDown.setVisibility(View.INVISIBLE);
arrowUp.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.VISIBLE);
alarme.setCollapsed(false);
}
});
arrowUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowUp.setVisibility(View.INVISIBLE);
arrowDown.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
alarme.setCollapsed(true);
}
});
editText.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) {
}
#Override
public void afterTextChanged(Editable s) {
alarme.setMensagem(s.toString());
}
});
arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE);
arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
editText.setText(alarme.getMensagem());
return convertView;
}
I thought each item would have its size wrapped depending on the visible content, but I have this result instead:
This is the item when expanded:
https://imgur.com/gallery/oIv50dT
This is the item when collapsed:
https://imgur.com/gallery/dmzw5GU
I wanted the collapsed item to have its size wrapped to the content.
I am going to guess here that in your XML layout you have set a static height.
You need to set it to be wrap_content which allows for dynamic row heights.
I solved it by changing the visibility of the views from View.INVISIBLE to View.GONE.

Drawable gets changed if clicked twice on edit text

I have two edit text views on which I have added two drawables. One is edit_text_bottom_line and another is edit_drawable.
Now I want to show edit_text_bottom_line drawable onClick of an edit text input.So I can see the layout like this :
This worked. Now Again I want to show another drawable when the we again click on edit text input and floating hint appears that time it should look like this:
This also works. But now if I again click on edit text view then it changes the layout edit_text_bottom_line drawable appears like this:
I want to show blue drawable when user starts typing and till he is on that edit text view and after if edit text is focused.
How can I find out if edit text is focused? I tried from onFocusChangeListener if the edit text is focused show the blue drawable but it gets dispappear on next click.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kiranacustomerapp.Activities.SearchActivity"
tools:showIn="#layout/activity_search">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="380dp"
android:background="#android:color/white"
android:orientation="vertical"
android:id="#+id/linearLayoutContainer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:id="#+id/linear1"
android:layout_marginTop="10dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg"
android:id="#+id/linearLayoutSpinner"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/recyclerview"
android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/editTextItemName"
android:layout_gravity="center_horizontal|center_vertical"
android:hint="#string/item_name"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="07dp"
android:textSize="14sp"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_bottom_line"
android:textStyle="bold"
android:inputType="text"
android:textColor="#color/lighttextcolor">
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear2"
android:layout_below="#+id/linear1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg"
android:id="#+id/linearLayoutUnits"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/recyclerviewUnits"
android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:focusable="false"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="07dp"
android:id="#+id/editTextItemUnit"
android:layout_gravity="center_horizontal"
android:hint="#string/unit"
android:textSize="14sp"
android:textStyle="bold"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_bottom_line"
android:inputType="text"
android:textColor="#color/lighttextcolor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear3"
android:layout_below="#+id/linear2">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="02dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="05dp"
android:id="#+id/editTextItemQuantity"
android:layout_gravity="center_horizontal"
android:hint="#string/quantity"
android:textSize="14sp"
android:imeOptions="actionDone"
android:textColorHint="#color/grey"
android:textStyle="bold"
android:background="#drawable/edit_text_bottom_line"
android:inputType="number"
android:textColor="#color/lighttextcolor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_below="#+id/linearp"
android:layout_marginTop="130dp">
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Select"
style="?android:attr/borderlessButtonStyle"
android:id="#+id/buttonSelect"
android:background="#drawable/btn_hlf_blue"
android:textColor="#android:color/white"
android:textSize="12sp"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
code:
containerLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Name.setFocusable(false);
edt_Item_Unit.setFocusable(false);
edt_Item_quantity.setFocusable(false);
linearLayoutRecycleView.setVisibility(View.GONE);
linearLayoutUnits.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.VISIBLE);
textInput_Item_Unit.setVisibility(View.VISIBLE);
textInput_Item_Unit.setBackgroundResource(0);
textInput_Item_name.setBackgroundResource(0);
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Name.setText("");
edt_Item_quantity.setText("");
edt_Item_Unit.setText("");
}
});
edt_Item_Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_Unit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_Name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Name.setFocusableInTouchMode(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
mRecyclerView.setAdapter(mAdapter);
edt_Item_Name.setText("");
edt_Item_Name.setBackgroundResource(R.drawable.edit_text_bottom_line);
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Name.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
linearLayoutRecycleView.setVisibility(View.VISIBLE);
linearLayoutUnits.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.GONE);
textInput_Item_Unit.setVisibility(View.GONE);
textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
textInput_Item_Unit.setBackgroundResource(0);
}
});
edt_Item_Name.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchActivity.this.mAdapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
edt_Item_Unit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Unit.setFocusable(true);
edt_Item_Name.setFocusable(false);
edt_Item_Unit.setFocusableInTouchMode(true);
edt_Item_Name.setBackgroundResource(0);
mUnitsRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
mUnitsRecyclerView.setAdapter(mUnitsAdapter);
edt_Item_Unit.setText("");
edt_Item_Unit.setBackgroundResource(R.drawable.edit_text_bottom_line);
edt_Item_Unit.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
linearLayoutUnits.setVisibility(View.VISIBLE);
linearLayoutRecycleView.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.GONE);
textInput_Item_Unit.setBackgroundResource(R.drawable.purple_bg);
}
});
edt_Item_Unit.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchActivity.this.mUnitsAdapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
edt_Item_quantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_quantity.setFocusableInTouchMode(true);
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
}
else {
}
}
});
Or rather applying two layouts, I have set the accent color as blue in the theme so by default the edit text takes blue color if focused that's fine but when I change the drawable of grey line after that how can I go to default blue line of an edit text?
Please help, thank you..
Just do one thing make your input text layout like this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#android:style/TextAppearance.Small">
<EditText
android:id="#+id/user_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="#dimen/_10sdp"
android:hint="#string/full_name"
android:inputType="text"
android:background="#android:color/transparent"
/>
</android.support.design.widget.TextInputLayout>
And it will solve your problem.
Change your xml TextInputLayout and TextInputEditText with this:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="02dp"
android:textColorHint="#color/grey">
<android.support.design.widget.TextInputEditText
android:id="#+id/editTextItemQuantity"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="05dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="#string/quantity"
android:imeOptions="actionDone"
android:inputType="number"
android:textColor="#color/lighttextcolor"
android:textSize="14sp"
android:textStyle="bold"
android:theme="#style/style_edittext_color_control"/>
</android.support.design.widget.TextInputLayout>
In your res/values/styles.xml add this:
<style name="style_edittext_color_control" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/grey</item>
<item name="colorControlActivated">#color/blue</item>
</style>
Hope this will give you proper result.

Cannot access resources in a layout in a PopupWindow, Objects are null referenced

I have an android application when clicked on an option from a side bar it goes to a fragment, and then into another fragment which has clickable radio buttons. When clicked on these it will create a popup window with some text fields in it.
Basically this is how the flow goes,
Activity --> Fragment 1 --> Fragment 2 --> PopupWindow
When i try to access the resources inside this popupWindow, in example:
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
in the line damageComponenetAutoCompleteTextview.requestFocus(); it gives me an error,
Attempt to invoke virtual method on a null object reference
I believe it's due to a wrong view i'm referring when calling on the Resources. Hope someone could point me to what i'm doing wrong. Thanks in Advance.
This is the method i'm using to create the popupWindow.
public void showDamagedItemEntryPopup(RadioButton radioButton, View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewG = ((ViewGroup)view.findViewById(R.id.damaged_comp_popup));
//I tried passing the root view to inflate() method instead of passing it null. Didn't work.
//View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.popupAnimation);
//I tried setting the content view manually but didnt work
//popupWindow.setContentView(view.findViewById(R.id.damaged_comp_popup));
Button buttonClose = (Button)popupView.findViewById(R.id.close_add_component_btn);
// Close button damaged item popop window
buttonClose.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
originalAmount = (EditText)this.findViewById(R.id.popup_add_component_original_amount);
customerContribution = (EditText)this.findViewById(R.id.popup_percentage);
quantity = (EditText)this.findViewById(R.id.popup_quantity);
finalAmount = (EditText)this.findViewById(R.id.popup_add_component_final_amount);
remarks = (EditText)this.findViewById(R.id.popup_add_component_remarks);
// Item Spinner
itemSpinnerArray = new ArrayList<String>();
itemSpinnerArray.add("Select Item");
// Status Spinner
ArrayList<String> statusSpinnerArray = new ArrayList<String>();
statusSpinnerArray.add("MR");
statusSpinnerArray.add("DR");
statusSpinnerArray.add("SP");
// Error comes at this point initially. When these Resource access line codes are commented, the popup works fine.
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
ArrayAdapter<String> itemSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemSpinnerArray);
damageComponenetAutoCompleteTextview.setThreshold(1);
damageComponenetAutoCompleteTextview.setAdapter(itemSpinnerArrayAdapter);
damageComponenetAutoCompleteTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//int index = cityNames.indexOf(actvCity.getText().toString());
itemSpinnerValue = (String) parent.getItemAtPosition(position);
Log.d("SK-->", "----------------------------------------------------------");
Log.d("SK-->","itemSpinnerValue: " + itemSpinnerValue);
}
});
statusSpinner = (Spinner)this.findViewById(R.id.popup_status_spinner);
ArrayAdapter<String> statusSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, statusSpinnerArray);
statusSpinner.setAdapter(statusSpinnerArrayAdapter);
//Creating a text Watcher
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
//String answerString = originalAmount.getText().toString();
if (originalAmount.getText().toString().trim().equals("") || customerContribution.getText().toString().trim().equals("")
|| quantity.getText().toString().trim().equals("")) {
// Error , one or more editText are empty
}
else
{
calculateFinalAmount();
}
//and now we make a Toast
//modify "yourActivity.this" with your activity name .this
//Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();
}
};
// Adding Text Watcher to our text boxes
originalAmount.addTextChangedListener(textWatcher);
customerContribution.addTextChangedListener(textWatcher);
quantity.addTextChangedListener(textWatcher);
// Show the popup
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
This is the XML i'm using for the popup.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/damaged_comp_popup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/popup_wire">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:background="#color/popup_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/popup_damage_component_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:hint="#string/damaged_componenet_item_string"/>
<Spinner
android:id="#+id/popup_status_spinner"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_damage_component_item"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
<EditText
android:id="#+id/popup_add_component_original_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_damage_component_item"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:layout_toRightOf="#+id/popup_status_spinner"
android:ems="10"
android:hint="#string/original_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_percentage"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_status_spinner"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/percentage_string"
android:inputType="number" />
<TextView
android:id="#+id/popup_percentageMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_toRightOf="#+id/popup_percentage"
android:text="#string/percentage_string"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="1dp"
android:layout_marginRight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<EditText
android:id="#+id/popup_quantity"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_percentage"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_percentageMark"
android:ems="10"
android:hint="#string/quantity_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_final_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_quantity"
android:layout_alignBottom="#+id/popup_quantity"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_quantity"
android:ems="10"
android:hint="#string/final_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/popup_percentage"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/remarks_string"
android:inputType="text|textMultiLine" />
<Button
android:id="#+id/add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_below="#+id/popup_add_component_remarks"
android:background="#drawable/correct"
android:layout_marginBottom="15dp"
android:onClick="onSaveItem"/>
<Button
android:id="#+id/close_add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_add_component_remarks"
android:layout_toRightOf="#+id/add_component_btn"
android:layout_marginLeft="10dp"
android:background="#drawable/cancel"/>
</RelativeLayout>
</LinearLayout>
It seems your damageComponenetAutoCompleteTextview belongs to the popupWindow which you inflated at top.
So try changing
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
To
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) popupView.findViewById(R.id.popup_damage_component_item);
And other relevant elements as well.

How to add badge to list

I want to add badge notification to my arraylist.But I couldn't find a tutorial for this.This is my list code:
public void chatList() {
conversationsAdapter=new ArrayAdapter<JsonObject>(this,0) {
#Override
public View getView(int c_position,View c_convertView,ViewGroup c_parent) {
if (c_convertView == null) {
c_convertView=getLayoutInflater().inflate(R.layout.random_bars,null);
}
JsonObject user=getItem(c_position);
String name=user.get("name").getAsString();
String image_url="http://domain.com/profile/thumb/"+user.get("photo").getAsString();
TextView nameView=(TextView)c_convertView.findViewById(R.id.tweet);
nameView.setText(name);
ImageView imageView=(ImageView)c_convertView.findViewById(R.id.image);
Ion.with(imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
return c_convertView;
}
};
ListView conversationsListView = (ListView)findViewById(R.id.conversationList);
conversationsListView.setAdapter(conversationsAdapter);
conversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startChat(conversationsAdapter.getItem(position));
}
});
}
My list:
I want to add badge to this list.How can I do this ?
Ps:I found a library https://github.com/jgilfelt/android-viewbadger but I don't want to use it.
Inflate the layout in custom listview which extends base adapter instead of array adapter. Add following layout into the custom list item layout.
drawer_list_item.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="48dp"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="#color/counter_text_color"/>
</RelativeLayout>
Use below drawable for background textview counter and set notification count value in textview
counter_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid android:color="#color/counter_text_bg" >
</solid>
<!-- If you want to add some padding -->
<padding
android:right="3dp"
android:left="3dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="2dp" >
</corners>
</shape>

Show Edit text on spinner item selected

I have a Spinner in my class with certain items. I have to display an EditText whose visibility I have set to "Invisible". I need to show this EditText when "Others" item is selected in the spinner.
Here is my code:-
Activity class:-
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintenance);
ETServiceStation = (EditText)findViewById(R.id.ETServiceStation);
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceCost= (EditText)findViewById(R.id.ETServiceCost);
spinServiceType = (Spinner)findViewById(R.id.spinServiceType);
btnMaintenenceSave= (ImageButton)findViewById(R.id.btnMaintenenceSave);
btnMaintenanceClear = (ImageButton)findViewById(R.id.btnMaintenenceClear);
spinServiceType.setOnItemSelectedListener(this);
btnMaintenanceClear.setOnClickListener(this);
btnMaintenenceSave.setOnClickListener(this);
ArrayAdapter<String> serviceTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.array.service_type_array, android.R.layout.simple_spinner_item);
serviceTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinServiceType.setAdapter(serviceTypeAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int servicePos, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(servicePos).toString();
if(spinnerServiceType.matches("Others"))
{
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceType.setVisibility(View.VISIBLE);
spinnerServiceType = ETServiceType.getText().toString();
}
serviceType = spinnerServiceType;
}
}
Resource:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="service_type_array">
<item>Full Service</item>
<item value="serviceother">Other</item>
</string-array>
</resources>
maintenance.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"
android:orientation="vertical"
android:padding="30dp">
<TextView
android:id="#+id/txtMTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maintenance"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#color/SteelBlue"
android:typeface="sans"/>
<TextView
android:layout_marginTop="60dp"
android:id="#+id/txtServiceStation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service station name:" />
<EditText
android:id="#+id/ETServiceStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/txtServiceType"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service type: "/>
<Spinner
android:id="#+id/spinServiceType"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/ETServiceType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:visibility="invisible"
android:ems="10" />
<TextView
android:id="#+id/txtServiceCost"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Servicing expence:" />
<EditText
android:id="#+id/ETServiceCost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnMaintenenceSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:src="#drawable/fuelbtnsaveclick" />
<ImageButton
android:id="#+id/btnMaintenenceClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#color/white"
android:src="#drawable/fuelbtnclearclick" />
</LinearLayout>
</LinearLayout>
Database:
public long createMainEntry(MaintenanceExpense me)
{
ContentValues mcv = new ContentValues();
mcv.put(KEY_SERVICE_STATION,me.getServiceStation());
mcv.put(KEY_SERVICE_TYPE,me.getServiceType());
mcv.put(KEY_SERVICE_COST,me.getServiceCost());
return otherInfoDatabase.insert(MAINTENANCE_DATABASE_TABLE, null, mcv);
}
I have to display the ETServiceType when "Others" item is clicked.
Thanks for any help.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(spinServiceType.getItemAtPosition(position).toString().equalsIgnorecase("Others")
{
editextObject.setVisibility(View.VISIBLE);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(position).toString();
//if spinnerServiceType 's value is other
//then set visibility of textview visible put text watcher on it so u can get text when user finished ..if u dont want then u can use simple getText then change its visibility
//As u are setting your array value from resource you cant update it programmatically
//so first of all u have to take its array items in list . so u can update this list after user enter the value in edit text
}

Categories

Resources