How to set popup window position based on button position in android? - android

I am adding button programatically while clicking on button I need show PopupWindow in that button position when ever orientation is changing that button position also changes..

I am trying to tell you what I thought to make it, may not be the best solution, just a reference. All sources here are pseudo-code.
Here is the parent layout, and set button and your popup window at same position:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_marginTop="80dp"
android:layout_marginLeft="80dp" >
</Button>
<com.yourdomain.android.PopupWindow
android:layout_marginTop="80dp"
android:layout_marginLeft="80dp"
android:visibility="GONE">
</com.yourdomain.android.PopupWindow>
</RelativeLayout>
Here is the layout of your popup window:
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
</LinearLayout>
Here you can create a class to bind to this layout, so its your custom component:
public class PopupWindow extends LinearLayout {
protected Context _context = null;
public PopupWindow (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
_context = context;
setupView(context);
}
public PopupWindow (Context context) {
super(context);
// TODO Auto-generated constructor stub
_context = context;
setupView(context);
}
public void setupView (Context context)
{
// here to initialize all children views in this layout
}
public void show ()
{
this.setVisibility (LinearLayout.Visible);
}
public void hide ()
{
this.setVisibility (LinearLayout.GONE);
}
}
I hope this helps.

Related

Custom compound view extended from RelativeLayout. Incorrect positioning of child views

I want to make widget which includes EditText and TextView and looks like this:
.
To achieve this I've created RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp">
<EditText
android:id="#+id/editTextHint"
style="#style/CustomEditTextTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="14dp"
android:textColor="#color/textColor"
android:textSize="16sp"
android:hint="To my first character"
tools:text="Тип клиента">
</EditText>
<TextView
android:id="#+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextHint"
android:layout_alignParentEnd="true"
android:layout_above="#id/editTextHint"
tools:text="Продавец"
android:fontFamily="#font/proxima_nova_regular"
android:textSize="16sp"
android:layout_marginEnd="4dp"
android:textColor="#color/colorPrimary"/>
</RelativeLayout>
The above RelativeLayout works great and it looks as planned. To make Compound view I've removed </RelativeLayout> tag and wrapped it into the </merge> (I don't apply code as it's identical except of RelativeLayout tag)
To work with view I've written MyCustomEditText class which extendes RelativeLayout
public class CustomEditText extends RelativeLayout {
private EditText editTextHint;
private TextView textViewEntry;
private String hintText;
private String inputText;
private String starterText;
private OnCustomEditTextListener listener;
public String getHintText() {
return hintText;
}
public String getInputText() {
return inputText;
}
public void setHintText(String hintText) {
editTextHint.setText(hintText);
}
public void setInputText(String inputText) {
textViewEntry.setText(inputText);
}
public CustomEditText(Context context) {
super(context);
initializeViews(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray;
typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
hintText = typedArray.getString(R.styleable.CustomEditText_hintText);
inputText = typedArray.getString(R.styleable.CustomEditText_inputText);
starterText = typedArray.getString(R.styleable.CustomEditText_starterText);
typedArray.recycle();
initializeViews(context);
}
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_edit_text, this);
setMinimumHeight(48);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
editTextHint = findViewById(R.id.editTextHint);
textViewEntry = findViewById(R.id.textViewContent);
bindDataToChildViews();
editTextHint.setOnClickListener(v -> listener.onClick());
}
private void bindDataToChildViews() {
editTextHint.setText(hintText);
if ((inputText == null)||(inputText.isEmpty())) {
textViewEntry.setText(starterText);
} else {
textViewEntry.setText(inputText);
}
}
public void setHintClickListener(OnCustomEditTextListener listener) {
this.listener = listener;
}
public interface OnCustomEditTextListener {
void onClick();
}
}
I'm trying tu use this view in other layouts but the positioning of text view is ugly:
I thought that extending view from RelativeLayout would be enough for correct positioning. So I need your help to define where I'me getting wrong. How to position elements correctly?
P.S. The replacement of RelativeLayout for merge tag was made to optimize drawing of views and avoiding unnecessary nesting layouts
First of all you dont need to have
android:layout_above="#id/editTextHint"
And add
android:layout_gravity="center_vertical"
to editTextHint and textViewContent
Also possibly you should remove
android:layout_marginEnd="4dp"
or change a bit it's 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();
}
});

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"

Android development - make a spinner look like a spinner

