I have 3 spinner for country, timezone and language with the respective labels arranged one below another in below manner
Label1 Spinner1
Label2 Spinner2
Label3 Spinner3
When Click on say Spinner1, I want entire row i.e. Label and spinner to change their background color, Say if I take it in a LinearLayout So on spinner click The background should change to blue
I want probably a xml way of changing background of whole layout and not programmatic as I will have to code for each spinner
Can someone tell me how do I change entire layout color on just spinner click
I tried Spinner.setOnItemSelectedListener, but that is hitting only when I select some item, but I want the layout to change color even on press of Spinner
Spinner in layout
layout.xml
<LinearLayout
android:id="#+id/linearlay"
android:layout_width="320dp"
android:layout_height="40dp"
android:background="#drawable/bg_spinner_pressed">
<TextView
android:id="#+id/label"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="Label"
android:textColor="#color/textColorPrimary"
android:textSize="14dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupBackground="#color/colorPrimary"
android:textColor="#android:color/white"
android:textSize="20sp"/>
</LinearLayout>
Settings.java file
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("Techie","----------------------------citizenship1----------------------------");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e("Techie","----------------------------citizenship2----------------------------");
}
});
I expect the Layout with id linearlay should change its primary color white to change to Blue on spinner click. please help
Call setonTouchListener and handle down and up events to change the colors
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("Techie", "----------------------------Action down----------------------------");
layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color1));
break;
case MotionEvent.ACTION_CANCEL:
Log.e("Techie", "----------------------------Cancel----------------------------");
layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color2));
break;
}
// allow target view to handle click
return false;
}
});
Related
Currently, I have a spinner menu attached to my toolbar.
toolbar_spinner_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/overview_spinner"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always"
android:title="" />
</menu>
I inflated the menu in my fragment by calling
AFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_spinner_menu, menu);
final int[] previousSelection = {0};
final MenuItem item = menu.findItem(R.id.overview_spinner);
final Spinner mSpinner = (Spinner) MenuItemCompat.getActionView(item);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, SPINNER_LIST);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0: // Recent
break;
case 1: // Now
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
super.onCreateOptionsMenu(menu, inflater);
}
R.layout.spinner_item
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="start" />
When an item is selected from the spinner, the user will be prompted to select a month and year from a dialog box. Upon clicking on "OK", the spinner's text should be updated into the choice made.
But this is what i get when I try to update the text dynamically using the following block of code, and as you can see, the text got cut off.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView selectedText = (TextView) getActivity().findViewById(R.id.spinner_text);
switch (position) {
case 0: // Recent
selectedText.setText("Since 1/8/2016");
break;
case 1: // Now
What I want to achieve is as following in the image illustration from Citymapper.
Any help rendered will be much appreciated!
I think the problem is with the spinner item width..your textview is getting too much space from the action bar rather than the space available..try lowering the textsize or you can remove the title from action bar as shown in the green one.
Solved the issue by modifying R.layout.spinner_item by changing gravity to start from right and setting a fixed layout_width in addition to some padding so it does not stick too close to the icon.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="150dp"
android:paddingRight="8dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="right" />
I have a requirement to create a button with close icon like as below:
i have use button and i have fatch problem when i click on button remove button.
but my requirement is onclick close icon when remove form list.
so please help me which control use so my problem solution.
Use this code it will solved your problem
buttondelete.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (buttondelete.getRight() - buttondelete.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
return true;
}
}
return false;
}
});
i would add to flow layout customLaout which contains of grey background and linear layout with 2 elements inside. TextView and ImageView.
Both has own ClickListeners
something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="Example Text"
android:id="#+id/buttonTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/buttonExitIcon"
android:src="#drawable/your_image_source"
/>
</LinearLayout>
and add Click listeners for both imageView and TextView
{
RelativeLayout mainView = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
Button bt_close = (Button)child.findviewById(R.id.btnclose);
bt_put.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout pView = (RelativeLayout)v.getParent();
mainView.removeView(pView);
}
});
mainView.addView(child);
}
Create a child.xml using the RelativeLayout with childs (TextView for text and Button as Remove button)
Inflate the xml layout , add each views into your main_layout and
setonclicklistner to button.
when clicked on button , get the parent view from the clicked view and remove the view from main_layout.
this may help you.
I need to create a Widget which look like photo below:
Upper part of photo resembles normal state of my costume widget and lower part of it resembles highlight state of the widget.
Widget primary features:
Both Normal and Highlighted sates could have a background with image and color at same time.
It could re-size by width easily (for example 200dp or 100dp)
I have 3 questions:
Is there any particular widget which could help me to create something like what I said?
Or should I create a custom widget myself?
Create a custom layout with a Vertical LinearLayout and two child TextView for Title and description.
Set the description TextView visibility as GONE , since you need to show the description only on highlight state.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_highlight"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/image_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold"
android:textSize="30dp"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! Iam here to display the description"
android:textStyle="italic"
android:textSize="25dp"
android:layout_margin="5dp"
android:visibility="gone"/>
</LinearLayout>
Now, on the code change the description TextView visibility as visible on long click.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_highlight);
final TextView descriptionTextView = (TextView)findViewById(R.id.description);
linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
descriptionTextView.setVisibility(View.VISIBLE);
return true;
}
});
Also , you need to hide back the description when long click is released. so you have to have onTouch listener like this..
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
descriptionTextView.setVisibility(View.GONE);
}
return false;
}
});
I need to use spinner as a menu. But problem is that when I click on an item it gets selected and shown at that place a behavior which I want to avoid. Secondly I need that the very first item should always be a heading no matter which of the items has been selected as in the following images:
spinner in normal condition
when user taps the spinner
Now if user taps any of the items heading should not be changed but item should be selected. I have achieved it using ListView but I think I must use proper Android components (if it is really possible).
Thanks in advance.
I have solved the above issue using the following code, but need to use spinner.
layout file
<RelativeLayout
android:id="#+id/header_main"
style="#style/layout_f_w"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/heading_color" >
<TextView
android:id="#+id/headingText"
style="#style/layout_wrap"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="MATCH CENTER"
android:textColor="#color/text_color_white"
android:textSize="15sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/matchcenter_menu"
style="#style/layout_wrap"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/headingText"
android:background="#android:color/transparent"
android:paddingTop="20dp"
android:src="#drawable/drp_down_menu" />
</RelativeLayout>
....
<ListView
android:id="#+id/mainscreen_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:color/black"
android:cacheColorHint="#00000000"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/transparent" />
Setting the views.
TextView headingText = (TextView)findViewById(R.id.headingText);
headingText.setTypeface(Utils.getCustomFont(LiveScoreCrowdScreen.this));
headingText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
matcheCenterMenu = (ImageButton)findViewById(R.id.matchcenter_menu);
matcheCenterMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
....
public void showMatchCenterMenu(View btn) {
ScreenMenuItemsAdapter adapter = null;
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
adapter = new ScreenMenuItemsAdapter(LiveScoreCrowdScreen.this, getResources().getStringArray(R.array.livescorecard_menuitems));
mainscreenMenu.setAdapter(adapter);
mainscreenMenu.setVisibility(View.VISIBLE);
mainscreenMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View row, int position, long id) {
CCApplication.isMenuOpened = false;
switch (position) {
case 0:// Refresh
//refresh screen
break;
case 1:// Highlights
//get highlights
break;
case 2:// Preferences
//get preferences
break;
case 3:// current time
// get current time
break;
}
mainscreenMenu.setVisibility(View.GONE);
}
});
}
just a hacking solution not proper one:
Again set the adapter with the spinner when you done all works in side onItemSelectedListener().
i.e
spinner.setAdapter(your_adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
//Do works here
//atlast again load the same adapter
spinner.setAdapter(your_adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
It will load the same adapter after selecting an item and show you like before.
What i did so far is a list view of textviews having the normal text and clickable spans:
Clicking the span i'm opening the URL, clicking the item View around the textView leads to the listView OnItemClickListener navigating to the item details, that's fine:
Now the problem is:
touching the textView makes the normal text be kinda highlighted (with the same color it has when the item is selected completely), textView's OnTouchListener touch event fires but not OnFocusChangeListener event and the item's View does not get the selection style. Tried all the variations of FOCUS_BLOCK_DESCENDANTS for listView, item View, the textView focusable was enabled or disabled with the same result.
Fortunately, textView OnClickListener event fires this way, but that's so ugly: the text is invisible while the touch is not released as the selected text color is the same as the item color, there's no other indication that the user is going to the item details other than that ugly text vanishing.
I suspect that happens because the content of the textView is Spannable, and the parts which are not CliclableSpan-s behave in this strange way.
Any chance i could select the item once the normal text is touched ?
The listView item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusable="false" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:focusable="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:focusable="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:focusable="false"
android:gravity="fill_horizontal|right"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
With the text view setClickable(false) i'm able to disable this weird selection style in the way that nothing happens while touching the text view area, not good but might be useful for solution.
Also tried to add not focusable & not clickable button to each item, when it's touched the complete item is selected and when touch is released the item's click event is passed, that's exactly what i expected from the textView with Spannable content.
Did you try setting the background of your TextView to Android's default list_selector_background?
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setBackgroundResource(android.R.drawable.list_selector_background);
For me it seems to give the result that you want.
UPDATE: After seeing that the item is not just a single TextView
Well, this is not a perfect solution (since it's probably better to fix the TextView - ListView highlighting interaction somehow), but it works well enough.
I figured out that instead of setting the movement method on the TextView (that triggers the issue), it is just simpler to check in the ListView's onItemClick() (after a click is definitely confirmed) to see if we should launch the onClick() on our ClickableSpans:
public class MyActivity extends Activity {
private final Rect mLastTouch = new Rect();
private boolean spanClicked(ListView list, View view, int textViewId) {
final TextView widget = (TextView) view.findViewById(textViewId);
list.offsetRectIntoDescendantCoords(widget, mLastTouch);
int x = mLastTouch.right;
int y = mLastTouch.bottom;
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
final Layout layout = widget.getLayout();
final int line = layout.getLineForVertical(y);
final int off = layout.getOffsetForHorizontal(line, x);
final Editable buffer = widget.getEditableText();
final ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length == 0) return false;
link[0].onClick(widget);
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (spanClicked(listView, view, R.id.details)) return;
// no span is clicked, normal onItemClick handling code here ..
}
});
listView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
mLastTouch.right = (int) event.getX();
mLastTouch.bottom = (int) event.getY();
}
return false;
}
});
// ...
}
}
The spanClicked() method is basically an abbreviated version of the LinkMovementMethod's onTouchEvent() method in the framework. To capture the last MotionEvent coordinates (to check for the click event), we simply add an OnTouchListener on our ListView.
Use TextView's setHighlightColor(Color.TRANSPARENT);
I think best way to solve this problem is using a selector. you can chan selection backgroun color so they will seen same
example selector code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
implement sth like that to your link.
let me know about result
Quote from documentation of the setMovementMethod() of TextView
Sets the movement method (arrow key handler) to be used for this
TextView. This can be null to disallow using the arrow keys to move
the cursor or scroll the view.
Be warned that if you want a TextView
with a key listener or movement method not to be focusable, or if you
want a TextView without a key listener or movement method to be
focusable, you must call android.view.View.setFocusable(boolean) again
after calling this to get the focusability back the way you want it.
The second paragraph explains that we should explicitly call the setFocusable method after setting the LinkMovementMethod to get focusability working the way we want.
You need to do it in code after inflating the list item.
Once you are able to get the focus, you can set the selector to have a different color text.
Here is the source code of setMovementMethod.