Custom Spinner view under Button - android

I have a Button that, when clicked, calls a Spinner. The Spinner has a custom view but the layout goes in the middle of the page instead of under the Button.
This is what it should look like:
Instead it looks like this:
How can I show the view under my Button like the first image? Here is my button and spinner .xml
<ImageButton android:id="#+id/window_recent_menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="#null"
android:src="#drawable/menu_btn"
android:contentDescription="#string/contentDesc"
android:visibility="gone"/>
<Spinner android:id="#+id/window_recent_menu_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:visibility="gone"/>
Thanks
P.S the android:visibility="gone" for the ImageButton is for certain views. That does not apply for the view seen above.

What you really want here is a PopupMenu, not a Spinner. (support package)

Related

how to add a clickable text in EditText

Hi, I have a UI like the picture above, when the "forgot?" text clicked it will open another activity, this is an EditText view, Is there any way I can do to add the "forgot?" text and make it clickable?
This should fit your needs:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/et_password"
android:hint="Password"/>
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/et_password"
android:text="Forgot?" />
</RelativeLayout>
Just set an OnClickListener to the TextView and you are good to go :)

ImageButton show nothing in RecyclerView

I have RecyclerView include ImageButton play each item. But when I run ImageButton show nothing.
https://gist.github.com/tugnt/98917e0a2293d6cddd2e0e6e21d3d4fb
<Button
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In your xml,see android:src property is not for button. It's imageview property. So use ImageView instead of Button or set android:background for Button
<ImageView
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now try this.
Below is your code, in this you have used the android:src="#drawable/ic_speaker" for the button.
<Button
android:src="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But src is for imageview, which doesn't support the button view. So use background instead of src and in the xml preview you cannot see the play button. Have a look at the below code, I have edited the code
<Button
android:background="#drawable/ic_speaker"
android:id="#+id/play"
android:layout_margin="#dimen/pd_3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Use the xml preview to see whether you can see the view or not, which helps to sort put the bug easily and save our time. Hope this is helpful :)

Click LinearLayout to execute action

I have a layout that's used in several activities. I have a LinearLayout in the layout that looks like this:
<LinearLayout
android:id="#+id/like_button"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/like_button_iv"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="10dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/ic_like"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:text="100"
/>
</LinearLayout>
I need to make it so that if a user clicks this LinearLayout, it executes an action and then changes the ImageView src to a different icon.
Also, is using a LinearLayout in this situation ideal?
How can I do this?
You can use your ImageView to be clickable and make it to do the job.
In your activity class declare a ImageView.
ImageView myimage;
In your oncreate method add
myimage=(ImageView) findViewById(R.id.like_button_iv);
myimage.setOnclickListener(this);
In method onClick add:
myimage.setImageDrawable(getResources().getDrawable(R.drawable.differntsrc));
Use your logic for selecting image source differntsrc

Android: How to create an EditText and set it to another views position?

I have a layout with a textView and a spinner in it.
If the user clicks on a button I like to hide the spinner and create an editText field programmatically at the spinners position. How can I do this?
The layout looks like:
<RelativeLayout
android:id="#+id/rl_steuern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/rl_betraege"
android:layout_alignRight="#+id/rl_betraege"
android:layout_below="#+id/rl_betraege"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" >
<Spinner
android:id="#+id/spinner_steuer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/TextView01" />
<TextView
android:id="#+id/TextView01"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Steuer:"
android:textSize="18dip" />
</RelativeLayout>
Hello first you need to add button to your xml file.I am not able to see it anywhere.
then on button click in your java class,hide the spinner and display the Edit Text view using following code:
spinner=(Spinner)findViewbyId(R.id.spinner);
EditText et = new EditText(YourActivity.this);
spinner.setVisibility(View.GONE); //hides the spinner
your_main_layout.addView(et); //dynamically adds EditText
You can ask if you have any further queries :)

How to create an overlay view in Android which toggles on button click

I want to create and overlay view when a button is clicked. Something like clicking smileys button in Whatsapp.
I have created a framelayout with default visibility as GONE and added an onclick listener on smiley button to toggle its visibility.
My layout XML:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<ImageView
android:id="#+id/smileyButton"
android:layout_width="24dip"
android:layout_height="48dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/contactIconContentDescription"
android:src="#drawable/emo_im_happy" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/smileyButton" >
<TextView
android:id="#+id/chat_smiley_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white" />
</FrameLayout>
</RelativeLayout>
I have added onClickListener to smileyButton which toggles visibility of chat_smiley_list.
Other than some positioning problems, I am not sure whether this is the best way to do it.
I think you could use a Context Menu: documentation

Categories

Resources