I'm creating a spinner programmatically in my activity class. The "Spinner" looks like a dropdown rather than a spinner. I guess I want it to look more like a picker (ie date picker / time picker / number picker) where you can spin through all the text options.
I would use a picker type widget if there was a text picker available but I can't seem to find such a widget - only a number picker. Here is the code I'm using in my activity.
ArrayAdapter <String>lv1List = new ArrayAdapter<String>(this.getApplicationContext(),android.R.layout.simple_spinner_item, new String[]{"item 1","item 2","item 3"});
Spinner sp = new Spinner(getApplicationContext());
sp.setAdapter(lv1List);
sp.setOnItemSelectedListener(this);
Take a look at Android Wheel. You can have text or pretty much anything on it. Works perfectly.
Quick ( and inefficient) example if you want to create your own spinner. (Works with List View Adapters):
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<Button android:id="#+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="up"/>
<Button android:id="#+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down"/>
</LinearLayout>
</LinearLayout>
CustomSpinner Class:
public class CustomSpinner extends FrameLayout{
// ------------------------------ FIELDS ------------------------------
private FrameLayout mContainer;
private SpinnerAdapter mAdapter;
private int index = 0;
// --------------------------- CONSTRUCTORS ---------------------------
public CustomSpinner(Context context) {
super(context);
init(context);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false));
findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
index--;
refresh();
}
});
findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
index++;
refresh();
}
});
mContainer = (FrameLayout) findViewById(R.id.container);
}
// -------------------------- OTHER METHODS --------------------------
public void setAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
refresh();
}
private void refresh() {
//----needs recycling for better performance---
//---now, we'll just clear up--
mContainer.removeAllViews();
//---do we need to refresh ? -----
if(mAdapter == null || mAdapter.getCount() == 0){return;}
//--clamp index--
index = Math.max(0,index);
index = Math.min(mAdapter.getCount() - 1, index);
//--get view and show it-----
View currentView = mAdapter.getView(index,null,mContainer);
mContainer.addView(currentView);
}
}
Use Case:
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.CustomSpinner android:id="#+id/custom_spinner"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
Activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});
((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter);
}
}

inflate custom Gallery Layout change properties fail

I tried to build my first custom gui element but I get problems when I try to change appearance or adapter (using Gallery) later in code.
My Problem: I can't change Custom Gallery properties
My actual Code:
First I create an XML which is the widget customGallery.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="#+id/toLeft"
android:background="#drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginBottom="1dip" />
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_toRightOf="#+id/toLeft"
android:spacing="40dip"
android:scrollbars="horizontal"/>
<ImageButton android:id="#+id/toRight"
android:background="#drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_toRightOf="#+id/gallery" />
</merge>
Later i create a test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.Test.GallerySlider
android:id="#+id/choose"
android:layout_span="2"
android:layout_width="300dip"
android:layout_height="wrap_content" />
</LinearLayout>
My next Step was to include this custom Widget into my project and change the adapter from my Widget:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout);
ViewStub st3 = new ViewStub(TestwidgetActivity.this);
LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineLayout.addView(st3,paramst3);
st3.setLayoutResource(R.layout.test);
View st3InflaView =st3.inflate();
GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose);
gSlider.setNewAdapter( new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"}));
}
This is the Widgetclass I wrote:
public class GallerySlider extends RelativeLayout implements OnClickListener {
private ArrayAdapter<String> adapter;
private Gallery gallery;
private ImageButton toLeftBtn = null;
private ImageButton toRightbtn = null;
public GallerySlider(Context context) {
super(context, null);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
public void init(Context ctx){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
inflater.inflate(R.layout.customGallery, this, true);
toLeftBtn = (ImageButton) findViewById(R.id.toLeft);
toLeftBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() > 0){
gallery.setSelection(gallery.getSelectedItemPosition()-1);
}
}
});
toRightbtn = (ImageButton) findViewById(R.id.toRight);
toRightbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){
gallery.setSelection(gallery.getSelectedItemPosition()+1);
}
}
});
adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1",
"1")});
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setBackgroundResource(R.drawable.h_sliderl);
gallery.setAdapter(adapter);
}
#Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case R.id.toLeft: gallery.setSelection(gallery.getFocusedChild().getId()-1);
break;
case R.id.toRight: gallery.setSelection(gallery.getFocusedChild().getId()+1);
break;
}
}
public void setNewAdapter(ArrayAdapter<String> _adapter){
gallery.setAdapter(_adapter);
((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged ();
}
}
If I call setNewAdapter(ArrayAdapter _adapter) nothing change.. . I also tried to change the size of the gallery but it also fails(nothig happen). Is my approach false?
greetings marcel
The first thing I can detect is that you are creating your custom view two times.
The first creation occurs with the ViewStub when you set the layout.
And the second one, which doesn't get added to the contentView when you inflate R.layout.test.
You are setting the adapter to the second custom view which it isn't added to the view hierarchy.

Categories

Resources