How to change the basic blue color of my-location layer.
And can I replace it with marker. Or i have to buld custom Tile.
And can i remove only pin with the radius and keep che locate-me tile
I didn't find any style cusomization. And the marker cannot be replaced.
I did a custom tile, and a custom getCurrentLocation function.
Here is my code:
First you need tile for the Locate me button in the top right corner. I add it to the Layout:
<?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="fill_parent"
android:layout_height="fill_parent"
tools:context="android.support.v4.app.FragmentActivity" >
<fragment
android:id="#+id/tabs_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.bucons.savetime.MyMapFragment" />
<ImageButton
android:id="#+id/myLocationButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="4dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_menu_mylocation"
android:background="#drawable/map_tile_bg" />
</RelativeLayout>
Needed resources are #drawable/ic_menu_mylocation
You can get it from: https://docs.google.com/file/d/0BzGos46YSzYfWkRYS2ZMNmdZREk/edit
And for Background: #drawable/map_tile_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke
android:width="1px"
android:color="#color/map_tile_bg_stroke_pressed"/>
<solid android:color="#color/map_tile_bg_solid_pressed"/>
<corners android:radius="1dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke
android:width="1px"
android:color="#color/map_tile_bg_stroke"/>
<solid android:color="#color/map_tile_bg_solid"/>
<corners android:radius="1dp"/>
</shape>
</item>
</selector>
Color resourser are:
<color name="map_tile_bg_solid">#aaffffff</color>
<color name="map_tile_bg_stroke">#ffcccccc</color>
<color name="map_tile_bg_solid_pressed">#aaFF9933</color>
<color name="map_tile_bg_stroke_pressed">#ffcccccc</color>
And here is my fragment:
public class MyMapFragment extends SupportMapFragment {
//Current location Marker
private Marker myLocationMarker;
GoogleMap map = null;
public MyMapFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setUpMapIfNeeded();
getCurrentLocation();
}
//set the map and set the tile for locate me button
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
map = getMap();
// Check if we were successful in obtaining the map.
if (map != null) {
//Top right button for go to location
ImageButton locateMeTile = (ImageButton) getSherlockActivity().findViewById(R.id.myLocationButton);
locateMeTile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getCurrentLocation();
}
});
}
}
}
//retrieve the current position. Only once
private void getCurrentLocation() {
locationManager = (LocationManager) getSherlockActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
String towers = locationManager.getBestProvider(crit, false);
Location location = locationManager.getLastKnownLocation(towers);
Double glat = null;
Double glon = null;
if(location != null){
glat = location.getLatitude();
glon = location.getLongitude();
}
CameraPosition pos = new CameraPosition.Builder()
.target(new LatLng(glat, glon))
.zoom(10)
.build();
if(pos != null) {
currLocationChange(pos.target);
map.animateCamera(CameraUpdateFactory.newCameraPosition(pos));
} else {
Toast.makeText(getSherlockActivity(), R.string.main_error, Toast.LENGTH_LONG).show();
}
}
//Add pin to the current Location or move existing
private void currLocationChange(LatLng loc) {
if(myLocationMarker != null) {
myLocationMarker.setPosition(new LatLng(loc.latitude,loc.longitude));
} else {
myLocationMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(loc.latitude,loc.longitude)));
}
}
}
Related
I want to have the following screen:
When I click IMAGE 1 and IMAGE 2 buttons I want in IMAGE ZONE to appear Image1.jpg and Image2.jpg respectively. I want Image1.jpg and Image2.jpg to be in different fragments and replace those fragments on button click rather than changing simply the android:src of the ImageView in IMAGE ZONE on button click. I want Image1.jpg to be the first image to appear when opening the activity. I can't find how to do that with fragments in Xamarin.Android
You can create two Fragment and its xml first.
FragmentOne :
public class Fragment1 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
return inflater.Inflate(Resource.Layout.layoutFragment1, container, false);
//return base.OnCreateView(inflater, container, savedInstanceState);
}
}
and its xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/th2"/>
</LinearLayout>
FragementTwo :
public class Fragment2 : Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
return inflater.Inflate(Resource.Layout.layoutFragment2, container, false);
//return base.OnCreateView(inflater, container, savedInstanceState);
}
}
and its xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment2">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/th2"/>
</LinearLayout>
Then in MainActivity , you can set its xml as follow :
<?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">
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="80dip"
android:orientation="horizontal">
<Button
android:id="#+id/buttonone"
android:layout_width="150dip"
android:layout_height="80dip"
android:text="FragmentOne"/>
<Button
android:id="#+id/buttontwo"
android:layout_width="150dip"
android:layout_height="80dip"
android:text="FragmentTwo"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="430dip">
</RelativeLayout>
</LinearLayout>
Finally , in MainActivity.cs implement this function :
public class MainActivity : AppCompatActivity
{
Android.Support.V4.App.Fragment fragmentOne;
Android.Support.V4.App.Fragment fragmentTwo;
Android.Support.V4.App.FragmentTransaction fragmentManager;
[Obsolete]
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button buttonone = FindViewById<Button>(Resource.Id.buttonone);
buttonone.Click += Buttonone_Click;
Button buttontwo = FindViewById<Button>(Resource.Id.buttontwo);
buttontwo.Click += Buttontwo_Click;
fragmentOne = new Fragment1();
fragmentTwo = new Fragment2();
fragmentManager = SupportFragmentManager.BeginTransaction();
fragmentManager.Add(Resource.Id.containerView, fragmentOne);
//adding first fragment when entering activity
fragmentManager.Commit();
}
private void Buttonone_Click(object sender, System.EventArgs e)
{
//throw new System.NotImplementedException();
Console.WriteLine("Buttonone_Click");
fragmentManager = SupportFragmentManager.BeginTransaction();
// replace to be the first fragment
fragmentManager.Replace(Resource.Id.containerView, fragmentOne);
fragmentManager.Commit();
}
private void Buttontwo_Click(object sender, System.EventArgs e)
{
//throw new System.NotImplementedException();
Console.WriteLine("Buttontwo_Click");
fragmentManager = SupportFragmentManager.BeginTransaction();
// replace ro be the second fragment
fragmentManager.Replace(Resource.Id.containerView, fragmentTwo);
fragmentManager.Commit();
}
}
The effect as follow :
I have a simple Xamarin.Android-App.
On the Activity of this app, there is a list displayed. Please see following MyActivity.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width ="match_parent"
android:layout_height="wrap_content"
android:text ="Click Me"
android:layout_marginTop ="20dp"
android:layout_marginLeft ="25dp"
android:layout_marginRight ="25dp"
android:id="#+id/button" />
<Space
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="#+id/space" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list" />
</LinearLayout>
One row of this list contains simply two entries, please see ListRow.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
</RelativeLayout>
So the code-behind of that activity looks like the following:
public class MyActivity : Activity
{
List<Entry> List = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MyActivity);
List = PopulateList();
var lv = FindViewById<ListView>(Resource.Id.list);
var adapter = new ListAdapter(this, Resource.Layout.List, List);
lv.Adapter = adapter;
FindViewById<Button>(Resource.Id.button).Click += OnButtonClick;
}
}
For the sake of completeness, here is the code for my ListAdapter.cs-class:
class ListAdapter : ArrayAdapter
{
List<Entry> List;
public ListAdapter(Context Context, int ListId, List<Entry> List) : base(Context, ListId, List)
{
this.List = List;
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
return v;
}
}
So my question now is the following: Assuming that there is more than one item within that List. On click of the button, I want to change one specific text within that list. Let's say of the second entry in the list the (Resource.Id.value).Text (which now says "Original Value") to "Changed Value" or something like that. But only of the second one. All the other items should stay the same.
Please see following example of output aimed, maybe it's easier to understand what I am trying to do:
Name Value
- - - - - - - - - - - - - - - -
No.1 Original Value
No.2 Original Value
No.3 Original Value
[Button Click]
Name Value
- - - - - - - - - - - - - - - -
No.1 Original Value
No.2 Changed Value
No.3 Original Value
Can anyone maybe help me / tell me how to do this? What does my private void OnButtonClick(object sender, EventArgs e)-method have to look like? How can I access a single entry in that list?
Thanks in advance for all answers and best regards
you could add a method in adapter to notifyDataSetChanged(); :
class ListAdapter : ArrayAdapter
{
List<Entry> List;
private int ItemPositionToChange = -1;
public ListAdapter(Context Context, int ListId, List<Entry> List) :
base(Context, ListId, List)
{
this.List = List;
}
//this method to let adapter notify
public void ChangeItemValue(int position)
{
ItemPositionToChange = position;
NotifyDataSetChanged();
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater =
(LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
//change the item value according to your needs
if(position == ItemPositionToChange ){
v.FindViewById<TextView>(Resource.Id.value).Text = "Changed Value";
}else{
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
}
return v;
}
}
then in your button click listener :
void OnButtonClick(object sender, EventArgs e)
{
//positionToChange is which item you want to change
adapter.ChangeItemValue(positionToChange);
}
I'm not too familiar with the Entry class, but it seems to have a Text property.
Assuming you know the index of the string (myIndex below):
private void OnButtonClick(object sender, EventArgs e) {
List[myIndex].Text = "Changed Value";
adapter.notifyDataSetChanged();
}
This is my MainActivity
public class ProjectDetailsActivity extends AppCompatActivity {
private static final String TAG = ProjectDetailsActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
if (savedInstanceState == null) {
addFragment(new ProjectDetailsFragment(), false);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_detail);
toolbar.setNavigationIcon(R.drawable.ic_action_back);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Button Clicked", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void addFragment(Fragment f, boolean isAddToBack) {
FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container, f);
if (isAddToBack) {
mFragmentTransaction.addToBackStack(null);
}
mFragmentTransaction.commit();
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
This my xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.abcd.scrollappdemo.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_scrollable_image_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/title_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:text="Garden Residency II"
android:textColor="#ffffffff" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/include_layout"
layout="#layout/project_detail_content" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
This is my fragment class`
public class ProjectDetailsFragment extends BaseFragment implements View.OnClickListener {
private static final String TAG = ProjectDetailsFragment.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_details, container, false);
mappingWidgets(mView);
return mView;
}
private void mappingWidgets(View v) {
}
#Override
public void onClick(View v) {
}
}
Error Log :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setNavigationIcon(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setNavigationIcon(int)' on a null object reference
at com.abc.xyz.abcActivity.onCreate(ProjectDetailsActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Activity theme in Mainfest is -
#style/Apptheme.Noactionbar
Just by putting these code in fragment I am able to access the actionbar from fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.activity_detail, container, false);
final Toolbar toolbar = (Toolbar) mView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.setTitle("Project Details");
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)mView.findViewById(R.id.collapsing_toolbar);
I am new to android, I want to display actionbar icons in actionbar. Simple task, but I am not able to get this. I have uploaded my Mainactivity.java file.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_icons, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my Custom Menu.XML file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_favorite_border_white_48dp"
android:title="action_search"
android:showAsAction="ifRoom"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_star_rate_white_18dp"
android:title="action_location_found"
android:showAsAction="ifRoom" />
</menu>
I have also added my Mainfest file for reference.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.actionbaricons">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And I am running the application in my Android Mobile (Nexus 5 Android 6.0.0)
Thanks in adavance.
In your main activity you can set actionbar icon
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.ic_button); /* setting icon */
}
I want to display spinner in action bar(not in activity,in Fragment).for that i did below things
Step-1 (spinnermenu.xml)
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="#layout/spinner"/>
</menu>`
Step-2 (spinner.xml)
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="wrap_content" />
step-3 (Code)
public class All extends Fragment{
ArrayList<String> spinnerlist;
ArrayAdapter<String> spinneradapter;
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.spinnermenu, menu);
spinnerlist = new ArrayList<String>();
spinnerlist.add("Items1");
spinnerlist.add("Items2");
spinneradapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerlist);
Spinner s = (Spinner) menu.findItem(R.id.menuSort).getActionView();
s.setAdapter(spinneradapter);
super.onCreateOptionsMenu(menu, inflater);
}
}
I am getting error in this line
setHasOptionsMenu(true);
and Null exception in this line
s.setAdapter(spinneradapter);
Could any one tell me how to rectify this error?
*PS : Is there any way to do the same?*