Click event fires when scrolling on EditText to see all its content(Xamarin.Android) - xamarin.android

When I scroll in an EditText to see its content when it's too long, its click event fires. How can I prevent that? The EditText is not editable.
Here is my activity:
public class MainActivity : AppCompatActivity, View.IOnTouchListener
{
EditText et;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
et = FindViewById<EditText>(Resource.Id.editText1);
et.SetOnTouchListener(this);
et.Click += delegate { this.OnClicking(); };
}
private void OnClicking()
{
//Show alert dialog
}
public bool OnTouch(View v, MotionEvent e)
{
v.OnTouchEvent(e);
var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService("input_method");
imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
return true;
}
}
Here is my axml file:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:text="Very long text Very long text Very long text Very long text Very long text "
android:layout_width="300dp"
android:focusable="false"
android:clickable="true"
android:singleLine="true"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/editText1" />
</LinearLayout>

Related

Android - EditText hint not updating in soft keyboard screen

The hint for my EditText when the soft keyboard is full screen mode is not reflecting the correct value after assigning a new value for hint to the EditText. Please see gif above.
Does anyone know how to resolve this? Below is isolated code to recreate this issue:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());
}
public void onButtonClick(View v) {
mBinding.barcodeEdittext.setHint("Some other Hint");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/barcode_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Initial Hint" />
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:text="Change Hint!"
android:onClick="onButtonClick" />
</LinearLayout>

Issue with Forward and backward options in Video player in Xamarin Android

i need to display a video in mobile app which was developed in Xamarin(Andriod) in which the for forward it moving 15 sec ahead and for backward it is moving 5 sec back
Can i get some help on this
In your case you seems to use the MediaController . However , if you want to custom the length of forward and back , you need to create a custom MediaController .
in XML
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
android:orientation="vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/videoView1" />
<LinearLayout
android:background="#android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="back"
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
android:text="play"
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<Button
android:text="forward"
android:id="#+id/forward"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
in Activity
VideoView videoView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
//...
videoView = FindViewById<VideoView>(Resource.Id.videoView1);
videoView.SetVideoURI(Android.Net.Uri.Parse("xxx.mp4")); // Path of your saved video file.
videoView.Start();
Button back = FindViewById<Button>(Resource.Id.back);
back.Click += Back_Click;
Button play = FindViewById<Button>(Resource.Id.play);
play.Click += Play_Click;
Button forward = FindViewById<Button>(Resource.Id.forward);
forward.Click += Forward_Click; ;
}
private void Forward_Click(object sender, EventArgs e)
{
videoView.SeekTo(videoView.CurrentPosition + 5 * 1000);
}
private void Play_Click(object sender, EventArgs e)
{
var button = sender as Button;
if (videoView.IsPlaying)
{
button.Text = "Play";
videoView.Pause();
}
else
{
button.Text = "Pause";
videoView.Start();
}
}
private void Back_Click(object sender, EventArgs e)
{
videoView.SeekTo(videoView.CurrentPosition-15*1000);
}
And in this way you just need to set the icon of the Buttons as you want .

webview overlaps appbar in Xamarin Android app

