Change expandable indicator in ExpandableListView - android

Trying to create an ExpandableListView. The initial view with the groups shows up fine. However, when I click the list item, my arrow does not change. See the images below.
How can I change the arrow's direction?
The layout XML:
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="#null"
android:background="#ffffff"
android:groupIndicator="#drawable/settings_selector"
android:transcriptMode="alwaysScroll" />
settings_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/arrow_down"
android:state_empty="true"/>
<item
android:drawable="#drawable/arrow_right"
android:state_expanded="true"/>
</selector>
</animation-list>

expandable listview
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="#drawable/group_indicator"
android:transcriptMode="alwaysScroll" />
setindicator here iam useing setindicator code like this this working nice
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
res/drawable/group_indicator
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
<item android:drawable="#drawable/arrow_right" android:state_empty="true"> </item>    
<item android:drawable="#drawable/arrow_down" android:state_expanded="true"></item>     
<item android:drawable="#drawable/arrow_right"></item>
</selector>

Try that for your settings_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/arrow_right"
android:state_expanded="true" />
<item
android:drawable="#drawable/arrow_down" />
</selector>

I had gone the way below: decide the left/right drawable for your groupView based on isExpanded flag.
By that way, it is easier for us to customize the padding/background and other things of the indicator drawable.
Hope it helps.
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = (TextView) mLayoutInflater.inflate(R.layout.menu_group, null);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : android.R.drawable.ic_menu_more, 0);
textView.setText(getGroup(groupPosition).toString());
return textView;
}

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MyActivity extends Activity {
private ExpandableListView mExpandableList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setGroupIndicator(null);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 10; i++){
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 10; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
}
public class Parent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}
public class MyCustomAdapter extends BaseExpandableListAdapter implements OnClickListener{
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
#Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
#Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
#Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
#Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
view.findViewById(R.id.button).setTag(i);
view.findViewById(R.id.button).setOnClickListener(this);
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
}
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
/* (non-Javadoc)
* #see android.view.View.OnClickListener#onClick(android.view.View)
* #since Mar 20, 2013
* #author rajeshcp
*/
#Override
public void onClick(View v) {
if(mExpandableList.isGroupExpanded((Integer)v.getTag()))
{
mExpandableList.collapseGroup((Integer)v.getTag());
}else
{
mExpandableList.expandGroup((Integer)v.getTag());
}
}
}
}
Change your MyActivity like this and let me know what else you want ?

subject:
int width = getResources().getDisplayMetrics().widthPixels;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
listView.setIndicatorBounds(width - getPixelValue(40), width - getPixelValue(10));
} else {
listView.setIndicatorBoundsRelative(width - getPixelValue(40), width - getPixelValue(10));
}
and helper method:
public static int getPixelValue(int dp) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}

Just Create a view/Imageview where ever u want in the xml of group item example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/expandable_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="6dp"
android:src="#drawable/group_icon_not_expanded" />
<TextView
android:id="#+id/group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_thin"
android:textColor="#android:color/black"
android:textSize="17sp" />
And then for your ExpandableListView use a GroupClickListener to change the image for ImageView programmatically, example :
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
if (parent.isGroupExpanded(groupPosition)) {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_not_expanded));
} else {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_expanded));
}
return false ;
}
});

I am a bit late to the party but my requirement was something similar to this but something was different.
I had 3 requirements:
if a group have child then show down_arrow (V)
if a group doesn't have a child then no arrow/image
if a group with child expanded then show up_arrow (^)
To achieve something similar to this:
Your ExpandableListView will look something similar to this with android:groupIndicator null
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="#null" />
Your group/header layout will look something like this with text and image at the end:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivDrawerHeaderItemIcon"
android:layout_width="#dimen/dp_30"
android:layout_height="#dimen/dp_30"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/dp_10"
android:layout_marginEnd="#dimen/dp_10"
android:src="#drawable/switch" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/lblListHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp_5"
android:layout_weight="1"
android:fontFamily="#font/roboto_condensed_regular"
android:gravity="center_vertical"
android:padding="#dimen/dp_10"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="#dimen/sp_16" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/dp_15"
android:layout_height="#dimen/dp_15"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/dp_10"
android:layout_marginEnd="#dimen/dp_10" />
In your BaseExpandableListAdapter's getGroupView() method, use
if (Objects.requireNonNull(expandableListDetail.get(expandableListTitle.get(listPosition))).size() > 0) {
if (isExpanded) {
ivIcon.setImageResource(R.drawable.arrow_up);
} else {
ivIcon.setImageResource(R.drawable.arrow_down);
}
}
Where, expandableListTitle is group and expandableListDetail is its children

Related

Drawable is not printed correctly in a listview

