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" />
Related
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;
}
});
I need spinner in Action Bar for filtering content, looking something similar to this:
But spinner should fill full length of action bar.
What i tried:
1) actionBar.setListNavigationCallbacks(navigationAdapter, this);
Yes, it works. But i need this filter only in 1 fragment. Yes, i can use ActionBar.NAVIGATION_MODE_LIST only in this fragment, and change it to ActionBar.NAVIGATION_MODE_STANDARD in others. But if i will want to add differenat filter in another fragment. So, i should to setting type, adapter and etc in every fragment. It's very very awfully.
2) Better solution is to use menu. In onCreateOptionsMenu of fragment i can add item with actionViewClass="android.widget.Spinner" only in fragment where i need this. Almost good solution except that this item will align right and doesn't fill full action bar.
So, my question is: how to make menu item fill full action bat length?
Or can you advise better solution?
Btw, setCustomView for ActionBar - bad solution for the same reasons that setListNavigationCallbacks...
Found solution. You should user your own layout for spinner item.
Instead of:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.projects_filteres, android.R.layout.simple_spinner_item);
Use:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
Where R.array.projects_filteres - just simple array from resources, and R.layout.projects_filter_item is RelativeLayout with android:layout_width="match_parent":
<?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="wrap_content"
android:gravity="fill_horizontal"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
So, whole code of onCreateOptionsMenu in fragment looks something like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.action_bar_context_menu, menu);
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner)MenuItemCompat.getActionView( filter);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
I have an EditText and its text gets selected from through code. But I want to allow users to cut/copy the selected text. However, the cut/copy context menu doesn't appear until user long presses on text. But then it loses the actual selection. So, I'm thinking to show the context menu as the text get selected by the code.
I tried this inside onFocusChanged, but nothing appeared.
openContextMenu(EditText);
IF I follow you usecase correctly, you can open context menu from the onFocusChangeListener registered on the testedEditText.
I prepared some small test for that which seems to be correctly supporting your usecase.
You need to openMenu on the hooks which are selecting the content in the EditText.
public class Main extends Activity {
private EditText testedEditText;
private Button selectingButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectingButton = (Button) findViewById(R.id.button);
testedEditText = (EditText) findViewById(R.id.textView);
registerForContextMenu(testedEditText);
selectingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testedEditText.setSelection(6, 11);
openContextMenu(testedEditText);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_all:
return true;
case R.id.copy:
//do something
return true;
case R.id.cut:
//do something
return true;
case R.id.paste:
//do something
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Weirdly enough registering testedEditText.requestFocus(), and setting onFocusChangedListener for EditText was not enough.
Additional xml files for reference:
cmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_all"
android:title="select all"
/>
<item android:id="#+id/copy"
android:title="copy"
/>
<item android:id="#+id/cut"
android:title="cut"
/>
<item android:id="#+id/paste"
android:title="paste"
/>
</menu>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Initial Text..."
android:layout_centerInParent="true"
/>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="button"
/>
</RelativeLayout>
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.
I have this code:
ListView listView;
static final String[] Liste = {"Satz des Pythagoras", "abc-Formel", "pq-Formel"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("CalcPlus");
listView = getListView();
//Own row layout
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, Liste));
listView.setOnItemClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.maths:
return true;
case R.id.physics:
Intent myIntent2 = new Intent(Home.this, ActivityPhysics.class);
startActivity(myIntent2);
default:
return super.onOptionsItemSelected(item);
}
}
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
if ("Satz des Pythagoras".equals(Liste[position])) {
Intent sdp = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(sdp, 0);
}
else if ("abc-Formel".equals(Liste[position])) {
Intent abc = new Intent(view.getContext(), ActivityABC.class);
startActivityForResult(abc, 0);
}
else if ("pq-Formel".equals(Liste[position])) {
Intent pq = new Intent(view.getContext(), ActivityPQ.class);
startActivityForResult(pq, 0);
}
}
This should display a list and a actionbar.
It did, for some time.
It looks like I messed up the code and, since I'm not having that much experience, can't find my mistake...so could someone review my code? :)
Because now it loads the activity..but doesn't display the list or the actionbar :o
Here is the XML in \menu\activity_home.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/maths"
android:title="#string/maths"
android:orderInCategory="1"
android:showAsAction="always|withText"
android:titleCondensed="#string/maths"
android:icon="#drawable/ic_action_calculator" />
<item android:id="#+id/physics"
android:icon="#drawable/ic_action_line_chart"
android:title="#string/physics"
android:orderInCategory="2"
android:showAsAction="always|withText"/>
This is layout\activity_home.xml:
<?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="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
</RelativeLayout>
So basically it's empty.
If you are using a ListActivity, your R.layout.activity_home has to have a ListView like:
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then you can create a new layout for the list view item in your adapter or use a predefined layout for the list item like:
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Liste));
With these two lines:
setContentView(R.layout.activity_home);
...
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, Liste));
You are using the same layout for the entire Activity and each row, which doesn't make sense. The Activity's layout should contain a ListView and the row layout shouldn't...
To use the default layouts, remove this line:
setContentView(R.layout.activity_home);
and change this one:
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Liste));
Also you need to onListItemClick() not onItemClick() to have the row clicks work.
#Override
protected void onListItemClick (ListView l, View v, int position, long id) {...}
Okay now the only entry in LogCat is Array Adapter:"You musst supply a resource ID for a textview.
You must give your TextView an id:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
And change the way you create your adapter to:
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.activity_home, R.id.text, Liste));