I'm trying to create a custom view of some components because I need this combination a lot in my application. Everything in the code works except the progress of my progressBar. I think that it doesn't get the value correctly of the XML file.
In my init void I call the setProgressBar(progress) to provide the current value to my xml file. When I enter for example 'setProgressBar(88)' it works perfectly but not with a value that it has to find in the xml file.
CustomProgressbar.java
public class CustomProgressbar extends RelativeLayout {
#StyleableRes
int index0 = 0;
#StyleableRes
int index1 = 1;
#StyleableRes
int index2 = 2;
TextView titleText;
TextView valueText;
ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
int[] sets = {R.attr.title, R.attr.value, R.attr.progress};
TypedArray typedArray = context.obtainStyledAttributes(attrs, sets);
CharSequence title = typedArray.getText(index0);
CharSequence value = typedArray.getText(index1);
int progress = typedArray.getInteger(index2,0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}
custom_progressbar_layout.xml
<RelativeLayout 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="wrap_content">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/value_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<ProgressBar
android:id="#+id/progressbar_attr"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="#id/value_text"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="10dp"
android:max="100"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progressTint="#android:color/holo_green_dark"
android:progressDrawable="#drawable/progressbars" />
</RelativeLayout>
main_activity.xml
<?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="match_parent"
android:orientation="vertical"
tools:context=".MainClass"
android:background="#android:color/background_dark">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<com.x.customprogressbar.CustomProgressbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="ThisIsTheTitle"
app:value="€ 28,30"
app:progress="77" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
And my attributes
<resources>
<declare-styleable name="CustomProgressbar">
<attr name="title" format="string"/>
<attr name="value" format="string"/>
<attr name="progress" format="integer"/>
</declare-styleable>
</resources>
Current results:
The XML file, as it should look. There it works perfectly.
Link1
When I run the app.
Link2
(I'm not allowed yet to add images so I got links.)
try this, styles attributes are getting in the other way
public class CustomProgressbar extends RelativeLayout {
private TextView titleText;
private TextView valueText;
private ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);
CharSequence title = typedArray.getText(R.styleable.CustomProgressbar_title);
CharSequence value = typedArray.getText(R.styleable.CustomProgressbar_value);
int progress = typedArray.getInteger(R.styleable.CustomProgressbar_progress, 0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}
Related
I've tried to make a custom preference layout, because I wanted to make a color picker with it.
I used the solution here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefFragment()).commit();
}
I wanted to edit the value of the TextView on the layout, that's why I added the lines with the TextView in it.
The problem is that it threw a NullPointerException on the setText line, because I think the findViewById() method returned null.
PrefFragment.java:
public class PrefFragment extends PreferenceFragment {
private TextView titleTV;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
titleTV = (TextView) getActivity().findViewById(R.id.title);
titleTV.setText(R.string.pref_bgcolor);
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="#string/pref_bgcolor"
android:summary="#string/pref_bgcolor_sum"
android:key="hu.atsoft.myip.color"
android:widgetLayout="#layout/color_pref"/>
</PreferenceScreen>
color_pref.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/color_preference">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:layout_below="#id/title"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<ImageView
android:id="#+id/color_preview"
android:layout_width="20dip"
android:layout_height="20dip"
android:background="#android:color/darker_gray"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dip"/>
</RelativeLayout>
What should I use to reach the elements instead of the findViewById() method?
You should use custom Preference:
PreferenceWithTip.class
public class PreferenceWithTip extends Preference {
private static final String TAG = "PreferenceWithTip";
String pTitle = null;
String tipstring = null;
#SuppressLint("Recycle")
public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.i(TAG,"PreferenceWithTip invoked");
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);
tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);
pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);
ta.recycle();
}
public PreferenceWithTip(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView pTitleView = (TextView)view.findViewById(R.id.title);
pTitleView.setText(pTitle);
TextView pTipView = (TextView)view.findViewById(R.id.summary);
pTipView.setText(tipstring);
}
#Override
protected View onCreateView(ViewGroup parent) {
return LayoutInflater.from(getContext()).inflate(R.layout.color_pref,
parent, false);
}
}
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PreferenceWithTip">
<attr name="tipstring" format="string"></attr>
<attr name="titlestring" format="string"></attr>
</declare-styleable>
</resources>
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.test.mytest.PreferenceWithTip
preference:tipstring="a"
preference:titlestring="title" >
</com.test.mytest.PreferenceWithTip>
</PreferenceScreen>
I'm trying to make something like the following screenshot (it's from an assignment from school):
As mentioned in the assignment, I have an Activity PlayActivity:
public class PlayActivity extends ActionBarActivity {
#Bind(R.id.activity_play_textView_score)
TextView txtScore;
#Bind(R.id.activity_play_board)
Board board;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
ButterKnife.bind(this);
}
}
The xml-layout for this activity:
<?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:layout_height="match_parent">
<TextView
android:id="#+id/activity_play_textView_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/activity_play_textView_score_placeholder"/>
<com.charlotteerpels.game2048.Board
android:id="#+id/activity_play_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
We also had to make a class Card, which is an extension of a FrameLayout:
public class Card extends FrameLayout {
#Bind(R.id.card_frame_layout_textView)
TextView txtNumber;
private int number;
public void setTxtNumber(TextView txtNumber) {
this.txtNumber = txtNumber;
}
public TextView getTxtNumber() {
return this.txtNumber;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return this.number;
}
public Card(Context context, AttributeSet attributeSet, int defaultStyle) {
super(context, attributeSet, defaultStyle);
inflateLayout();
ButterKnife.bind(this);
}
public Card(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
inflateLayout();
ButterKnife.bind(this);
}
public Card(Context context) {
super(context);
inflateLayout();
ButterKnife.bind(this);
}
private void inflateLayout() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.card_frame_layout, this, true);
}
}
The xml-layout for this class:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/card_frame_layout_background"
android:layout_margin="4dp">
<TextView
android:id="#+id/card_frame_layout_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/card_frame_layout_textView_placeholder"
android:textSize="40sp"
android:layout_gravity="center"/>
</FrameLayout>
At least we had to make a class Board, which is an extension of a GridLayout:
public class Board extends GridLayout {
Card[][] cardBoard;
public Board(Context context, AttributeSet attributeSet, int defaultStyle) {
super(context, attributeSet, defaultStyle);
initBoard(context);
}
public Board(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
initBoard(context);
}
public Board(Context context) {
super(context);
initBoard(context);
}
public void initBoard(Context context) {
//Set the settings for the GridLayout (the grid is the board)
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.board_grid_layout, this, true);
//Initialize the cardBoard[][] and populate it
cardBoard = new Card[4][4];
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
cardBoard[rij][kolom] = new Card(getContext());
}
}
int cardMeasure = getCardMeasure(context);
addCardBoardToGridLayout(cardMeasure);
}
private void addCardBoardToGridLayout(int cardMeasure) {
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
Card card = cardBoard[rij][kolom];
addView(card, cardMeasure, cardMeasure);
}
}
}
private int getCardMeasure(Context context) {
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
return screenWidth/4;
}
}
The xml-layout for the Board:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/board_grid_layout_background"
android:columnCount="4"
android:rowCount="4">
</GridLayout>
But all I'm getting is this:
I'm also using Butterknife (website: http://jakewharton.github.io/butterknife/)
for binding resources, but mostly to bind views and elements from the xml-layouts.
Can anybody help me?
Found what was wrong!
So in the xml-layout from Card, I changed the layout_width and layout_height from the TextView:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/card_frame_layout_background"
android:layout_margin="4dp">
<TextView
android:id="#+id/card_frame_layout_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/card_frame_layout_textView_placeholder"
android:textSize="40sp"
android:layout_gravity="center"/>
</FrameLayout>
The "addCardBoardToGridLayout(int cardMeasure)" changed to the following:
private void addCardBoardToGridLayout(int cardMeasure) {
setRowCount(4);
setColumnCount(4);
removeAllViews();
for(int rij=0; rij<4; rij++) {
for(int kolom=0; kolom<4; kolom++) {
Card card = cardBoard[rij][kolom];
addView(card, cardMeasure, cardMeasure);
}
}
}
Now it looks like this (and it's exactly like I wanted it!):
I make WearableListView list. Problem is that setting android:layout_height="20dp" doesn't help
How to set height in this case? In Android Wear sample projects Notifications and Timer they also just set atribute android:layout_height="80dp". But I tried to set in the projects android:layout_height="20dp" but it didn't help! (below is my project source code):
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text_hole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:textSize="#dimen/list_item_text_size" />
</base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout>
HolesListItemLayout.java:
public class HolesListItemLayout extends LinearLayout
implements WearableListView.OnCenterProximityListener {
private TextView mName;
private final int mFadedTextColor;
private final int mChosenTextColor;
public HolesListItemLayout(Context context) {
this(context, null);
}
public HolesListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HolesListItemLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mFadedTextColor = getResources().getColor(R.color.grey);
mChosenTextColor = getResources().getColor(R.color.black);
}
// Get references to the icon and text in the item layout definition
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mName = (TextView) findViewById(R.id.text_hole);
}
#Override
public void onCenterPosition(boolean animate) {
mName.setTextSize(18);
mName.setTextColor(mChosenTextColor);
}
#Override
public void onNonCenterPosition(boolean animate) {
mName.setTextColor(mFadedTextColor);
mName.setTextSize(14);
}
}
HolesListAdapter.java:
public class HolesListAdapter extends WearableListView.Adapter {
private final Context mContext;
private final LayoutInflater mInflater;
public HolesListAdapter(Context context) {
this.mContext = context;
mInflater = LayoutInflater.from(context);
}
#Override
public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new WearableListView.ViewHolder(
mInflater.inflate(R.layout.list_item_hole, null));
}
#Override
public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
TextView text = (TextView) holder.itemView.findViewById(R.id.text_hole);
text.setText(mContext.getString(R.string.hole_list_item) + " " + (position + 1));
text.setTextColor(mContext.getResources().getColor(android.R.color.black));
holder.itemView.setTag(position);
}
#Override
public int getItemCount() {
return Preferences.HOLES;
}
}
The WearableListView is hard-coded to only display three items at a time - it measures the item height by dividing the list view's height by three ... so there isn't a practical way of doing what you want.
I suggest making the text font larger ...
I've got idea. Insert list into FrameLayout container. And by changing height of container list item height is changed. Result:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerInParent="true">
<android.support.wearable.view.WearableListView
android:id="#+id/wearable_list_game_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:scrollbars="none"/>
</FrameLayout>
</RelativeLayout>
Add one LinearLayout inside and set
android:layout_height="50dp"
android:layout_margin="5dp"
Hi I wanted to create a multirow radio buttons inside my radio group. If i use the radio group vertically my design goes weird and it not looks good. Iam having 7radio buttons which i wanna show 2 in a row. Please help me if anybody knows.
add in your xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 3"
custom:group="group1" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option A"
custom:group="group2" />
<com.example.app.GroupedRadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option B"
custom:group="group2" />
</LinearLayout>
// add this line in your attr.xml file
<declare-styleable name="GroupedRadioButton">
<attr name="group" format="string" />
</declare-styleable>
public class RadioGroupedButton extends RadioButton {
private static Map<String, WeakReference<RadioGroupedButton>> buttonMap;
private OnClickListener externalListener;
private String groupName;
static {
buttonMap = new HashMap<String, WeakReference<RadioGroupedButton>>();
}
public RadioGroupedButton(Context context, AttributeSet attrs) {
super(context, attrs);
processAttributes(context, attrs);
setOnClickListener(internalListener, true);
}
private void processAttributes(Context context, AttributeSet attrs) {
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.GroupedRadioButton);
int attributeCount = attributes.getIndexCount();
for (int i = 0; i < attributeCount; ++i) {
int attr = attributes.getIndex(i);
switch (attr) {
case R.styleable.GroupedRadioButton_group:
this.groupName = attributes.getString(attr);
break;
}
}
attributes.recycle();
}
private OnClickListener internalListener = new OnClickListener() {
#Override
public void onClick(View view) {
processButtonClick(view);
}
};
private void setOnClickListener(OnClickListener listener, boolean internal) {
if (internal)
super.setOnClickListener(internalListener);
else
this.externalListener = listener;
}
#Override
public void setOnClickListener(OnClickListener listener) {
setOnClickListener(listener, false);
}
private void processButtonClick(View view) {
if (!(view instanceof RadioGroupedButton))
return;
RadioGroupedButton clickedButton = (RadioGroupedButton) view;
String groupName = clickedButton.groupName;
WeakReference<RadioGroupedButton> selectedButtonReference = buttonMap.get(groupName);
RadioGroupedButton selectedButton = selectedButtonReference == null ? null
: selectedButtonReference.get();
if (selectedButton != clickedButton) {
if (selectedButton != null)
selectedButton.setChecked(false);
clickedButton.setChecked(true);
buttonMap.put(groupName, new WeakReference<RadioGroupedButton>(clickedButton));
}
if (externalListener != null)
externalListener.onClick(view);
}
}
How do you anchor a Dropdown spinner to a perent Linearlayout?
I have a TextView and a Spinner in a horizontal LinearLayout. I want to set the LinearLayout to be the anchor of the spinner drop down. Just like you can do with a AutoCompleteTextViews drop down.
<LinearLayout
android:id="#+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom"
android:weightSum="1" >
<AutoCompleteTextView
android:id="#+id/autoText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".8"
android:gravity="bottom"
android:dropDownAnchor="#id/parent"/>
<mycode.CustomSpinner
android:id="#+id/customSpinner"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".2"
android:drawSelectorOnTop="true"
android:gravity="bottom"/>
</LinearLayout>
I found out that the easiest way was to extend a ImageButton and add a ListPopupWindow.
public class MenuDropDown extends ImageButton {
private ListPopupWindow mListDropDownWindow;
private int mDropDownAnchorId;
private ListAdapter mAdapter;
private DropDownOnClickListener mDropDownOnClickListener;
private OnItemClickListener mOnItemClickListener;
public MenuDropDown(Context context, AttributeSet attrs) {
this(context, attrs,R.attr.menuDropDown);
}
public MenuDropDown(Context context) {
this(context, null);
}
public MenuDropDown(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mListDropDownWindow = new ListPopupWindow(context);
mListDropDownWindow
.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MenuDropDown, defStyle, 0);
mDropDownAnchorId = a.getResourceId(
R.styleable.MenuDropDown_dropDownAnchor,
View.NO_ID);
mListDropDownWindow
.setOnItemClickListener(new DropDownItemClickListener());
mListDropDownWindow.setModal(true);
a.recycle();
setFocusable(true);
mDropDownOnClickListener = new DropDownOnClickListener();
super.setOnClickListener(mDropDownOnClickListener);
}
private class DropDownItemClickListener implements
AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
dissmissDropDown();
if(mOnItemClickListener != null){
mOnItemClickListener.onItemClick(parent, v, position, id);
}
}
}
private class DropDownOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
showDropDown();
}
}
private void dissmissDropDown() {
mListDropDownWindow.dismiss();
}
public <T extends ListAdapter> void setAdapter(T adapter) {
mAdapter = adapter;
mListDropDownWindow.setAdapter(mAdapter);
}
public boolean isPopupShowing() {
return mListDropDownWindow.isShowing();
}
public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
mOnItemClickListener = listener;
}
private void showDropDown() {
if (mListDropDownWindow.getAnchorView() == null) {
if (mDropDownAnchorId != View.NO_ID) {
mListDropDownWindow.setAnchorView(getRootView().findViewById(
mDropDownAnchorId));
} else {
mListDropDownWindow.setAnchorView(this);
}
}
mListDropDownWindow.show();
if (VERSION.SDK_INT >= 9) {
mListDropDownWindow.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
}
}
public ListAdapter getAdapter() {
return mAdapter;
}
}
attrs.xml:
<attr name="menuDropDown" format="reference" />
<declare-styleable name="MenuDropDown" perent="ImageButton">
<attr name="dropDownAnchor" format="reference" />
</declare-styleable>
Layout:
<AutoCompleteTextView
android:id="#+id/autoText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".8"
android:gravity="bottom"
android:dropDownAnchor="#id/parent"/>
<mycode.MenuDropDown
android:id="#+id/customSpinner"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".2"
android:drawSelectorOnTop="true"
mycode:dropDownAnchor="#id/parent"
android:gravity="bottom"/>
</LinearLayout>