How do you anchor a Dropdown spinner to a parent Linearlayout? - android

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>

Related

How do I change the progress value of a progress bar programmatically?

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);
}
}

Show loader on Spinner (Dropdown) when it is fetching data from web service

In the image above, I have shown that when the user touches the drop-down spinner it will call the web api for getting data for the spinner. Then, that moment, I want to show the loader only on the spinner view on the left or right somewhere on the view itself like in the image, rather than on whole screen when it is getting data from the web service dynamically and hide that progress bar later when web service completely hit at the end (Ignore that search bar in image).
Just create an custom adapter for your spinner. Follow the instructions found here How to create Spinner-list using CustomAdapter in android .
Put the loading view in the layout inflated in the getView method in the adapter, and manipulate it via a callback from your async task used for fetching the result.
In this i am showing loader on start button and hiding loader when stop button is pressed so you can use according to your need.So , for this i have made three class CustomSpinner,Spinner_Custom_adapter and Activity class for using it
In main layout file
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<www.your_packagename.com.spinnerwithloaderex.CustomSpinner
android:id="#+id/custm_spnr"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="#drawable/dropdown_create_sales"
android:paddingRight="15dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:id="#+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
CustomSpinner class
public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private Spinner_Custom_adapter spinner_custom_adapter;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
public void setItems(Activity activity, ArrayList<String> spnr_Arr) {
spinner_custom_adapter = new Spinner_Custom_adapter(activity, spnr_Arr);
setAdapter(spinner_custom_adapter);
}
public Spinner_Custom_adapter getSpinner_custom_adapter() {
return spinner_custom_adapter;
}
public void showLoader() {
setEnabled(false);
spinner_custom_adapter.showLoader(true, true);
}
public void dismissLoader() {
setEnabled(true);
spinner_custom_adapter.showLoader(false, true);
}
}
Custom_Adapter class
public class Spinner_Custom_adapter<T> extends ArrayAdapter<T> {
private LayoutInflater flater;
private ProgressBar spinner_progress;
private TextView txtTitle;
private Boolean showOrNot = false;
Spinner_Custom_adapter(Activity context, ArrayList<T> list) {
super(context, R.layout.loader_spinner_lt, R.id.title, list);
flater = context.getLayoutInflater();
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = flater.inflate(R.layout.loader_spinner_lt, parent, false);
}
Object object = getItem(position);
String rowItem = null;
if (object instanceof String) {
rowItem = (String) object;
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem);
ProgressBar spinner_progress = (ProgressBar) convertView.findViewById(R.id.spinner_progress);
this.txtTitle = txtTitle;
this.spinner_progress = spinner_progress;
showLoader(showOrNot, false);
return convertView;
}
void showLoader(Boolean showOrNot, boolean notifyListOrNot) {
if (txtTitle != null && spinner_progress != null) {
this.showOrNot = showOrNot;
spinner_progress.setVisibility(showOrNot ? View.VISIBLE : View.GONE);
if (notifyListOrNot) {
notifyDataSetChanged();
}
}
}
}
Spinner single view layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/spinner_progress"
android:ellipsize="end"
android:singleLine="true"
android:text="Strawberry"
android:textColor="#CC0033"
android:textSize="16dp" />
<ProgressBar
android:id="#+id/spinner_progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
and for using it
custm_spnr = (CustomSpinner) findViewById(R.id.custm_spnr);
ArrayList<String> items = new ArrayList<>();
items.add("Abcdefg");
items.add("hijklm");
items.add("nopqr");
items.add("stu");
items.add("vwxyza1b1c1");
items.add("d1e1f11g1h1");
custm_spnr.setItems(MainActivity.this, items);
findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.showLoader();
}
});
findViewById(R.id.stop_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.dismissLoader();
}
});

GridView issue with large data while scrolling