I want to create a listview that displays the same clickable icon several times. I tried many ways, including a custom adapter with layout inflater but failed... At this point my ListView displays numbers instead of the icon. Can you help me please? here is my code
public class UserSelectionActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
static ArrayList<Integer> arrayOfIcons = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_selection);
arrayOfIcons.clear();
for (int i=0; i<3; i++) {arrayOfIcons.add(R.drawable.edit);}
ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(this, android.R.layout.simple_list_item_1, arrayOfIcons);
ListView listView2 = (ListView) findViewById(R.id.usersListView2);
listView2.setAdapter(adapter2);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO
}
});
}
}
I don't do anything in onResume method, that's why I didn't share it
And my xml file :
<?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="match_parent">
<ListView
android:id="#+id/usersListView2"
android:layout_width="80dp"
android:layout_height="450dp"
android:layout_alignParentEnd="true"
android:paddingTop="4dip"
android:paddingBottom="3dip" />
</RelativeLayout>
Hy, I suggest you to create a custom adapter:
1) create your list_item:
In res -> layout create a new file named list_item.xml. It contains only one imageView inserted into a ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ImageView
android:id="#+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#mipmap/ic_launcher" />
2) In your activity_main.xml you have to inser your listView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ListView
android:id="#+id/listViewIcon"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
3) Now you have to create a new class call, for example, IconAdapter, where you create your custom adapter:
public class IconAdapter extends BaseAdapter {
Context context;
List<Integer> iconIDList;
/**
*
* #param context = activity context
* #param iconIDList = list with icon's id
*/
public IconAdapter(Context context, List<Integer> iconIDList) {
this.context = context;
this.iconIDList = iconIDList;
}
#Override
public int getCount() {
//return the size of my list
return iconIDList.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
//inflate my view
if (convertView == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.list_item, null);
}
//istantiate my imageView
ImageView imageView = convertView.findViewById(R.id.imageViewIcon);
//set imageView's icon
imageView.setImageResource(iconIDList.get(i));
//return my view
return convertView;
}
}
4) Now in your MainActivity, put all together:D
public class MainActivity extends AppCompatActivity {
List<Integer> iconIDList;
IconAdapter iconAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//istantiate my components
iconIDList = new ArrayList<>();
listView = findViewById(R.id.listViewIcon);
iconAdapter = new IconAdapter(this, iconIDList);
int myIcon = R.drawable.ic_launcher_background;
//populate the list with icon's id
for (int i = 0; i < 5; i++) {
iconIDList.add(myIcon);
}
//set my custom adapter to the listView
listView.setAdapter(iconAdapter);
//set clickListner to the elements of my listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//i is the index of the clicked element
Toast.makeText(MainActivity.this, "Click on element n. " + i, Toast.LENGTH_SHORT).show();
}
});
}
}
I hope that can help you!!! Good job!! If you have a question, comment my answer!!
In your comment you ask me how to add one listener for the name (textView) clicked and one for the icon clicked.
1) modify your list_item.xml file. Now we can use:
a LinearLayout instead of ConstraintLayout;
a textView;
a ImageButton instead of ImageView.
<?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:orientation="horizontal"
android:descendantFocusability="blocksDescendants" > <!--this is very important to detect click-->
<TextView
android:id="#+id/textViewIcon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="left|center"
android:text="TextView"
android:textSize="24sp" />
<ImageButton
android:id="#+id/imageButtonIcon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
app:srcCompat="#mipmap/ic_launcher" />
</LinearLayout>
2) Is better if you create a class that contains your data. We can call this class IconData:
public class IconData {
int iconID;
String text;
//constructor
public IconData(int iconID, String text) {
this.iconID = iconID;
this.text = text;
}
//getter and setter
public int getIconID() {
return iconID;
}
public void setIconID(int iconID) {
this.iconID = iconID;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
3) Now we have to change our IconAdapter.
We'll add an Interface to detect clicks on the ImageButton;
We'll change the list's data type;
We'll instatiate the newest TextView and ImageButton:
//create custom adapter -> I extend my class using BaseAdapter
public class IconAdapter extends BaseAdapter {
//create an interface to comunicate the clicks on the imageButton
public interface MyIconAdapterInterface {
void setOnClickListnerMyImageButton (int position);
}
Context context;
//change the list's name and data type
List<IconData> iconIDTextList;
MyIconAdapterInterface myIconAdapterInterface;
/**
* #param context = activity context
* #param iconIDTextList = list with icon's id and text
* #param myIconAdapterInterface = the interface that mainActivity will implements
*/
public IconAdapter(Context context, List<IconData> iconIDTextList, MyIconAdapterInterface myIconAdapterInterface) {
this.context = context;
this.iconIDTextList = iconIDTextList;
this.myIconAdapterInterface = myIconAdapterInterface;
}
#Override
public int getCount() {
//return the size of my list
return iconIDTextList.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
//inflate my view
if (convertView == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.list_item, null);
}
//istantiate my imageView and textView
ImageButton imageButton = convertView.findViewById(R.id.imageButtonIcon);
TextView textView = convertView.findViewById(R.id.textViewIcon);
//set imageView's button
int image = iconIDTextList.get(i).getIconID();
imageButton.setImageResource(image);
//set text
String text = iconIDTextList.get(i).getText();
textView.setText(text);
//setOnclickListner on my imageButton
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//pass the position of my clicks to the myIconAdapterInterface
myIconAdapterInterface.setOnClickListnerMyImageButton(i);
}
});
return convertView;
}
}
4) Finally we have to change the MainActivity.java's code:
implement our class with the new interface that we have create in our IconAdapter;
modify our list data type;
pass the newest list and the interface to the IconAdapter;
implement the interface's methods.
//I implement my MainActivity with IconAdapter.MyIconAdapterInterface. I create this interface
//in the iconAdapter class
public class MainActivity extends AppCompatActivity implements IconAdapter.MyIconAdapterInterface {
//change to iconIDTextList and change List data type
List<IconData> iconIDTextList;
IconAdapter iconAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//istantiate my components
iconIDTextList = new ArrayList<>();
listView = findViewById(R.id.listViewIcon);
//the second "this" is refer to the IconAdapter.MyIconAdapterInterface
iconAdapter = new IconAdapter(this, iconIDTextList, this);
int myIcon = R.drawable.ic_launcher_background;
String myText = "Hello! ";
//populate the list with icon's id and text
for (int i = 0; i < 5; i++) {
iconIDTextList.add(new IconData(myIcon, myText + i));
}
//set my custom adapter to the listView
listView.setAdapter(iconAdapter);
//set clickListner to the elements of my listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//i is the index of the clicked element
Toast.makeText(MainActivity.this, "Click on element n. " + i, Toast.LENGTH_SHORT).show();
}
});
}
//this is IconAdapter.MyIconAdapterInterface's method that I have to implement
#Override
public void setOnClickListnerMyImageButton(int position) {
//position = click's position
Toast.makeText(this, "Click on image n. "
+ position, Toast.LENGTH_SHORT).show();
}
}
Good Job!!

