Making custom layout that should fullfit the spinner - android

My spinner is constrained to guidelines and changes it size depending on the screen size. And then it comes my problem. I have this
its custom_spinner.XML
Its width and height is match constraint
My CustomAdapter
public class MyCustomSpinnerAdapter extends ArrayAdapter<MySpinnerData>
{
private Context context;
private List<MySpinnerData> spinnerData;
private boolean Car = false;
private String initialText;
private boolean alignLeft = false;
public MyCustomSpinnerAdapter(#NonNull Context context,#LayoutRes int resource,List<MySpinnerData> spinnerData)
{
super(context, resource,spinnerData);
spinnerData = spinnerData;
context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent)
{
return myCustomSpinnerView(position, convertView, parent );
}
#Override
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent)
{
return myCustomSpinnerView(position, convertView, parent);
}
private View myCustomSpinnerView(int position, #Nullable View myView, #NonNull ViewGroup parent)
{
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customview = layoutInflater.inflate(R.layout.spinner_layout,parent,false);
TextView textView = (TextView)customview.findViewById(R.id.textView);
textView.setText(spinnerData.get(position).getIconName()); //function that set text
return customview;
}
}
This is what i get
This is custom_spinner.XML
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/my_border"
android:fadeScrollbars="false"
android:fontFamily="#font/titillium_web"
android:gravity="center_horizontal|center_vertical"
android:text="simple"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textView"
app:layout_constraintStart_toStartOf="#+id/textView"
app:layout_constraintTop_toTopOf="#+id/textView"
app:srcCompat="#drawable/autic" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.00" />
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="1" />
And this is activity_main.XML (spinner)
<Spinner
android:id="#+id/Tablice"
android:layout_width="0dp"
android:layout_height="89dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="8dp"
android:background="#null"
android:popupBackground="#color/uplatiParkingDugme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/TablicaDugme"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25"
/>
Width fullfit great , but as soon as i set match constraint as a width parameter of custom_spinner, a dropdown items wont show?
Can anyone help me? Im stuck on this for a weeks..

After i add
customview.measure(0,0);
In MyCustomSpinnerView method, it works
But it displays items with low height , not the height of actual spinner?
What does measure function do , and why it works now?

Related

RecyclerView Item Weird Spacing on phone and Emulator but Normal on Android Studio Preview

I have an app that displays posts in a Recycler View. The recycler View item has an image, a title and a chip group with hashtags
My problem is that I am getting a weird spacing between the chip group and text when I play the app on my phone or emulator but it looks fine on the preview
What this could possible be?
This is the xml for the recycler View item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:clipToPadding="false"
android:gravity="center_horizontal|top"
android:orientation="vertical"
android:padding="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#color/white"
android:clipToPadding="false"
android:elevation="4dp"
android:padding="2dp">
<ImageView
android:id="#+id/imageViewThumbnailRecyclerPost"
android:layout_width="157dp"
android:layout_height="100dp"
android:backgroundTint="#color/dark_gray_eg"
android:foregroundGravity="center"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/thumbnail_preview" />
<ImageView
android:id="#+id/imageViewTypeRecyclerPost"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="3dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="4dp"
android:elevation="3dp"
android:foregroundGravity="center"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/google_slides_icon" />
<ImageView
android:id="#+id/imageViewStatusRecyclerPost"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="3dp"
android:foregroundGravity="center"
android:scaleType="centerInside"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageViewTypeRecyclerPost"
app:srcCompat="#drawable/icon_edited" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textViewTitleRecyclerPost"
android:layout_width="157dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="5dp"
android:inputType="textMultiLine"
android:lines="2"
android:text="A Vinda da fampilia real e a independencia do Brasil"
android:textColor="#color/dark_gray_eg"
android:textSize="13sp"
android:textStyle="bold" />
<com.google.android.material.chip.ChipGroup
android:id="#+id/chipGroupRecyclerPost"
android:layout_width="157dp"
android:layout_height="wrap_content"
android:layout_marginStart="-1dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="-1dp"
app:chipSpacingHorizontal="0dp"
app:chipSpacingVertical="5dp"
app:singleLine="false">
</com.google.android.material.chip.ChipGroup>
</LinearLayout>
This is how it looks like on the preview and should look like on my phone:
This is how it is actually looking like on my phone:
This is how I initialize the recycler:
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
adapterRecyclerSearch = new AdapterRecyclerSearch(getContext(), postsList);
recyclerViewSearch.setLayoutManager(layoutManager);
recyclerViewSearch.setHasFixedSize(true);
recyclerViewSearch.setAdapter(adapterRecyclerSearch);
This is my adapter:
public class AdapterRecyclerSearch extends RecyclerView.Adapter<AdapterRecyclerSearch.MyViewHolder> {
// list of Posts
public List<DatabasePostHolder> postsList = new ArrayList<>();
public Context context;
public StorageReference storage = FirebaseInstances.getStorageReference();
public StorageReference storageThumbnails = storage.child(Constants.STORAGE_FOLDER_THUMBNAILS);
public AdapterRecyclerSearch(Context context, List<DatabasePostHolder> postsList){
// recieve context for glide and posts list to display
this.context = context;
this.postsList = postsList;
}
public void setPostsList(List<DatabasePostHolder> postsList) {
this.postsList = postsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View postView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_post_layout, parent, false);
return new MyViewHolder(postView);
}
#SuppressLint("ResourceAsColor")
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
// set hashtag chips
List<String> tagsList = postHolderInPosition.getTags();
holder.chipGroupHashtags.removeAllViews();
for (int i=0; i<tagsList.size(); i++){
Chip chip = (Chip) LayoutInflater.from(context).inflate(R.layout.chip_hashtag_template, holder.chipGroupHashtags, false);
chip.setText("#" + tagsList.get(i));
holder.chipGroupHashtags.addView(chip);
}
}
#Override
public int getItemCount() {
return postsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
// variables of what will be inside the layout for the adapter
TextView textViewTitle;
ImageView imageThumbnail, imageType, imageStatus;
ChipGroup chipGroupHashtags;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
// define view components from post layouts
textViewTitle = itemView.findViewById(R.id.textViewTitleRecyclerPost);
imageThumbnail = itemView.findViewById(R.id.imageViewThumbnailRecyclerPost);
imageType = itemView.findViewById(R.id.imageViewTypeRecyclerPost);
imageStatus = itemView.findViewById(R.id.imageViewStatusRecyclerPost);
chipGroupHashtags = itemView.findViewById(R.id.chipGroupRecyclerPost);
}
}
}
Adding this to the chip group appearence resource solved it for me
<item name="chipMinTouchTargetSize">0dp</item>
<item name="chipMinHeight">15dp</item>

