Skip to content

Commit c72cca3

Browse files
author
Luis
committed
Merge pull request #423 from LuisAlbertoPenaNunez/feature/422-Hide-keyboard-on-MainPage-when-the-app-start
Hide keyboard on MainPage when the app start
2 parents 1af1af3 + 93a1ef1 commit c72cca3

7 files changed

Lines changed: 70 additions & 6 deletions

File tree

Mobile/Android/Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
<Compile Include="About\AboutViewModel.cs" />
190190
<Compile Include="About\ContributorAddedEventHandler.cs" />
191191
<Compile Include="Services\EmailService.cs" />
192+
<Compile Include="Services\KeyboardService.cs" />
192193
</ItemGroup>
193194
<ItemGroup>
194195
<None Include="Resources\AboutResources.txt" />

Mobile/Android/Framework/Bootstrapping/Bootstrapping.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Api.Contract;
1111
using Api;
1212
using APIs;
13+
using Android.Views.InputMethods;
1314

1415
namespace Empleado
1516
{
@@ -64,6 +65,7 @@ void Run ()
6465
SimpleIoc.Default.Register<IJobsApiService, FakeJobsApiService>();
6566
SimpleIoc.Default.Register<IGithubContributorService, OctocatContributorService>();
6667
SimpleIoc.Default.Register<IEmailService, EmailService> ();
68+
SimpleIoc.Default.Register<IKeyboardService, KeyboardService>();
6769
}
6870
}
6971

Mobile/Android/MainPage/MainPageFragment.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Microsoft.Practices.ServiceLocation;
1616
using GalaSoft.MvvmLight.Messaging;
1717
using Android.Support.V4.View;
18+
using Android.Views.InputMethods;
19+
using Core;
1820

1921
namespace Android
2022
{
@@ -32,6 +34,8 @@ public class MainPageFragment : Android.Support.V4.App.Fragment
3234

3335
IMessenger _messenger;
3436

37+
IKeyboardService _keyboardService;
38+
3539
public override void OnCreate (Bundle savedInstanceState)
3640
{
3741
base.OnCreate (savedInstanceState);
@@ -57,6 +61,8 @@ void GetServices ()
5761
{
5862
_viewModel = ServiceLocator.Current.GetInstance<MainPageFragmentViewModel>();
5963

64+
_keyboardService = ServiceLocator.Current.GetInstance<IKeyboardService>();
65+
6066
_messenger = GalaSoft.MvvmLight.Messaging.Messenger.Default;
6167
}
6268

@@ -78,9 +84,13 @@ void SetUpScreen()
7884

7985
_searchView.QueryTextChange += OnQueryTextChanged;
8086

81-
_searchView.SetIconifiedByDefault(false);
87+
_searchView.Focusable = true;
88+
89+
_searchView.RequestFocusFromTouch();
90+
91+
_searchView.ClearFocus();
8292

83-
_searchView.SetQueryHint(GetString(Resource.String.HomePageSearchBarHint));
93+
// _searchView.SetQueryHint(GetString(Resource.String.HomePageSearchBarHint));
8494

8595
PersonalizeSearchView();
8696
}
@@ -119,7 +129,11 @@ public override void OnViewCreated (View view, Bundle savedInstanceState)
119129

120130
void OnSearchLayoutSelected(object sender, EventArgs e)
121131
{
122-
_searchView.RequestFocus();
132+
_searchView.SetIconifiedByDefault(false);
133+
134+
_searchView.RequestFocusFromTouch();
135+
136+
_keyboardService.ShowKeyboard(_searchView);
123137
}
124138

125139
//I know this is wrong, but i will fix this later.
@@ -128,8 +142,6 @@ void OnSearchLayoutSelected(object sender, EventArgs e)
128142
//So somehow i need to accomplish this task
129143
void OnQuerySubmit(object sender, SearchView.QueryTextSubmitEventArgs e)
130144
{
131-
132-
133145
_viewModel.UserIsTypingCommand.Execute(_searchView.Query);
134146
}
135147

@@ -149,7 +161,7 @@ public override View OnCreateView (LayoutInflater inflater, ViewGroup container,
149161
{
150162
var view = inflater.Inflate(Resource.Layout.MainPageFragmentLayout, container, false);
151163

152-
_searchLayout = view.FindViewById (Resource.Id.MainSearchLayout);
164+
_searchLayout = view.FindViewById (Resource.Id.segueta);
153165

154166
_locationContainer = view.FindViewById<LinearLayout> (Resource.Id.locationContainer);
155167

Mobile/Android/Resources/layout/MainPageFragmentLayout.axml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
app:cardCornerRadius="4dp"
1414
android:layout_margin="5dp">
1515
<LinearLayout
16+
android:id="@+id/segueta"
17+
android:clickable="true"
1618
android:layout_width="match_parent"
1719
android:weightSum="1"
1820
android:layout_height="wrap_content">
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Core;
3+
using Android.Views.InputMethods;
4+
using Android.Content;
5+
using Android.Views;
6+
7+
namespace Android
8+
{
9+
public class KeyboardService : IKeyboardService
10+
{
11+
InputMethodManager _inputMethodManager;
12+
13+
IContextService _contextService;
14+
15+
public KeyboardService (IContextService contextService)
16+
{
17+
_contextService = contextService;
18+
19+
_inputMethodManager =
20+
((Context)_contextService.GetContext ()).GetSystemService (Context.InputMethodService) as InputMethodManager;
21+
}
22+
23+
public void ShowKeyboard (object focusOver)
24+
{
25+
if(focusOver == null)
26+
throw new ArgumentNullException("focusOver");
27+
28+
var focus = (View) focusOver;
29+
30+
_inputMethodManager.ShowSoftInput(focus, ShowFlags.Forced);
31+
32+
_inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
33+
}
34+
}
35+
}
36+

Mobile/Core/Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="Github\GithubUser.cs" />
5151
<Compile Include="Github\OctocatContributorService.cs" />
5252
<Compile Include="Services\IEmailService.cs" />
53+
<Compile Include="IKeyboardService.cs" />
5354
</ItemGroup>
5455
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
5556
<ItemGroup>

Mobile/Core/IKeyboardService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Core
4+
{
5+
public interface IKeyboardService
6+
{
7+
void ShowKeyboard(object toFocusOn);
8+
}
9+
}
10+

0 commit comments

Comments
 (0)