Float items below ExpandableListview

Before expand
After expand
İf height match_parent override other views
I WANT TO GET LİKE THİS EXPAND AND CLOSE
I WANT TO GET LİKE THİS EXPAND AND CLOSE OTHER EXPANDABLE LİST VİEWS
It is my xml file for showing expandable list etc.
<?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="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="180dp"
android:background="#drawable/sizeozel_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="SİZE ÖZEL"
android:textSize="24sp"
android:layout_marginTop="55dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="95dp"
/>
<ExpandableListView
android:id="#+id/exp_referans_linkim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="105dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="165dp"
/>
<ExpandableListView
android:id="#+id/exp_satislarim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="175dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="235dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="245dp"
android:text="İş Ortaklarım"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="270dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="280dp"
android:text="Sepetim"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="305dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="315dp"
android:text="Alınan Ürünler"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="340dp"
/>
<ExpandableListView
android:id="#+id/exp_bilgilerim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="355dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="420dp"
/>
</RelativeLayout>
My adapter java file for expandable list view
package arabulkazan.albatros.com.arabulkazan.Adapters;
import android.content.Context;
import android.database.DataSetObserver;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import arabulkazan.albatros.com.arabulkazan.MainActivity;
import arabulkazan.albatros.com.arabulkazan.R;
import static arabulkazan.albatros.com.arabulkazan.Fragments.SizeOzel.ref_childListMap;
import static arabulkazan.albatros.com.arabulkazan.Fragments.SizeOzel.ref_groupList;
public class Exp_Ref_List_Adapter implements ExpandableListAdapter {
Context context;
public Exp_Ref_List_Adapter(Context context) {
this.context = context;
}
#Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public int getGroupCount() {
return ref_groupList.size();
}
#Override
public int getChildrenCount(int groupIndex) {
String group = ref_groupList.get(groupIndex);
List<String> childInfoList = ref_childListMap.get(group);
return childInfoList.size();
}
#Override
public Object getGroup(int groupIndex) {
return ref_groupList.get(groupIndex);
}
#Override
public Object getChild(int groupIndex, int childIndex) {
String group = ref_groupList.get(groupIndex);
List<String> childInfoList = ref_childListMap.get(group);
return childInfoList.get(childIndex);
}
#Override
public long getGroupId(int groupIndex) {
return groupIndex;
}
#Override
public long getChildId(int groupIndex, int childIndex) {
return childIndex;
}
#Override
public boolean hasStableIds() {
return true;
}
// This method will return a View object displayed in group list item.
#Override
public View getGroupView(int groupIndex, boolean isExpanded, View view, ViewGroup viewGroup) {
// Create the group view object.
LinearLayout groupLayoutView = new LinearLayout(context);
groupLayoutView.setOrientation(LinearLayout.HORIZONTAL);
// Create and add a textview in returned group view.
String groupText = ref_groupList.get(groupIndex);
TextView groupTextView = new TextView(context);
groupTextView.setText(groupText);
groupTextView.setTextSize(30);
groupLayoutView.addView(groupTextView);
return groupLayoutView;
}
// This method will return a View object displayed in child list item.
#Override
public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View view, ViewGroup viewGroup) {
// First get child text/
Object childObj = this.getChild(groupIndex, childIndex);
String childText = (String)childObj;
// Create a TextView to display child text.
TextView childTextView = new TextView(context);
childTextView.setText(childText);
childTextView.setTextSize(20);
// childTextView.setBackgroundColor(Color.GREEN);
// Set child textview offset left. Then it will align to the right of the group image.
childTextView.setPadding(15,0,0,0);
return childTextView;
}
#Override
public boolean isChildSelectable(int groupIndex, int childIndex) {
return false;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public void onGroupExpanded(int groupIndex) {
}
#Override
public void onGroupCollapsed(int groupIndex) {
}
#Override
public long getCombinedChildId(long groupIndex, long childIndex) {
return 0;
}
#Override
public long getCombinedGroupId(long groupIndex) {
return 0;
}
}
And my main fragment java code
public class Main extends android.support.v4.app.Fragment {
public static List<String> ref_groupList = null;
public static Map<String, List<String>> ref_childListMap = null;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.fragment_main_page, container, false);
this.referans_linkim("Referans Linkim","12345678","kobi45678");
Exp_Ref_List_Adapter exp_ref_list_adapter=new Exp_Ref_List_Adapter(SizeOzel.this.getActivity());
final ExpandableListView exp_ref = (ExpandableListView)root.findViewById(R.id.exp_referans_linkim);
DisplayMetrics metrics = new DisplayMetrics();//for determine indicator position
SizeOzel.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
final float scale = getResources().getDisplayMetrics().density;
int elli= (int) (50 * scale + 0.5f);
int on= (int) (10 * scale + 0.5f);
exp_ref.setIndicatorBounds(width-elli,width-on);
exp_ref.setAdapter(exp_ref_list_adapter);
// Add on group expand listener.
exp_ref.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupIndex) {
// Get total group size.
int groupListSize = ref_groupList.size();
// Close other expanded group.
for(int i=0;i < groupListSize; i++) {
if(i!=groupIndex) {
exp_ref.collapseGroup(i);
}
}
}
});
// Referansım linkim data assign
private void referans_linkim(String name,String temsil_kod,String kobi_kod)
{
if(this.ref_groupList == null)
{
this.ref_groupList = new ArrayList<String>();
}
if(this.ref_childListMap == null)
{
this.ref_childListMap = new HashMap<String, List<String>>();
}
if(!this.ref_groupList.contains(name)) {
this.ref_groupList.add(name);
}
// Create child list.
List<String> childList = new ArrayList<String>();
childList.add("Temsilci Kodu: " + temsil_kod);
childList.add("Kobi Kodu: " + kobi_kod);
// Add child data list in the map, key is group name.
this.ref_childListMap.put(name, childList);
}
}
When i try to expand my expandable listview its expanded.But it compress itself to determine width and height.I want to float items that under expandable list.How can i do that? Thanks in advance

