Custom dialog collapsing when title is removed - c#

My app is supposed to have a button in the main activity that when pressed should show a custom dialog box that will be used as a register form for users. The problem is when I remove the title bar of my custom dialog window collapses horizontally. Any advise on how to fix this? Is the idea of using a fragment as a registration form a good one? TIA
Here is the image to Illustrate
Here is what it looks like with the title bar
Here is the code for the Main Activity
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mbtnfragment = FindViewById<Button>(Resource.Id.MyButton);
mbtnfragment.Click += mbtnfragment_Click;
}
void mbtnfragment_Click(object sender, EventArgs e)
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
create_dialog dialog = new create_dialog();
dialog.Show(transaction, "Fragment Dialog");
Console.WriteLine("Fragment created");
}
Code for the create_dialog class
class create_dialog: DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState)
{
base.OnCreateView(inflater, container, savedInstancesState);
var view = inflater.Inflate(Resource.Layout.fragment_dialog, container, false);
return view;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
base.OnActivityCreated(savedInstanceState);
}
}
}

change the fragment_dialog layout's width from wrap_content to match_parent or a defined size

You can set a style without title to your custom dialog
#Override
public void onCreateDialog(Bundle savedInstanceState) {
setStyle(STYLE_NO_TITLE, 0);
}
Or you can set a theme in the second parameter like Theme.AppCompat.Dialog...

Related

Xamarin Android - Access textview onclick within a Fragment

I am trying to access a TextView that is part of a Fragment, in a seperate Click function.
public class FragmentHome : AndroidX.Fragment.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragment_home, container, false);
// Initialise View's Textviews & TextInput
TextView genderButton= view.FindViewById<TextView>(Resource.Id.genderButton);
TextView secondButton = view.FindViewById<TextView>(Resource.Id.secondButton);
TextInputEditText firstInput = view.FindViewById<TextInputEditText>(Resource.Id.firstInput);
// set up click actions
genderButton.Click += GenderButton_Click;
return view;
}
private void GenderButton_Click(object sender, System.EventArgs e)
{
if (genderButton.Text == "Male")
{
genderButton.Text = "Female";
}
else
{
genderButton.Text = "Male";
}
}
}
I am trying to access the Text of the genderButton, but I get the error "The name 'genderButton' does not exist in the current context". I have tried adding both
TextView genderButton = FindViewById<TextView>(Resource.Id.genderButton);
and
View view = inflater.Inflate(Resource.Layout.fragment_home, container, false); TextView genderButton = view.FindViewById<TextView>(Resource.Id.genderButton);
But the first one has the problem that FindViewById doesn't exist in the current context, and the second option obviously won't work because it would be inflating a new fragment, instead of defining the context for genderButton.
Is there a way where that I can properly define the context for genderButton or send the genderButton data with the Click event?
this is a basic C# scope problem. You are declaring genderButton inside of OnCreateView, so it only has scope inside that method. If you want to use it elsewhere in the code, you need to declare it at the class level, not inside of a method
TextView genderButton;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragment_home, container, false);
// Initialise View's Textviews & TextInput
genderButton= view.FindViewById<TextView>(Resource.Id.genderButton);
TextView secondButton = view.FindViewById<TextView>(Resource.Id.secondButton);
TextInputEditText firstInput = view.FindViewById<TextInputEditText>(Resource.Id.firstInput);
// set up click actions
genderButton.Click += GenderButton_Click;
return view;
}

How to capture an image in xamarin android using the camera in fragment

I am a beginner in xamarin android.I want to take photo using my device camera,I am using the following code to to capture the image but, I got an exception when running the code
Here is my Fragment.CS
public class ScanFragments : Fragment
{
ImageView imageView;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static ScanFragments NewInstance()
{
var scan = new ScanFragments { Arguments = new Bundle() };
return scan;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
imageView =View.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =View. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
return inflater.Inflate(Resource.Layout.ScanLayout, null);
}
private void BtnCamera_Click(object sender, EventArgs e)
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
}
}
Well now I see your mistake its actually very simple you get the null reference because of the following reasons
1.OnCreateView does not contain a definition of View basically what I mean here is View is a class which you are using as an object of that class.
2.The actual way I recommend using fragments is first you define it an XML or an AXML as its foreground view in on OnCreateView as follows :
return inflater.Inflate(Resource.Layout.ScanLayout, null);
3.Override the OnViewCreated method and use its View to do all your work as follows :
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
imageView =view.FindViewById<ImageView>(Resource.Id.Iv_ScanImg);
var btnCamera =view. FindViewById<Button>(Resource.Id.btnCamera);
btnCamera.Click += BtnCamera_Click;
}
4.For details related to the Android Activity and Fragment lifecycle check here
Goodluck!

The name "getView" does not exist in the current context

I am building an Android application that uses fragments to display different views. The first tab contains the code below. Unfortunately, I get this error:
The name 'getView' does not exist in the current context
I also tried placing the getView() in onActivityCreated, rather than onViewCreated, but I received the same error. Why is getView not being recognized?
namespace BottomToolbar
{
public class FirstFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static FirstFragment NewInstance()
{
var fragment = new FirstFragment { Arguments = new Bundle() };
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.FirstFragment, container, false);
return view;
}
public override void OnViewCreated(View view, Bundle savedInstanceState)
{
Button button = (Button)getView().findViewById(Resource.Id.getWeatherButton);
}
}
}
Change
Button button = (Button)getView().findViewById(Resource.Id.getWeatherButton);
to
Button button = View.FindViewById<Button>(Resource.Id.getWeatherButton);
Try this:
Public override void OnViewCreated(View view, Bundle savedInstanceState) {
Button button = (Button)view.getView().findViewById(Resource.Id.getWeatherButton); }

Create Fragment Dynamically Xamarin android

How to create Fragment Dynamically and show data in fragment when Main.axml called in OnCreate method in android Xamarin.
Please Help
Thanks in advance
You can dynamically create a FrameLayout for your fragment. For example:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Main);
LinearLayout root = FindViewById<LinearLayout>(Resource.Id.rootlayout);
FrameLayout frame = new FrameLayout(this);
frame.Id = 10001000;
frame.LayoutParameters = new ViewGroup.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
root.AddView(frame);
if (savedInstanceState == null)
{
Fragment fragment = new MyFragment();
FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.Add(frame.Id, fragment).Commit();
}
}
Of course we need also create our custom Fragment for example like this:
public class MyFragment : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Create your view and add your data here. Return this view.
//For example:
TextView tv = new EditText(this.Activity);
tv.Text = "This is Fragment";
return tv;
}
}

Access Fragment Element from calling Activity

Is it even possible to access Views inside of a Fragment from the calling Activity. Or do i have to put all the needed Information into the Bundle when calling NewInstance or by using a static Method inside of my Fragment
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ActivityNavigationLayout);
var button = FindViewById<Button>(Resource.Id.Button);
}
private void SetUpFragmentManager()
{
var frag = OverViewFragment.NewInstance();
var fm = this.FragmentManager.BeginTransaction();
fm.Replace(Resource.Id.content_frame, frag);
fm.Commit();
}
When Resource.Id.Button is part of Fragment inflated inside that Activity?
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Main, container, false);
return rootView;
}

Categories