can we cast EditText to TextView in android - android-edittext

by mistake i cast my editText view to Textview class object, but i did't get class cast exception
here is my code. let me explain , why this??
in my .xml file
<EditText
android:id="#+id/adduser_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_10"
android:hint="#string/mob_no"
android:inputType="phone"
/>
in activity class.
TextView adduser_phone = (TextView) findViewById(R.id.adduser_phone);
here not hit the class castexception. why..???

yes you can do so because EditText is sublcass of TextView
EditText is a thin veneer over TextView that configures itself to be editable.
checkout dev link1 dev link2for detail

Related

CustomComponent FindViewById from referenced project returns null

In C# Xamarin Android projects, I am trying to create a custom compound component to select a month. This extends LinearLayout.
I expect to re-use it (and keep my main project more tidy), so I have 2 projects - my main/parent project, containing my Activity and multiple Fragments; and a simple Android project for my component.
When I run the component project directly using its own Activity, it works as expected, but when I reference it from my main project, my FindViewById return null.
I can see that this and context are from the main project - and I would like to understand what I've done wrong or what I need to add so that it finds the component's layout and controls to inflate?
Main view (Ui.DateScrollPicker.Droid is my component's namespace and ScrollMonthPicker my class):
<?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:paddingLeft="10dp"
android:paddingRight="10dp">
.......
<ui.datescrollpicker.droid.ScrollMonthPicker
android:id="#+id/my_scroll_month_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Referenced from a Android.Support.V4.App.Fragment in the override View OnCreateView method:
scrollMonthPicker = view.FindViewById<ScrollMonthPicker>(Resource.Id.scroll_month_picker);
scrollMonthPicker.DateChanged += DatePicker_Changed;
By extending LinearLayout, I am implementing 2 constructors, including, ScrollMonthPicker(Context context, IAttributeSet attrs) : base(context, attrs, 0) which is called, and goes off to inflate my component's layout.
Component view (scroll_month_layout.axml):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/month_current_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month" />
<TextView
android:id="#+id/year_current_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year" />
<android.support.v7.widget.RecyclerView
android:id="#+id/month_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
ScrollMonthPicker constructor:
public ScrollMonthPicker(Context context, IAttributeSet attrs)
: base(context, attrs, 0)
{
InflateLayout(context, attrs);
}
ScrollMonthPicker inflating view:
private void InflateLayout(Context context, IAttributeSet attrs)
{
var inflater = LayoutInflater.FromContext(this.Context);
inflater.Inflate(Resource.Layout.scroll_month_layout, this);
monthCurrentText = FindViewById<TextView>(Resource.Id.month_current_text);
yearCurrentText = FindViewById<TextView>(Resource.Id.year_current_text);
monthRecyclerView = FindViewById<RecyclerView>(Resource.Id.month_recycler_view);
monthRecyclerView.HasFixedSize = true;
......
}
The component works fine when called from 'its own' Activity, but all FindViewById are null (monthCurrentText, yearCurrentText and monthRecyclerView) when referenced by the fragment in the main project.
If I wait for the exception to be thrown in the main fragment from monthRecyclerView.HasFixedSize, I do not get a null reference exception, but this one:
Unhandled Exception:
System.NotSupportedException: Could not activate JNI Handle 0x7fff30d40b00 (key_handle 0x8f4c53e) of Java type 'md53fd9f663a5e8ddcd0a5da1b57d05fd99/ScrollMonthPicker' as managed type 'Ui.DateScrollPicker.Droid.ScrollMonthPicker'.
How do I make it populate as expected by referencing from a separate project??
Many thanks.
It seems your both projects are the android project. You could not use another android project code in one project. I think you need to create your CustomComponent project as a library.

Close app after closing last fragment

I am writing an app with Xamarin.Android and MvvmCross. I am using fragments and have a "content container" design where I show the fragments in my content_frame view:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</FrameLayout>
Everything works, and I can navigate through my app by showing/closing the fragments in my content_frame, but when I close the last fragment, instead of closing the app, it shows a blank screen instead. I reckon I can fix this in my MvxAppCompatViewPresenter class but I don't know how? I currently don't have anything in my view presenter:
public class ViewPresenter : MvxAppCompatViewPresenter
{
public ViewPresenter(IEnumerable<Assembly> androidViewAssemblies) : base(androidViewAssemblies)
{
}
public override void Show(MvxViewModelRequest request)
{
base.Show(request);
}
public override void Close(IMvxViewModel viewModel)
{
base.Close(viewModel);
}
}
Here is the first fragment:
[MvxFragmentPresentation(typeof(LoginViewModel), Resource.Id.content_frame, true)]
[Register("myapp.droid.fragments.LoginSelectionFragment")]
public class LoginSelectionFragment : BaseFragment<LoginSelectionViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_login_selection;
}
One of the approaches you can use, is to not add the fragments that are starting fragments to the backstack by setting the MvxFragmentPresentation property AddToBackStack to false (false is also the default if no parameter is passed).
The idea there is that for the first fragment you would rely on the activity being added to the backstack. Essentially, the first fragment and the activity could then be considered the same with respect to the backstack, eliminating the blank screen.
However, this would only work if the starting fragments do not need to be added to the backstack, with in the same activity context, later in the navigational flow. In future versions of MvvmCross you would easily be able to overcome this limitation via the updated IMvxOverridePresentationAttribute.

