How to make an EditText to be focused but not clickable?(Xamarin.Android) - 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;
}
}

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 .

Click event fires when scrolling on EditText to see all its content(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>

sizing issue of gridview in nested scrollview

you can see gridview in the image which is not wrapping whole nestedscrollview Guys please help me I am stucked in this code, I need a collapsing toolbar layout and I succeed too but the gridview inside the nested scrollview is not wrapping total space of nestedscrollview, I tried many ways but failed.
This is the xml which is helping me to collapse toolbar and where I am getting gridview issues,
<?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:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.patelsanket.myalbum.activities.AlbumImageDisplayActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_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:id="#+id/headerImage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/shows_header_image"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:src="#drawable/cod" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
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>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="#+id/album_image_grid_view"
android:layout_width="match_parent"
android:layout_height="550dip"
android:choiceMode="multipleChoice"
android:horizontalSpacing="0.5dp"
android:numColumns="2"
android:paddingBottom="20dip" />
</RelativeLayout>
</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_margin="#dimen/fab_margin"
android:src="#drawable/ic_add"
app:backgroundTint="#009688"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end|right" />
</android.support.design.widget.CoordinatorLayout>
Use RecyclerView with GridLayoutManager. As Recycler view is compitable with Collasping bartoolbar too...
I hope it helps..
After searching a lot I found that using expandable gridview it can be solved. I used the following class and it is working fine.
This is a custom implementation of the native GridView ,
Here we override the functionality pertaining to GridView's height.
This implementation does away with a fixed Height GridView , rather
the height of the GridView expands depending upon the number of child elements added into it.
public class ExpandableGridView extends GridView{
boolean expanded = false;
public boolean isExpanded()
{
return expanded;
}
public ExpandableGridView(Context context)
{
super(context);
}
public ExpandableGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
add this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
album_image_grid_view.setNestedScrollingEnabled(true);
}
or you can use RecyclerView instead of GridView

Problems creating a custom controller for Xamarin Android

I am porting a native Android app to Xamarin + MVVMCross. On my Native Android app I was easily able to create my custom DateDisplayPicker and use it. I'm having trouble in Xamarin. My control lives here:
My DateDisplayPicker.cs looks like so:
namespace XxxxXxxx.Droid.CustomViews
{
public class DateDisplayPicker : TextView, DatePickerDialog.IOnDateSetListener
{
private readonly Context context;
private readonly Calendar calendar;
protected DateDisplayPicker(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public DateDisplayPicker(Context context) : base(context)
{
}
public DateDisplayPicker(Context context, IAttributeSet attrs) : base(context, attrs)
{
this.context = context;
SetAttributes();
this.calendar = Calendar.Instance;
}
public DateDisplayPicker(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
this.context = context;
calendar = Calendar.Instance;
}
private void SetAttributes()
{
Click += (sender, args) => ShowDateDialog();
}
private void ShowDateDialog()
{
var c = Calendar.Instance;
var dp = new DatePickerDialog(context, this, c.Get(CalendarField.Year),
c.Get(CalendarField.Month), c.Get(CalendarField.DayOfMonth));
dp.Show();
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Text = String.Format("%s/%s/%s", monthOfYear + 1, dayOfMonth, year);
calendar.Set(year, monthOfYear, dayOfMonth);
}
public void AddDay()
{
calendar.Add(CalendarField.Date, 1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public void SubtractDay()
{
calendar.Add(CalendarField.Date, -1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public string GetDate()
{
var sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.Format(calendar.Time);
}
}
}
And them I'm accessing it in my Android fragment like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
android:id="#+id/managerDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="XX-XX-XXXX"
android:textSize="16sp"
android:textColor="#android:color/holo_blue_dark"
style="#android:style/Widget.Holo.Spinner"
android:hint="Date" />
<ImageButton
android:id="#+id/managerDateMinusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/managerDate"
android:layout_marginRight="#dimen/button_padding"
android:src="#drawable/minus"
android:background="#android:color/transparent" />
<ImageButton
android:id="#+id/managerDatePlusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/managerDate"
android:layout_marginLeft="#dimen/button_padding"
android:src="#drawable/plus"
android:background="#android:color/transparent" />
</RelativeLayout>
<ExpandableListView
android:id="#+id/managerExpandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:stackFromBottom="false"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="#+id/assignedMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="No tasks today."
android:visibility="visible"
android:layout_below="#id/progressBar1" />
</RelativeLayout>
From the few examples I've seen this is pretty much how they've been set up but I must be missing something. Any ideas? I've confirmed my namespace for the control in the axml is all lowercase. When my application errors out I get:
Pending exception is:
android.view.InflateException: Binary XML file line #1: Error inflating class com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
(raw stack trace not found)
Caused by:
java.lang.ClassNotFoundException: Didn't find class "com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker" on path: DexPathList[[zip file "/data/app/com.workspace.xxxxxxxx-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.workspace.xxxxxxxx-1, /vendor/lib, /system/lib]]
Never mind I was just being dumb. If I leave off the "com." this works so it seems like the lesson learned here is to use EXACTLY what the namespace is on the control class, in this case it was "xxxxxxxx.droid.customviews.DateDisplayPicker" in the axml. Hope this helps anyone else who strugged with this!

Resources