i am using xamarin android app template with drawerlayout.i am using webview in app_bar_main.axml. when the activity is launched .it overlaps the appbar and does not show toolbar.my app_bar_main.axml is as follows: i want to show webview but below appbar.the webview is below toolbar but contents of toolbar are hidden.i want contents of toolbar so that i can navigate through toolbar in appbar .app_bar_main.axml .
<?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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appBarLayout"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_below="#id/toolbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:layout_below="#id/progressBar"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<WebView
android:id="#+id/StoreView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
the MainActivity.cs code is here.
the app_bar_main.axml is in activity_main.axml as produced by drawerlayout template.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.Click += FabOnClick;
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.SetNavigationItemSelectedListener(this);
// start store activity
if (IsOnline() == true)
{
message = "items";
var intent = new Intent(this, typeof(StoreActivity));
intent.PutExtra("Data", "message");
StartActivity(intent);
}
}
```
Here is a simple demo which achieve your function.The main code is as follows:
The activity_main.axml
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#33B86C"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_width="match_parent">
<FrameLayout
android:background="#android:color/holo_purple"
android:id="#+id/content_frame"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="200dp"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
MainActivity.cs
public class MainActivity : AppCompatActivity
{
DrawerLayout drawer;
NavigationView navigationView;
private string mDrawerTitle;
private string[] mContentTitles;
Toolbar toolbar;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
mDrawerTitle = this.Title;
mContentTitles= this.Resources.GetStringArray(Resource.Array.contents_array);
toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowTitleEnabled(false);
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
// Get our button from the layout resource,
// and attach an event to it
drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
if (navigationView != null)
setupDrawerContent(navigationView);
ActionBarDrawerToggle toggle = new MyActionBarDrawerToggle(this, drawer, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);
drawer.AddDrawerListener(toggle);
toggle.SyncState();
if (savedInstanceState == null) //first launch
{
toolbar.Title = mContentTitles[0];
var fragment = WebviewFragment.NewInstance(0);
var fragmentManager = this.FragmentManager;
var ft = fragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment);
ft.Commit();
}
}
void setupDrawerContent(NavigationView navigationView)
{
navigationView.NavigationItemSelected += (sender, e) => {
int ItemId = e.MenuItem.ItemId;
e.MenuItem.SetChecked(true);
int index = 0;
if (ItemId == Resource.Id.nav_home) {
index = 0;
} else if (ItemId == Resource.Id.nav_messages) {
index = 1;
}
else if (ItemId == Resource.Id.nav_about)
{
index = 2;
}
// update the main content by replacing fragments
var fragment = WebviewFragment.NewInstance(index);
var fragmentManager = this.FragmentManager;
var ft = fragmentManager.BeginTransaction();
ft.Replace(Resource.Id.content_frame, fragment);
ft.Commit();
// update selected item title, then close the drawer
mDrawerTitle = mContentTitles[index];
drawer.CloseDrawers();
};
}
internal class WebviewFragment : Fragment
{
public const string ARG_NUMBER = "planet_number";
public WebviewFragment()
{
// Empty constructor required for fragment subclasses
}
public static Fragment NewInstance(int position)
{
Fragment fragment = new WebviewFragment();
Bundle args = new Bundle();
args.PutInt(WebviewFragment.ARG_NUMBER, position);
fragment.Arguments = args;
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.fragment_content2, container, false);
var i = this.Arguments.GetInt(ARG_NUMBER);
var url = this.Resources.GetStringArray(Resource.Array.weburls_array)[i];
var title = this.Resources.GetStringArray(Resource.Array.contents_array)[i];
var web_view = rootView.FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new HelloWebViewClient());
web_view.LoadUrl(url);
this.Activity.Title = title;
return rootView;
}
}
internal class MyActionBarDrawerToggle : ActionBarDrawerToggle
{
MainActivity owner;
public MyActionBarDrawerToggle(MainActivity activity, DrawerLayout layout, Toolbar toolbar, int openRes, int closeRes)
: base(activity, layout, toolbar, openRes, closeRes)
{
owner = activity;
}
public override void OnDrawerClosed(View drawerView)
{
owner.toolbar.Title = owner.Title;
owner.InvalidateOptionsMenu();
}
public override void OnDrawerOpened(View drawerView)
{
owner.toolbar.Title = owner.mDrawerTitle;
owner.InvalidateOptionsMenu();
}
}
}
strings.xml
<resources>
<string name="app_name">DrawLayoutApp</string>
<string name="action_settings">Settings</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string-array name="contents_array">
<item>Home</item>
<item>Messages</item>
<item>About</item>
</string-array>
<string-array name="weburls_array">
<item>https://www.google.com/</item>
<item>https://msdn.itellyou.cn/</item>
<item>https://www.baidu.com/</item>
</string-array>
</resources>
menu.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:title="Home" />
<item
android:id="#+id/nav_messages"
android:title="Messages" />
<item
android:id="#+id/nav_about"
android:title="About" />
</group>
</menu>
The effect is as follows:

How to make an EditText to be focused but not clickable?(Xamarin.Android)

I have 3 EditText:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="false"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/editText1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="true"
android:clickable="false"
android:id="#+id/editText2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusedByDefault="false"
android:id="#+id/editText3" />
I want when the activity is opened the second EditText to be focused(with the cursor blinking in it) but when I tap on it I don't want the keyboard to show up.
Here's an example I put together that works for me. And with this solution, you can remove the "focusedByDefault" and "clickable" attributes from all of your EditText views in your layout file.
public class MainActivity : AppCompatActivity, View.IOnTouchListener {
private EditText editText2;
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main);
editText2 = FindViewById<EditText>(Resource.Id.editText2);
editText2.RequestFocus();
editText2.SetOnTouchListener(this); // Requires addition of View.IOnTouchListener interface to class
}
public bool OnTouch(View v, MotionEvent e) {
v.OnTouchEvent(e);
var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService(InputMethodService);
imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
return true;
}
}

How to add the icon to the right side in the NavigationView

I am working on Xamarin.android application, where it contains the Drawer layout with NavigationView. I need to change the icon to the right side instead of left of the textView.
here is my layout code:
<android.support.design.widget.NavigationView
android:id="#+id/questiontypes_nav_view"
android:layout_width="220dp"
android:ellipsize="end"
android:layout_below="#+id/navigation_drawer_top"
android:maxLines="1"
android:scrollbars="none"
android:layout_above="#+id/navigation_drawer_bottom"
android:nestedScrollingEnabled="true"
android:layoutDirection="ltr"
android:layout_height="match_parent"
app:theme="#style/NavigationDrawerStyle"
android:background="#031d2c"
android:fitsSystemWindows="true"
app:itemTextAppearance="?android:attr/textAppearanceSmall"
app:itemTextColor="#android:color/white"
app:itemBackground="#drawable/nav_item_drawable"
app:itemIconTint="#drawable/nav_item_drawable">
</android.support.design.widget.NavigationView>
is there any way to get icon to the right side in the NavigationView.
Based on your description, do you want to switch position between textview and icon? If so, you could create a listview to achieve it in NavigationView like the following code.
This is GIF of demo.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="220dp"
android:scrollbars="none"
android:nestedScrollingEnabled="true"
android:layoutDirection="ltr"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main">
<ListView
android:paddingTop="500px"
android:id="#+id/lst_menu_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
ListItem.axml(customize menu)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Ls_textview"
android:text="this is text"
android:textSize="15dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="18dp"/>
<ImageView
android:layout_marginLeft="50dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/Ls_ImageView"
android:src="#drawable/lcon"/>
</LinearLayout>
This is in OnCreate() method.
ListView listView = FindViewById<ListView>(Resource.Id.lst_menu_items);
List<ListItem> listItems = new List<ListItem>();
listItems.Add(new ListItem(Resource.Drawable.lcon, "Title1"));
listItems.Add(new ListItem(Resource.Drawable.lcon, "Title2"));
listItems.Add(new ListItem(Resource.Drawable.lcon, "Title3"));
listItems.Add(new ListItem(Resource.Drawable.lcon, "Title4"));
MyAdapter myAdapter= new MyAdapter(listItems, this);
listView.Adapter = myAdapter;
listView.ItemClick += (s, e) =>
{
OnClick(e.Position);
};
You could handle click event like following code.
private void OnClick(int position)
{
position++;
if (position == 1)
{
Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
}
else if (position == 2)
{
Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
}
else if (position == 3)
{
Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
}
else if (position == 4)
{
Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
}
DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
drawer.CloseDrawer(GravityCompat.Start);
}
This is adapter of listview
public class ListItem {
public ListItem(int ImageSrc, string ItemName)
{
this.ImageSrc = ImageSrc;
this.ItemName = ItemName;
}
public int ImageSrc { get; set; }
public string ItemName { get; set; }
}
public class MyAdapter : BaseAdapter
{
List<ListItem> listItems;
Context context;
public MyAdapter(List<ListItem> listItems, Context context)
{
this.listItems = listItems;
this.context = context;
}
public override int Count => listItems.Count;
public override Java.Lang.Object GetItem(int position)
{
throw new NotImplementedException();
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
convertView = LayoutInflater.From(context).Inflate(Resource.Layout.ListItem, parent, false);
TextView textView = convertView.FindViewById<TextView>(Resource.Id.Ls_textview);
ImageView imageView = convertView.FindViewById<ImageView>(Resource.Id.Ls_ImageView);
//int id=Resource.Drawable.Capture;
textView.Text = listItems[position].ItemName;
imageView.SetImageResource(listItems[position].ImageSrc);
return convertView;
}
}

Resources