Linear View in ScrollView with Array Adapter growing up instead of down on new items

I have an Java Android project I am working on wherein I have a LinearLayout that is managed by an array adapter.
My issue is when I add a new item to the array adapter and update the view the List of items is growing upwards instead of down as is visible in the images below.
Why is this now growing down? and how can i make it do so?
Code follows pics
Main Activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"
>
<AnalogClock
android:id="#+id/clockMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<TextView
android:id="#+id/dateMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clockMain"
/>
<TextView
android:id="#+id/titleMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/annie_use_your_telescope"
android:text="#string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dateMsg"
app:layout_constraintVertical_bias="0.05"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
/>
<ListView
android:id="#+id/taskList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5"
android:nestedScrollingEnabled="true"
/>
<Button
android:id="#+id/addNewTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_new_task"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.95"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Task List
<?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"
>
<TextView
android:id="#+id/taskDesc"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textSize="15sp"
android:text="testing task"
android:layout_weight="1"
/>
<TextView
android:id="#+id/taskDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="this is bull shit"
android:layout_weight="1"
/>
<TextView
android:id="#+id/taskTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="some more bullish"
android:layout_weight="1"
/>
</LinearLayout>
inflator
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.task_list, null);
TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
TextView taskTime = (TextView)view.findViewById(R.id.taskTime);
taskDesc.setText(tasks.get(i).description);
taskDate.setText(tasks.get(i).taskDate.toString());
taskTime.setText(tasks.get(i).taskTime.toString());
return view;
}
import java.util.ArrayList;
public class TaskAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<Task> tasks;
public TaskAdapter(Context applicationContext, ArrayList<Task> tasks) {
this.context = applicationContext;
this.tasks = new ArrayList<>(tasks);
inflater = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return tasks.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.task_list, null);
TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
TextView taskTime = (TextView)view.findViewById(R.id.taskTime);
taskDesc.setText(tasks.get(i).description);
taskDate.setText(tasks.get(i).taskDate.toString());
taskTime.setText(tasks.get(i).taskTime.toString());
return view;
}
}
Can you add the array adapter you are using(how you are inserting data in arrayadapter) and where you are passing the array to listview.