MVVMCross Android button enable

I am new to MVVMCross framework for Android and having trouble on how to enable/disable a button. I wasn't able to find a documentation around this area.
Code ViewModel:
private bool _buttonEnabled;
public bool ButtonEnabled
{
get
{ return string.IsNullOrEmpty(EmailLogin);}
set
{
_buttonEnabled = value;
RaisePropertyChanged(() => ButtonEnabled);
}
}
Android axml:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Login"
android:background="#color/yellow"
local:MvxBind="Enabled(ButtonEnabled)"/>
But unable to get it to work. What I'm trying to achieve is that if EmailLogin variable is NullOrEmpty then button should be disabled. Where am i going wrong ?
Hopefully i'm not pushing this but can i add additional binding to it so that if it is disabled or enabled i can change the color of the button ?
You just need to change the binding syntax to this:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Login"
android:background="#color/yellow"
local:MvxBind="Enabled ButtonEnabled"/>
I don't know if you are doing it because your code doesn't show everything, but you will also need to call RaisePropertyChanged(() => ButtonEnabled); inside the setter of your EmailLogin property.
Also if you want to change the color of your button, you can install the official color plugin. Usage would be something like this:
private MvxColor _myColor;
public MvxColor MyColor
{
get
{ return _myColor; }
set
{
_myColor = value;
RaisePropertyChanged(() => MyColor);
}
}
And then: local:MvxBind="Enabled ButtonEnabled; TextColor MyColor"

EditText with multiple lines option accepts enter key as value android

This might be silly question ever but still couldn't find solution. I have EditText with multiple line option on press of enter key in softkey moves to next lines and when clicked on post button to post the content of edittext the value has 2 enter option[i.e 2 blank lines]. before posting i do check for
blank and null value but this doesn't prevent posting enter value.
if (!(story_write_text.getText().toString().equals(""))||!(story_write_text.getText().toString().equals(null)))
my question: how to check edittext value if there are only enter values so that i can prevent from posting blank enter value to server.
here's my edittext xml code:
<EditText
android:id="#+id/story_write_text"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text"
android:gravity="left"
android:hint="Write a comment"
android:inputType="textCapSentences"
android:padding="5dp"
android:textSize="15sp" />
just add android:inputType="textPersonName"
to the EditText it will stop it from pressing enter
If it not working then use below code
I would subclass the widget and override the key event handling in order to block the Enter key:
class MyTextView extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}

add layout to splashscreen

Is it possible to add a layout to the MvxSplashScreenActivity? I have overiden the OnViewModelSet like in all the other activities and placed the following code:
protected override void OnViewModelSet()
{
base.OnViewModelSet ();
SetContentView (Resource.Layout.SplashScreen);
}
The layout I am trying to load is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="#drawable/applogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:layout_centerInParent="true" /></RelativeLayout>
and I am getting the following exception:
Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type
Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextStack`1[[Cirrious.MvvmCross.Binding.Droid.BindingContext.IMvxAndroidBindingContext,
Cirrious.MvvmCross.Binding.Droid, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]
I cant seem to find anything online regarding the mvvmcross splash screen..
Ahy Ideas?
You can't use a databound layout within the splashscreen - the splashscreen is displayed before mvvmcross is fully started.
However, for a simple layout, you do pass a resource Id down to the base class constructor:
public class SplashScreen : MvxSplashScreenActivity
{
public SplashScreen()
: base(Resource.Layout.SplashScreen)
{
}
}
Further - to avoid black start screens - most people use a theme to specify a whole screen image in their splashscreen - see the 'standard' splash screen supplied by nuget - https://github.com/slodge/MvvmCross/blob/v3/nuspec/DroidContent/SplashScreen.cs.pp
Ensure Initialize Setup before calling OnCreate.
protected override void OnCreate(Bundle bundle)
{
var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(this);
setupSingleton.EnsureInitialized();
base.OnCreate(bundle);
}

Resources