Unknown Member Error - c#

I'm working on an application for the Android OS using Android Mono so I can program it in C#. All things are working well except for this one issue I am getting.
Basically, I have a sample code that changes the text of a button on every click. I tried to modify the sample code so that instead of changing the button, it changes the text of an xml textview object and, upon getting this error, I replaced it with an edittext object. I replaced it because it seemed common sense that editing an edittext would be easier than a textview.
Anywho, I got the same error at the same place. Whenever I try to change the textview.text or the edittext.text, the program freezes up and I get the error 'Unknown Member".
Here is the C# portion of the code:
Button ampButton = FindViewById<Button>(Resource.Id.CurrentButton);
ampButton.Click += delegate
{
SetContentView(Resource.Layout.AmpScreen);
Button ampButton2 = FindViewById<Button>(Resource.Id.CurrentButtonamp);
EditText ampData = FindViewById<EditText>(Resource.Id.ampdata);
ampButton2.Click += delegate
{
ampButton2.Text = string.Format("{0} clicks!", count2++);
ampData.Text = ampButton2.Text;
//string temp = Convert.ToString(count2) + " clicks!";
//ampData.Text=temp;
//count2++;
};
};
Here are the XML sections that are affected:
From the Main.xml:
<Button
android:id="#+id/CurrentButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Amp"
/>
From the AmpScreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView id="#+id/ampImage"
android:layout_width="50px"
android:layout_height="50px"
android:layout_gravity="center"
android:src="#drawable/amp"/>
<EditText id="#+id/ampdata"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading"/>
<Button
android:id="#+id/CurrentButtonamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Re-scan"
/>
<Button
android:id="#+id/homeButtonamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back"
/>
</LinearLayout>

Looks like a simple issue with your xml.
In your AmpScreen.xml on the EditText element:
id="#+id/ampdata"
should be:
android:id="#+id/ampdata"

Related

MaterialSpinner label/hint not showing when running the app

The materialspinner is not showing any hints or labels.
This is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://shcemas.android.com/apk/res-auto"
android:orientation="vertical"
android:minWidth="300dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fr.ganfra.materialspinner.MaterialSpinner
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/eventnameSpinner"
app:ms_enableFloatingLabel="true"
app:ms_floatingLabelText="Select Event"
app:ms_hint="Select Event"
app:ms_alignLabels="true"/>
<fr.ganfra.materialspinner.MaterialSpinner
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/eventpackageSpinner"
app:ms_enableFloatingLabel="true"
app:ms_floatingLabelText="Select Package"
app:ms_hint="Select Package"
app:ms_alignLabels="true"/>
</LinearLayout>
With the code
app:ms_floatingLabelText="Select Package"
app:ms_hint="Select Package"
it is supposed to show when running the app, instead it only shows the content inside it. It is supposed to show a hint "Select Event" and "Select Package" for both spinners but it did not, and when clicking the spinner, the label would hover on top same as the EditTexts. Is there something missing?
This is the layout
Are you using nuget Nivaes.Droid.Ganfra.MaterialSpinner,right?
I found that this nuget hasn't been updated for several years and is not recommended.
But you can use Xamarin.Android Spinner as an alternative.
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
Similarly, when setting property prompt, the spinner does not display the value of prompt.
But when we set property style="#android:style/Widget.Spinner" for spinner, if we set data resource for the Spinner and click the Spinner, the prompt will display in a dialog.
<Spinner
style="#android:style/Widget.Spinner"
android:tooltipText="abc"
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
Of course, you can also add a TextView to display the prompt,just as document Xamarin.Android Spinner says.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="#string/planet_prompt"
/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
Note:
You can check the following threads for more ideas:
How to make an Android Spinner with initial text "Select One"?
Spinner prompt not showing

Clickable Button in a custom listview