empty row in listview

I created a listView which I associated with a adapter. Problem is that rows appear empty even if I set the textview from the view which was created. I mention that the number of rows from listview is right. Thank you.
shopping_page_item
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="#+id/productTitle"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Adapter
public class ShoppingListAdapter extends ArrayAdapter<String> {
public ShoppingListAdapter(Context context, String content[]) {
super(context, R.layout.shopping_page_item, content);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.shopping_page_item, parent, false);
}
TextView editText = convertView.findViewById(R.id.productTitle);
editText.setText(getItem(position).toString());
return convertView;
}
}
Try this
<TextView
android:id="#+id/productTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Your parent layout's height is match_content, make it wrap_content. It may be possible that your one row covering the complete screen.

How to remove the Shadow of the Spinner DropDownView

I use the widget Spinner with a custom adapter and custom views in spinnerMode dropdown. My problem is that I cannot remove the default shadow that casts the dropdown of the Spinner.
Here is the code inside the Activity
mSpinner = (Spinner)findViewById(R.id.spinner);
setSpinnerData();
customSpinnerAdapter = new CustomSpinnerAdapter(this,R.layout.spinner_item,CustomListViewValuesArr );
mSpinner.setAdapter(customSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
itemSelect(pos);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Also this is the Spinner inside the Layout of the Activity:
<Spinner
android:id="#+id/spinner"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:background="#null"
android:padding="0dp"
android:layout_margin="15dp"
android:dropDownVerticalOffset="#dimen/vertical_offset"/>
I use my own custom Adapter :
public class CustomSpinnerAdapter extends ArrayAdapter {
public ArrayList CustomListViewValuesArr;
LayoutInflater inflater;
Typeface myTypeFace;
SpinnerItem item = null;
public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList CustomListViewValuesArr) {
super(context, textViewResourceId, CustomListViewValuesArr);
this.CustomListViewValuesArr = CustomListViewValuesArr;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.myTypeFace = Typeface.createFromAsset(context.getAssets(),"fonts/OpenSans-Regular.ttf");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomDropDownView(position, convertView, parent);
}
public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_drop_down);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.spinner_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_item);
TextView hint = (TextView)row.findViewById(R.id.spinner_hint);
hint.setTypeface(myTypeFace);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}
}
And finally I use custom views for the Dropdown
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:elevation="-6dp"
android:layout_height="35dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/spinnerDropDownItemStyle"
android:singleLine="true"
android:id="#+id/spinner_drop_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#color/spinner_font_color"
android:layout_marginLeft="20dp"
android:textSize="14sp"
android:layout_marginStart="20dp"
android:ellipsize="marquee">
</TextView >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_hint_change_pass"/>
and for the Spinner Item
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:ellipsize="marquee"
android:background="#drawable/spinners_background"
style="#style/spinnerItemStyle"
android:layout_height="35dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spinner_hint"
android:gravity="center"
android:textStyle="bold"
android:textColor="#color/spinner_font_color"
android:textSize="14sp"
android:id="#+id/spinner_hint"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_alignTop="#+id/spinner_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_item"
android:textColor="#color/spinner_header_color"
android:gravity="center"
android:text="sss"
android:textSize="14sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/spinner_hint"
android:layout_toEndOf="#+id/spinner_hint" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="#drawable/down_arrow_spinner"
android:layout_marginRight="17dp"
android:layout_marginEnd="1dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/imageView2" />
What I have tried and didn't worked :
Add Elevation to 0dp in the Spinner widget
Add null background to Spinner
Thanks in Advance !
Use this:
android:popupElevation="0dp"
Eventually I used an attributed of a spinner android:popupBackground="#null" with this the background of the pop up of the spinner is removed so and the shadow does. The only thing the I had to change was to Assign a background of #fff in my custom spinner dropdown item. So I changed it like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="35dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/spinnerDropDownItemStyle"
android:singleLine="true"
android:id="#+id/spinner_drop_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#color/spinner_font_color"
android:layout_marginLeft="20dp"
android:textSize="14sp"
android:layout_marginStart="20dp"
android:ellipsize="marquee">
</TextView >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_hint_change_pass"/>
</RelativeLayout >