I am having problem with "GridView".If I use 6 items then it is not creating problem while scrolling but as I increase no. of items its behavior is unpredictable.
Please check the following video link for better understanding my problem:
GridView Issues video link
I am attaching my codes here:
activity_achievements.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="#color/green_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back_iv"
android:text="Acheivements"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/back" />
<TextView
android:id="#+id/total_achievements_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="0/15"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/acheivements_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</RelativeLayout>
achievements_row.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xyz.quitsmoking.view.AchievementsView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v7.widget.CardView
android:id="#+id/card_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="2dp"
android:background="#color/white_color"
card_view:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:src="#drawable/achievement" />
<TextView
android:id="#+id/title_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:text="Himalyi"
android:textColor="#color/green_color" />
<TextView
android:id="#+id/desc_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="16sp"
android:padding="10dp"
android:text="5 cigarettes non-smoked"
android:textColor="#color/teal_color" />
</LinearLayout>
</android.support.v7.widget.CardView>
</pairroxz.com.quitsmoking.view.AchievementsView>
AchievementsAdapter.java
public class AchievementsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Achievements> list;
private LayoutInflater inflater;
public AchievementsAdapter(Context context,ArrayList<Achievements> list){
this.context = context;
this.list = list;
initialize();
}
private void initialize() {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
AchievementsView view = null;
if(view==null) {
view = (AchievementsView) inflater.inflate(R.layout.achievements_row,null);
}else{
view = (AchievementsView) convertView;
}
view.setContents(list.get(position));
notifyDataSetChanged();
return view;
}
}
AchievementsView.java
public class AchievementsView extends RelativeLayout {
private TextView title_tv,desc_tv;
private ImageView image_iv;
public AchievementsView(Context context) {
super(context);
}
public AchievementsView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
initialize();
}
private void initialize() {
image_iv = (ImageView)findViewById(R.id.image_iv);
title_tv = (TextView)findViewById(R.id.title_tv);
desc_tv = (TextView) findViewById(R.id.desc_tv);
}
public void setContents(Achievements achievements){
image_iv.setImageDrawable(getContext().getResources().getDrawable(achievements.getImg()));
title_tv.setText(achievements.getTitle());
desc_tv.setText(achievements.getDesc());
}
}
AchievementsActivity.java
public class AchievementsActivity extends AppCompatActivity implements View.OnClickListener {
private GridView achievements_gv;
private AchievementsAdapter adapter;
private ArrayList<Achievements> list;
DatabaseHelper helper;
private ImageView back_iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_achievements);
initialize();
}
private void initialize() {
helper = DatabaseHelper.getInstance(this);
back_iv = (ImageView) findViewById(R.id.back_iv);
achievements_gv = (GridView) findViewById(R.id.acheivements_gv);
list = new ArrayList<Achievements>();
getData();
setListener();
}
private void setListener() {
back_iv.setOnClickListener(this);
}
private void getData() {
list = helper.getAchievementsList();
//list =
adapter = new AchievementsAdapter(this, list);
achievements_gv.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_iv:
onBackPressed();
break;
}
}
}
if(view == null)
view.setTag(viewHolder);
viewHolder = (ViewHolder) view.getTag();
don't use this. or send your code

How to populate data in AutoCompleteTextView?

I have to show two section in AutoCompleteTextView (Something like this):
I have created a custom layout which have two CardViews and each CardView have three TextViews. Right now I am not distributing the section on the basis of type. The whole data is loaded into one section.
Activity
final AutocompleteLocalityAdapter adapterLocalities = new AutocompleteLocalityAdapter(context,
R.layout.support_simple_spinner_dropdown_item, new ArrayList<Locality>());
AutocompleteLocalityAdapter
public class AutocompleteLocalityAdapter extends ArrayAdapter<Locality> {
public AutocompleteLocalityAdapter(Context context, int layout, List<Locality> localities) {
super(context, layout, localities);
this.localities = localities;
updateList("");
}
In updateList method I am making a new network call to fill the data in Locality class.
What do I need to do to categories the search result as per given image? ArrayAdapter is not going to work here for sure.
The possible solution I am thinking here is:
Replace ArrayAdapter to RecyclerViewAdapter.
Any hint will be appreciable.
The possible makeshift to this solution is PopUpWindow. Inside the PopUpWindow I put two RecyclerView and populate them through network calls.
dashboard_profile_popup_window
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/margin_low"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/locationCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/landmark"
android:textStyle="bold" />
<ListView
android:id="#+id/localityView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/landmarkCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="#dimen/corner_radius"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/location"
android:textStyle="bold" />
<ListView
android:id="#+id/landmarkView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
CustomWidget
public class CustomAutoCompleteView extends EditText {
private Context context;
TextListViewAdapter locationAdapter;
TextListViewAdapter landmarkAdaper;
PopupWindow pwindow;
ClickListener clickListener;
public CustomAutoCompleteView(Context context) {
super(context);
this.context = context;
setCustomization();
}
public void closeWindow(){
pwindow.dismiss();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setCustomization();
}
public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setCustomization();
}
public void updateList(List<LocalityEntity> locationList, List<LocalityEntity> landmarkList) {
if (pwindow == null) {
initPopupWindow();
}
locationAdapter.updateList(locationList);
landmarkAdaper.updateList(landmarkList);
}
public void initPopupWindow() {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.dashboard_profile_popup_window, null);
ListView landmarkRecyclerView = (ListView) layout.findViewById(R.id.localityView);
ListView localityRecyclerView = (ListView) layout.findViewById(R.id.landmarkView);
landmarkRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
localityRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.localityText)).getText().toString();
String gid = ((TextView) view.findViewById(R.id.localityGID)).getText().toString();
clickListener.placeSelected(gid);
}
});
landmarkRecyclerView.setAdapter(landmarkAdaper);
localityRecyclerView.setAdapter(locationAdapter);
pwindow = new PopupWindow(context);
pwindow.setContentView(layout);
pwindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
pwindow.setWidth(this.getWidth());
pwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pwindow.setFocusable(true);
pwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
pwindow = null;
}
});
pwindow.showAsDropDown(this);
}
private void setCustomization() {
locationAdapter = new TextListViewAdapter(getContext());
landmarkAdaper = new TextListViewAdapter(getContext());
initPopupWindow();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void placeSelected(String gid);
}
}
Now call this customViewWidget through following code:
place_pop_up.setClickListener(this);
place_pop_up.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private Timer timer = new Timer();
private final long DELAY = 2000;
#Override
public void afterTextChanged(final Editable s) {
if (s.length() > 3) {
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
getAutoCompleteSearchResult(s.toString());
}
}, DELAY);
}
}
});
In getAutoCompleteSearchResult make the network call and call place_pop_up.updateList(locality, landmark);

How to set WearableListView item height

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"

Categories

Resources