Android Spinner: app crashes onItemSelected [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I came up with problem integrating setting title to my custom spinner. After click on any item crashes with this error.
08-04 02:48:39.490 16956-16956/com.example.ilija.festivalapp_makedox E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ilija.festivalapp_makedox, PID: 16956
java.lang.NullPointerException
at com.example.ilija.festivalapp_makedox.CustomSpinner$1.onItemSelected(CustomSpinner.java:98)
at com.example.ilija.festivalapp_makedox.SpinnerPlus.setSelection(SpinnerPlus.java:21)
at android.widget.Spinner$DropdownPopup$1.onItemClick(Spinner.java:1042)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1282)
at android.widget.ListView.performItemClick(ListView.java:4450)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3174)
at android.widget.AbsListView$3.run(AbsListView.java:3925)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
CustomSpinner.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_spinner);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Movies");
setSupportActionBar(toolbar);
activity = this;
SpinnerPlus SpinnerExample = (SpinnerPlus) findViewById(R.id.spinner);
SpinnerPlus SpinnerExample1 = (SpinnerPlus) findViewById(R.id.spinner1);
SpinnerPlus SpinnerExample2 = (SpinnerPlus) findViewById(R.id.spinner2);
SpinnerPlus SpinnerExample3 = (SpinnerPlus) findViewById(R.id.spinner3);
SpinnerPlus SpinnerExampleTest = (SpinnerPlus) findViewById(R.id.spinnerTest);
/* Spinner SpinnerExampleTest = (Spinner) findViewById(R.id.spinnerTest);
Spinner SpinnerExampleTest1 = (Spinner) findViewById(R.id.spinner1Test);
Spinner SpinnerExampleTest2 = (Spinner) findViewById(R.id.spinner2Test);
Spinner SpinnerExampleTest3 = (Spinner) findViewById(R.id.spinner3Test);*/
output = (TextView)findViewById(R.id.output);
setListData();
setListData1();
setListData2();
setListData3();
setListDataTest();
/* setListData1Test();
setListData2Test();
setListData3Test();*/
Resources res = getResources();
adapter = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr,res);
adapter1 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1,res);
adapter2 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2,res);
adapter3 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3,res);
adapterTest = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArrTest,res);
/*adapter1Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1Test,res);
adapter2Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2Test,res);
adapter3Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3Test,res);*/
adapter.setDropDownViewResource(R.layout.spinner_rows);
SpinnerExample.setPrompt("Select your favorite Planet!");
SpinnerExample.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.layout.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
// if(i>=1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","10");
} else if(position==1) {
intent.putExtra("Cartoons","11");
} else if(position==2) {
intent.putExtra("Cartoons","12");
} else if(position==3) {
intent.putExtra("Cartoons","13");
} else if(position==4) {
intent.putExtra("Cartoons","14");
} else if(position==5) {
intent.putExtra("Cartoons","15");
} else if(position==6) {
intent.putExtra("Cartoons","16");
} else if(position==7) {
intent.putExtra("Cartoons","17");
} else if(position==8) {
intent.putExtra("Cartoons","18");
}
startActivity(intent);
//}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExample1.setAdapter(adapter1);
SpinnerExample1.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
// if(i>2){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","20");
} else if(position==1) {
intent.putExtra("Cartoons","21");
} else if(position==2) {
intent.putExtra("Cartoons","22");
} else if(position==3) {
intent.putExtra("Cartoons","23");
} else if(position==4) {
intent.putExtra("Cartoons","24");
} else if(position==5) {
intent.putExtra("Cartoons","25");
} else if(position==6) {
intent.putExtra("Cartoons","26");
} else if(position==7) {
intent.putExtra("Cartoons","27");
} else if(position==8) {
intent.putExtra("Cartoons","28");
}
startActivity(intent);
// }
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
NothingSelectedSpinnerAdapter.java
package com.example.ilija.festivalapp_makedox;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;
/**
* Decorator Adapter to allow a Spinner to show a 'Nothing Selected...' initially
* displayed instead of the first choice in the Adapter.
*/
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
/**
* Use this constructor to have NO 'Select One...' item, instead use
* the standard prompt or nothing at all.
* #param spinnerAdapter wrapped Adapter.
* #param nothingSelectedLayout layout for nothing selected, perhaps
* you want text grayed out like a prompt...
* #param context
*/
public NothingSelectedSpinnerAdapter(
SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
/**
* Use this constructor to Define your 'Select One...' layout as the first
* row in the returned choices.
* If you do this, you probably don't want a prompt on your spinner or it'll
* have two 'Select' rows.
* #param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0)
* #param nothingSelectedLayout layout for nothing selected, perhaps you want
* text grayed out like a prompt...
* #param nothingSelectedDropdownLayout layout for your 'Select an Item...' in
* the dropdown.
* #param context
*/
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
/**
* View to show in Spinner with Nothing Selected
* Override this to do something dynamic... e.g. "37 Options Found"
* #param parent
* #return
*/
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
// Spinner does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ?
new View(context) :
getNothingSelectedDropdownView(parent);
}
// Could re-use the convertView if possible, use setTag...
return adapter.getDropDownView(position - EXTRA, null, parent);
}
/**
* Override this to do something dynamic... For example, "Pick your favorite
* of these 37".
* #param parent
* #return
*/
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
#Override
public int getCount() {
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
#Override
public Object getItem(int position) {
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
#Override
public int getItemViewType(int position) {
return 0;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public long getItemId(int position) {
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
#Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return adapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
}
SpinnerPlus.java:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AdapterView;
import android.widget.Spinner;
import java.lang.reflect.Field;
public class SpinnerPlus extends Spinner {
AdapterView.OnItemSelectedListener listener;
public SpinnerPlus(Context context, AttributeSet attrs) {
super(context,attrs);
}
#Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(this, getSelectedView(), position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
AdapterView.OnItemSelectedListener listener) {
this.listener = listener;
}
}
CustomAdapter.java
package com.example.ilija.festivalapp_makedox;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by ilija on 15-Jul-16.
*/
public class CustomAdapter extends ArrayAdapter<String>{
private Activity activity;
private ArrayList data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;
public CustomAdapter(
CustomSpinner activitySpinner,
int textViewResourceId,
ArrayList objects,
Resources resLocal
) {
super(activitySpinner, textViewResourceId, objects);
activity = activitySpinner;
data = objects;
res=resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getDropDownView(int position, View convertView, ViewGroup parent){
return getCustomView(position,convertView,parent);
}
public View getView(int position, View convertView, ViewGroup parent){
return getCustomView(position,convertView,parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent){
View row = inflater.inflate(R.layout.spinner_rows, parent, false);
tempValues = null;
tempValues = (SpinnerModel) data.get(position);
TextView label = (TextView)row.findViewById(R.id.movieName);
ImageView image = (ImageView)row.findViewById(R.id.imageSpinner);
RelativeLayout backgorund = (RelativeLayout)row.findViewById(R.id.spinnerBackground);
label.setText(tempValues.getMovieName());
image.setImageResource(res.getIdentifier("com.example.ilija.festivalapp_makedox:drawable/"+tempValues.getImage(),null,null));
//backgorund.setBackgroundColor(tempValues.getBackground());
return row;
}
}
activity_custom_spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:agendaCalendar="http://schemas.android.com/apk/res-auto"
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:fitsSystemWindows="true"
tools:context=".MainScreen">
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--tools:context=".MainScreen"-->
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MainScreen">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="55dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:background="#drawable/makecover"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<ScrollView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="155dp"
android:layout_marginBottom="10dp"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/ico"
android:text="SELECTIONS:"
android:paddingTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp" />
<TextView
android:id="#+id/ico1"
android:text="INTERNATIONAL COMPETITION"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:paddingTop="5dp"/>
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinner"
android:background="#color/main_competition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/ico2"
android:text="NEWCOMERS COMPETITION"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- <com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinner1"
android:background="#color/newcomers"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/ico3"
android:text="FOCUS RUSSIA"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinner2"
android:background="#color/russian"
android:spinnerMode="dropdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/ico4"
android:text="SHORT DOX"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinner3"
android:background="#color/short1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/spinnerTesttxt"
android:text="STUDENT DOX"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:paddingTop="5dp"/>
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="#+id/spinnerTest"
android:background="#color/student"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!--<TextView
android:id="#+id/ico2spinnerTest"
android:text="ActionSpinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/spinner1Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/ico3spinnerTest"
android:text="HorrorspinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/spinner2Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/ico4spinnerTest"
android:text="CartoonspinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="#+id/spinner3Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
</LinearLayout>
</ScrollView>
<include layout="#layout/content_custom_spinner" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main" app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
May be ((TextView) view.findViewById(R.id.movieName)).getText().toString(); is null. Take your String from ListViewValues
SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = (String) CustomListViewValuesArr1.get(postion)
// if(i>=1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","10");
} else if(position==1) {
intent.putExtra("Cartoons","11");
} else if(position==2) {
intent.putExtra("Cartoons","12");
} else if(position==3) {
intent.putExtra("Cartoons","13");
} else if(position==4) {
intent.putExtra("Cartoons","14");
} else if(position==5) {
intent.putExtra("Cartoons","15");
} else if(position==6) {
intent.putExtra("Cartoons","16");
} else if(position==7) {
intent.putExtra("Cartoons","17");
} else if(position==8) {
intent.putExtra("Cartoons","18");
}
startActivity(intent);
//}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

how to change the style of TextView in Expandable List View?

i am using expandable list view and i want to change the style of textview that i am using in it.
i have to add glowing effect to the text of both expanded group view and and selected child view.
my xml code for GroupView :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/groupelement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="21dp"
android:gravity="left"
/>
<ImageView
android:id="#+id/ud_image"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:scaleType="centerInside"
/>
</LinearLayout>
my xml code for childview :-
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/submenuitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="16dip"
android:paddingBottom="5dp"
android:paddingLeft="10dp" />
what i have to do :-
what i have done till now :-
my expandable list view Adapter :-
public class ExpandableMenuListAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<ExpandableMenu> expandableMenu;
private Context mContext;
private Typeface tfBold;
private Typeface tflight;
private Typeface tfregular;
private TextView grpText;
private TextView childText;
private ImageView indicator;
public ExpandableMenuListAdapter(ArrayList menus,Context context){
expandableMenu=menus;
mContext=context;
tfBold = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Bold.otf");
tflight = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Light.otf");
tfregular = Typeface.createFromAsset(mContext.getAssets(), "fonts/Raleway-Regular.otf");
}
#Override
public int getGroupCount() {
return expandableMenu.size();
}
#Override
public int getChildrenCount(int i) {
return expandableMenu.get(i).getSubMenu().size();
}
#Override
public Object getGroup(int i) {
return expandableMenu.get(i);
}
#Override
public Object getChild(int i, int i2) {
return expandableMenu.get(i).getSubMenu().get(i2);
}
#Override
public long getGroupId(int i) {
return 0;
}
#Override
public long getChildId(int i, int i2) {
return i2;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inflater.inflate(R.layout.groupview_with_child, null,false);
}
indicator=(ImageView)view.findViewById(R.id.ud_image);
grpText=(TextView)view.findViewById(R.id.groupelement);
String title = expandableMenu.get(i).getTitle();
grpText.setText(title);
grpText.setTypeface(tfregular);
if ( getChildrenCount( i ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
view.setPadding(0, 0, 0, 20);
} else {
indicator.setVisibility(View.VISIBLE);
indicator.setImageResource( isExpanded ? R.drawable.up : R.drawable.down );
view.setPadding(0, 0, 0, 20);
if(isExpanded) view.setPadding(0, 0, 0, 5);
}
/* if(isExpanded){
grpText.setTextAppearance(mContext,R.style.glow);
}else{
grpText.setTextAppearance(mContext,R.style.dim);
}*/
return view;
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view= inflater.inflate(R.layout.submenu, null,false);
}
if ((12 == expandableMenu.get(i).getSubMenu().size() - 1)){
view.setPadding(0, 0, 0, 20);
}
childText = (TextView) view.findViewById(R.id.submenuitem);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
//textView.setText(mParent.get(i).getArrayChildren().get(i1));
String subTitle= expandableMenu.get(i).getSubMenu().get(i2);
childText.setText(subTitle);
childText.setTypeface(tfBold);
//view.setTag(holder);
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i2) {
return true;
}
}
any leads on this will be appreciated !!
Use android:shadowColor , android:shadowDx and android:shadowDy for the text view.
Refer to the below link
How to add shadow to TextView on selection/focus
Cahnge your TextView As bwlow
Add style="#style/myshadowstyle" to it
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/submenuitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="16dip"
style="#style/myshadowstyle"
android:paddingBottom="5dp"
android:paddingLeft="10dp" />
Create styles.xml in your values folder
as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myshadowstyle">
<item name="android:shadowColor">#ff8800</item>
<item name="android:shadowDx">-2</item>
<item name="android:shadowDy">-2</item>
<item name="android:shadowRadius">1</item>
</style>
</resources>
Now here you can edit style as it suits to you
Try this
if you want to change the text style of group list ......
you can directly change ur headerlist.xml textview to CustomTextview like this <chifferobe.app.com.chifferobe.CustomTextView
android:id="#+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/iv_icon"
android:textColor="#color/colorWhite"
android:layout_marginLeft="#dimen/_15dp"
android:textSize="#dimen/_15dp" />
if you want to change programmatically do this.... go to adapter class,then in your getGroupView method
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
and if you want in child list ......
1.same as grouplist.xml do in your childlis.xml alse
if you want to change programmatically do this.... go to adapter class,then in your getChildView method
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
lblListHeader.setTypeface(null, Typeface.BOLD);
txtListChild.setText(childText);

How to combine children and their header in ExpanadbleListView?

I am tying to find a way to combine the group view and the child views in a list like this
except that it will be an expandable list instead.
Thank you.
I've taken the liberty to draw up some sample code for this. It made the most sense to just create your own custom view extending from ExpandableListView (especially if you want the rounded corners), a custom adapter to handle the groups and children, and then have your main.xml just include multiple custom views. As you can see from the screenshot, I've managed to reproduce it pretty closely.
So here's the code:
RoundedExpandableListView.java
public class RoundedExpandableListView extends ExpandableListView {
public RoundedExpandableListView(Context context) {
super(context);
}
public RoundedExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
// for setting the background color and rounded corners
int bgColor = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "background", 0xffffff);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundedExpandableListView);
int radius = a.getInteger(R.styleable.RoundedExpandableListView_radius, 0);
PaintDrawable drawable = new PaintDrawable();
drawable.getPaint().setColor(bgColor);
drawable.setCornerRadius(radius);
setBackgroundDrawable(drawable);
a.recycle();
}
}
GroupedViewAdapter.java
public class GroupedViewAdapter extends BaseExpandableListAdapter {
private Context mContext;
private String[] mGroups;
private Object[][] mChildren;
private int mGroupLayoutId;
private int mChildLayoutId;
private Drawable mTitleDrawable;
public GroupedViewAdapter(Context c, String[] k, Object[][] v, int titleDrawableResId) {
this(c, k, android.R.layout.simple_expandable_list_item_1, v,
android.R.layout.simple_expandable_list_item_1, titleDrawableResId);
}
public GroupedViewAdapter(Context c, String[] k, int groupLayoutResId, Object[][] v,
int childLayoutResId, int titleDrawableResId) {
super();
mContext = c;
mGroups = k;
mChildren = v;
mGroupLayoutId = groupLayoutResId;
mChildLayoutId = childLayoutResId;
mTitleDrawable = mContext.getResources().getDrawable(titleDrawableResId);
mTitleDrawable.setBounds(0, 0, 48, 48); // not necessarily the best way, but just to show example
}
#Override
public int getGroupCount() {
return mGroups.length;
}
#Override
public int getChildrenCount(int groupPosition) {
return (mChildren[groupPosition] == null) ? 0 : mChildren[groupPosition].length;
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public String getGroup(int groupPosition) {
return mGroups[groupPosition];
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return (mChildren[groupPosition] == null) ? null : mChildren[groupPosition][childPosition];
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v;
if (convertView != null) {
v = convertView;
} else {
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(mGroupLayoutId, parent, false);
}
TextView tv = ((TextView) v);
tv.setText(getGroup(groupPosition));
tv.setTag(getGroupId(groupPosition));
// set title group special properties
if (groupPosition == 0) {
tv.setTextSize(20);
tv.setCompoundDrawables(mTitleDrawable, null, null, null);
} else if (isExpanded) {
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_expanded, 0);
} else {
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_collapsed, 0);
}
return tv;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView != null) {
v = convertView;
} else {
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(mChildLayoutId, parent, false);
}
TextView tv = ((TextView) v);
tv.setText((String)getChild(groupPosition, childPosition));
tv.setTag(getChildId(groupPosition, childPosition));
return tv;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity.java
public class MainActivity extends Activity {
private RoundedExpandableListView mContact;
private RoundedExpandableListView mAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContact = (RoundedExpandableListView) findViewById(R.id.listview_contact);
mAddress = (RoundedExpandableListView) findViewById(R.id.listview_address);
mContact.setAdapter(new GroupedViewAdapter(this,
getResources().getStringArray(R.array.contact_list_items), // I chose to create this directly in XML, but you can generate it via Java like I did the children below
R.layout.drawer_list_item,
createChildList(),
R.layout.drawer_list_item,
R.drawable.ic_action_email));
mAddress.setAdapter(new GroupedViewAdapter(this,
getResources().getStringArray(R.array.address_list_items),
R.layout.drawer_list_item,
createChildList(),
R.layout.drawer_list_item,
R.drawable.ic_action_map));
mContact.setGroupIndicator(null); // since the adapter is changing how to show the group indicator icon
mAddress.setGroupIndicator(null); // since the adapter is changing how to show the group indicator icon
}
private String[][] createChildList() {
// Do stuff here to generate the list of children. Since the examples
// didn't have any, I didn't add any here.
return null; // Change this once you have children
}
}
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/com.example.sample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#95c91d" >
<com.example.sample.RoundedExpandableListView
android:id="#+id/listview_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:background="#ffffff"
local:radius="20" />
<com.example.sample.RoundedExpandableListView
android:id="#+id/listview_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:background="#ffffff"
local:radius="20"
android:layout_below="#+id/listview_contact" />
</RelativeLayout>
res/layout/drawer_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="14sp"
android:text="test"
android:paddingTop="#dimen/list_item_margin"
android:paddingRight="#dimen/list_item_margin"
android:paddingBottom="#dimen/list_item_margin"
android:drawableLeft="#android:drawable/ic_menu_manage"
android:drawablePadding="12dp"
/>
res/values/attrs.xml (for creating custom attributes for your View)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundedExpandableListView">
<attr name="radius" format="integer" />
</declare-styleable>
</resources>
res/values/strings.xml
<!-- stuff up here -->
<string-array name="contact_list_items">
<item name="item_title">Contact</item>
<item name="item_number">+41 41 41</item>
<item name="item_email">xxx#email.com</item>
<item name="item_website">www.website.com</item>
</string-array>
<string-array name="address_list_items">
<item name="item_title">Address</item>
<item name="item_address">xxxxxx\nxxxxxxxxxxx\nxxxxxxxxxx\nxxxxxxx</item>
<item name="item_other">PDF</item>
</string-array>
That should be it! Good luck!

Categories

Resources