Custom Spinner with default down arrow in it

I have a custom layout for spinner. Its both selected and dropdown list item's background color will change depending on user demand. So i am changing depending on their values.. so far so good. But the problem is since i am using custom layout or changing background color, there is no arrow to show that it is a spinner. When i add arrow from layout then all even dropdown list have that arrow with them. I can manipulate it by setting arrow's background to null for drop down items but it is not a good way and dont always work. So how can i show spinner default arrow with my custom spinner without using spinner style. Dont forget that i am changing background of every item.
Here is my custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:weightSum="6" android:id="#+id/spinnerMainLayout"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="14"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp" android:layout_marginTop="8dp"
android:layout_height="wrap_content" android:layout_marginLeft="10dp"
android:text="New Text" android:textSize="19sp" android:layout_weight="5"
android:layout_gravity="center" android:layout_marginBottom="8dp"
android:gravity="center" android:layout_marginRight="4dp"
android:id="#+id/type" android:typeface="serif"/>
<View
android:layout_width="1dp" android:layout_marginLeft="8dp" android:layout_marginTop="4dp"
android:layout_height="match_parent" android:layout_marginBottom="4dp"
android:background="#666666" />
<TextView
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_weight="9"
android:typeface="serif" android:maxLines="3" android:minLines="2"
android:layout_gravity="center" android:layout_marginBottom="8dp"
android:layout_marginLeft="10dp" android:layout_marginRight="5dp"
android:text="sd" android:textSize="18sp" android:gravity="center_vertical"
android:id="#+id/history" />
</LinearLayout>
</LinearLayout>
and here is my custom spinner class
class CustomSpinner extends ArrayAdapter<String> {
private String[] fixStrs = new String[]{"Definition","Result","Error"};
public CustomSpinner(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
//fixStrs = objects;
}
#Override public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
/*LinearLayout spinnerMain = (LinearLayout) mySpinner.findViewById(R.id.spinnerMainLayout);
spinnerMain.setBackgroundColor(Color.parseColor(colors[0]));*/
TextView fixText = (TextView) mySpinner.findViewById(R.id.type);
fixText.setText(fixStrs[position]);
fixText.setTextColor(Color.parseColor(colors[1]));
TextView historyText = (TextView) mySpinner.findViewById(R.id.history);
historyText.setText(history[position]);
historyText.setTextColor(Color.parseColor(colors[1]));
return mySpinner;
}
}
Create a different layout for the spinner dropdown and pass it to the .setDropDownViewResource method of the ArrayAdapter, then you must specify the id of the textview in this layout in the constructor call of your adapter

Categories

Resources