My problem is I can't make the button I added in my custom listview to be clickable.
Here is the XML file of my custom listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="335dp"
android:layout_height="100dp"
android:minHeight="80dp"
android:maxHeight="250dp"
android:background="#ffffff"
android:id="#+id/list"
android:baselineAligned="false"
android:longClickable="false"
android:descendantFocusability="blocksDescendants"
android:clickable="false">
<Button
android:background="#drawable/grbgCan"
android:layout_width="15dp"
android:layout_height="20dp"
android:id="#+id/deleteMemo"
android:layout_gravity="start"
android:layout_marginLeft="270dp"
android:layout_marginTop="5dp"
android:duplicateParentState="false"
android:clickable="true" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/id"
android:layout_marginLeft="290dp"
android:layout_marginTop="-20dp"
android:layout_marginRight="15dp"
android:clickable="true" />
<TextView
android:id="#+id/top"
android:text="Title"
android:textColor="#ff7e7777"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="255.5dp"
android:layout_height="19.9dp"
android:paddingLeft="10dp"
android:layout_marginTop="-20dp" />
<TextView
android:id="#+id/bottom"
android:textColor="#ff7e7777"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="63.3dp"
android:paddingLeft="50dp"
android:layout_gravity="left"
android:text="Note" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/date"
android:layout_marginLeft="250dp"
android:layout_marginRight="5dp" />
</LinearLayout>
In my main activity I tried the Btn.Click event but it only produces an error. And from what I've read in similar problems, their button was initialize in their Array Adapter but I'm using SimpleCursorAdapter because I'm fetching data from my sqlite database.
What should I do then? Please don't give me java codes because some them aren't applicable in xamarin studio since it's c#. Thanks.
You can't do it this way because you have a lot of same items in ListView with same ids
You must set OnClickListener to your button in your Adapter's getView() method like this
(this is java code for android studio, so it can differ from c# code, but gist is the same)
Button btn = (Button) convertView.findViewById(R.id.deleteMemo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here you go
}
});
Try something like this (If you don't have Fragments delete the view. below)
var _view = view.FindViewById<ListView>(Resource.Id.listView1);
if (_view.ChildCount > 0){
Button _button = (Button)_view.GetItemAtPosition(0).GetChildAt(0);
}
Because you have a lot of similar objects on a list this is the only way i think to access them,
Remember that item position is relative to the current visible items on the screen and you cant access items that are not visible

Monofor droid VS 2012 navigate to another page using button

Here is the problem right now.
create a simple axml file
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Hello" />
</LinearLayout>
and here is the register.axml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="#+id/registerID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="70dp"
android:hint="ID" />
</LinearLayout>
i searched a lot at this forum but what i found the code was in java.
and im using c# in VS2012 to do so.
and i searched a lot internet and failed to find for it.
can anyone give some idea ?
Just like with Java Android, Xamarin.Android uses both Activities and Fragments for displaying content and navigation.
The most simple sample would be that you create two Activities. One Main Activity, with you first layout, and a second one with your register.axml layout.
The button in the Main Activity, has a Click event, which you can do whatever you want, when it is clicked, i.e. start the second Activity. This can be done with an Intent, which Android uses i.e. for navigation:
var button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += (s, e) => {
var intent = new Intent(this, typeof(SecondActivity));
StartActivity(intent);
};
I recommend you to read the documentation Xamarin has, which goes through these basic things.

Display Modal window in Xamarin

I'm newbie in Xamarin. I'm creating a simple application with Xamarin. I have a table view in my layout. Each row of table view displays a modal window. For example the first row is used to get the full name of the user. I want to display the modal window shown (rightside) in the given figure.
I have created a Layout for the same as given below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:minWidth="25px"
android:minHeight="25px">
<EditText
android:inputType="textPersonName"
android:layout_width="200dp"
android:layout_height="55px"
android:id="#+id/txtFirstName"
android:ellipsize="none"
android:gravity="fill_horizontal"
android:hint="First Name"
android:height="55dp"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:textColor="#000000" />
<EditText
android:inputType="textPersonName"
android:layout_width="200dp"
android:layout_height="55px"
android:id="#+id/txtLastName"
android:ellipsize="none"
android:gravity="fill_horizontal"
android:hint="Last Name"
android:height="55dp"
android:layout_marginTop="25dp"
android:layout_gravity="center_horizontal"
android:textColor="#000000" />
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:minWidth="25px"
android:minHeight="25px"
android:layout_marginTop="25dp">
<Button
android:text="Save"
android:layout_width="100dp"
android:layout_height="55px"
android:id="#+id/btnSave"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:textColor="#000"
android:layout_marginLeft="165dp" />
</RelativeLayout>
</LinearLayout>
And my code to display popup
tblrName.Click += delegate
{
//Want to display the modal window here.
};
I tried to set the layout using setContentView() but it opens a new window while I want to display the modal window in same screen itself.
Can anybody help me to achieve this?
Use AlertDialog.
In your click event, try this:
var alert = new AlertDialog.Builder(this);
alert.SetView(LayoutInflater.Inflate(Resource.Layout.Modal, null));
alert.Create().Show();
To get values from AlertDialog, you could keep a reference to the EditText in the dialog. And then, in your Save button click event, simply call EditText.Text to get the value.

How do i add a Clear Text Field button to my android app in C#?

i'm very much a beginner with programming.. but i'm trying to develop a small android app using monodroid to store contacts in and call contacts from the address book.
Please forgive me if this is very simple but i have a text field and a button, both of which are in my Resources XML file and i want to be able to CLEAR the text in the textbox by clicking the button which will obviously be called 'Clear'.. all help is much appreciated as i'm looking forward to learning more.
Let's say you have the following layout, which I assume is similar to what you have already:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/TextField"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/Clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear"/>
</LinearLayout>
Then in your activity's code you can do:
var clear = FindViewById<Button>(Resource.Id.Clear);
var textField = FindViewById<TextView>(Resource.Id.TextField);
clear.Click += (src, args) =>
{
textField.Text = "";
};
how about this?
Button ClearText=(Button) findViewById(R.id.ClearText);
ClearText.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText textedit=(EditText) findViewById(R.id.textedit);
textedit.setText("");
}
});
clear must be registered as onclick handler for the button in the layout file like this
<ImageButton android:id="#+id/ClearText"
android:text="#string/ClearText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:onClick="clear"**
android:src="#drawable/clear"
/>

Categories