Spinner onItemSelected to clear the EditText Text - android-edittext

I have a spinner on one of my Activity that on initialization will load the data from database for spinner.setSelection(0). When I change the spinner value, lets say spinner.setSelection(1), I want to clear the text from EdittText ed1 View prior to loading the data(if exist) or leave it blank/null/empty
Here is the snippet of my code.
public class TestActivity extends Activity implements
AdapterView.OnItemSelectedListener {
...
Spinner spinner;
EditText ed1;
...
protected void onCreate(Bundle savedInstanceState) {
...
...
spinner = (Spinner) findViewById(R.id.spinner1);
ed1 = (EditText)findViewById(R.id.ed1);
...
adapter = new ArrayAdapter<String>(this,R.layout.temp_layout01,R.id.temptxt01,xNames);
spinner.setAdapter(adapter);
spinner.setSelection(getSpinnerIndex(spinner,mname));
spinner.setOnItemSelectedListener(this);
}
Method to clear the EditText data
private void clearAllFields() {
Log.i(TAG, "Clearing Fields");
ed1.getText().clear(); //test that didn't clear the data
ed1.setText(null); // test that didn't clear the data
}
onItemSelected for spinner listener
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clearAllFields();
mname = parent.getItemAtPosition(position).toString();
mID = dbHelper.getCurrentData(mname);
spinner.setSelection(getSpinnerIndex(spinner,mname));
populateData();
}
The ed1 is within a TableRow
<TableRow
android:id="#+id/tbHole18TR"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/ed1"
android:background="#color/Green3"
android:textAlignment="center" />
</TableRow>
Is this something to do with focusChange etc.

Try this logic
get current spinner data and EditText data
compare them if find not equal clear else do nothing

Related

How to make 2 layouts sliding on button click in xamarin/android?

I'm begining with xamarin and android, and I'm not sure to understand the good way to do.
Then I created a main activity and a second activity with their respective layouts.
In the first I put a button with that code:
button.Click += (sender, e) =>
{
var intent = new Android.Content.Intent(this, typeof(Activity1));
StartActivity(intent);
OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft);
};
And in the second:
button.Click += (sender, e) =>
{
Finish();
};
But this doesn't do what I want, I would like the two layouts two slide as if there were side by side, but here the second layout just slides on the first. Also it comes from the left were I would like it to come from the right and there's no SlideInright.
Also is it the right way to do using two activities, mustn't I have only one activity and two layouts (views?).
First, you could use one activity which contains two ViewGroup. As for your question, you can use Animation and FrameLayout which contains two RelativeLayout to complete what you want, like this:
MainActivity.axml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:a="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl2"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/holo_green_dark">
<Button
android:id="#+id/bt2"
android:text="page2"
android:layout_height="100dp"
android:layout_width="100dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/holo_red_dark">
<Button
android:id="#+id/bt1"
android:text="page1"
android:layout_height="100dp"
android:layout_width="100dp" />
</RelativeLayout>
</FrameLayout>
MainActivity:
[Activity(Label = "Aniamtion", MainLauncher = true)]
public class MainActivity : Activity
{
Button bt1,bt2;
RelativeLayout rl1, rl2;
int width;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
getScreenWidth();//get the screen's width, so we can define how much the animation can translate
initView();
initListener();
}
private void getScreenWidth()
{
DisplayMetrics dm = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(dm);
width = dm.WidthPixels;
}
private void initListener()
{
bt1.Click += toPage1;
bt2.Click += toPage2;
}
private void toPage1(object sender, EventArgs e)
{
//use ObjectAnimator to complete it
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", 0, -width);
objectAnimator.SetDuration(500);
objectAnimator.Start();
ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", width, 0);
objectAnimator1.SetDuration(500);
objectAnimator1.Start();
}
private void toPage2(object sender, EventArgs e)
{
ObjectAnimator objectAnimator = ObjectAnimator.OfFloat(rl1, "translationX", -width, 0);
objectAnimator.SetDuration(500);
objectAnimator.Start();
ObjectAnimator objectAnimator1 = ObjectAnimator.OfFloat(rl2, "translationX", 0, width);
objectAnimator1.SetDuration(500);
objectAnimator1.Start();
}
private void initView()
{
bt1 = FindViewById<Button>(Resource.Id.bt1);
bt2 = FindViewById<Button>(Resource.Id.bt2);
rl1 = FindViewById<RelativeLayout>(Resource.Id.rl1);
rl2 = FindViewById<RelativeLayout>(Resource.Id.rl2);
}
}
Second, about this OverridePendingTransition(Android.Resource.Animation.SlideInleft, Android.Resource.Animation.SlideInleft) method, the parameter you can use System resource, or you can custom it .
Update:
put this into your Resource->anim->SlideInRight, this is about customing animation in Xamarin.Android, it is same as Android, and this is document which you can refer to
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<translate android:fromXDelta="50%p"
android:toXDelta="0"
android:duration="300"/>
</set>

Android onDraw doesn't draw anything in custom EditText

similar problems
I have a Custom EditText:
public class MyEditText extends AppCompatEditText {
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, getPaint());
}
}
in xml:
<com.angcyo.myapplication.MyEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
Normal effect:
effect
But If you Simultaneous setting
android:gravity="right" android:inputType="text"
like this:
<com.angcyo.myapplication.MyEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:gravity="right"
android:inputType="text"/>
Well, the result is...
result
What happened? doesn't draw anything?
Try to extend EditText rather than AppCompatEditText.
And change getmeasuredHeight() and getmeasuredWidth() to getHeight() and getWidth() then see.
Initialize Paint like
Paint paint =new Paint(); paint.setColor(Color.BLACK);
and change the radius to 50f or 60f in third parameter and then see.

Can't load AR experience in wikitude

I need to create AR app to do as in this link:
http://www.wikitude.com/external/doc/documentation/latest/android/imagerecognition.html#multipletarget
I wrote a code and tried it, it open a camera, but I think it can't load to use AR experience (index.html)
This is a MainActivity.java code:
NOTE: the path of index.html in th project is: assets/miarec/index.hmtl
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.wikitude.architect.ArchitectView;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
ArchitectView architectView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.architectView = (ArchitectView)this.findViewById( R.id.architectView );
final ArchitectView.ArchitectConfig config = new ArchitectView.ArchitectConfig( "3Axht4JgkVsnCHOuwl76PM0NDRnpRNL5PwwrPd1kgIN7aAP5sGJxBp3hvA3SIE+zrzwaqvulTUb7hhYz1/iQJiY9M3K8825GHJkssi5nHP8ZhtdoIo0z2fV2PkGUY9DzmVG/z2f3Uu8FKyulXFFegYEQZcvSCVIYvBmI6y0MIAFTYWx0ZWRfX6IIZxx4haqZxXDO5sj4bWkWKn2INx38uRuDKzaF6TVj7NV2MpeQeOvWWWGx7TKLL8DfOO8W4ARCSicBw6Vmejq7WMFQuMvte1y+r6rxZ2irjTBd8IpiAezUX8cxpy26SNbd1Gm+PWdjizE3Yqqr3/mi5NcDWlGm3GICiCeFvDNFgwryW7kPG6JTjhWA3GiUTfUE7AAJI349ZbtisyhP8YLzRvPUIc8t9nKs0aEMCme3e3CNiacl7rAuUWI7YNYD28GQ9EpTwAL8F29tV1n3dcXhxyuKfyptft71DI1SVVQ67/3UNml6bA06R3ZZiwyYD1f9l/Kfz5Q6RaFGXBL6vKHKwSt0vfeul2lmB4N1FJ+6GCSsB8cvIyhq3QtMQhbG9CiihTlsF9WVQdIVtbXFuGo=" /* license key */ );
this.architectView.onCreate( config );
}
#Override
protected void onPostCreate( final Bundle savedInstanceState ) {
super.onPostCreate( savedInstanceState );
if ( this.architectView != null ) {
// call mandatory live-cycle method of architectView
this.architectView.onPostCreate();
try {
// load content via url in architectView, ensure '<script src="architect://architect.js"></script>' is part of this HTML file, have a look at wikitude.com's developer section for API references
String s= getAssets().open("miarec/index.html").toString();
this.architectView.load(getAssets().open("miarec/index.html").toString());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
this.architectView.onResume();
}
#Override
protected void onPause() {
super.onPause();
// call mandatory live-cycle method of architectView
if ( this.architectView != null ) {
this.architectView.onPause();
}
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
// call mandatory live-cycle method of architectView
if ( this.architectView != null ) {
this.architectView.onDestroy();
}
}
#Override
public void onLowMemory() {
super.onLowMemory();
if ( this.architectView != null ) {
this.architectView.onLowMemory();
}
}
}
This is a activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.wikitude.architect.ArchitectView android:id="#+id/architectView"
android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</RelativeLayout>
I get this warning in android studio xml preview:
Rendering Problems The following classes could not be instantiated:
- com.wikitude.architect.ArchitectView (Open Class, Show Exception)
Tip: Use View.isInEditMode() in your custom views to skip code or show
sample data when shown in the IDE Exception Details
java.lang.NullPointerException   at
com.wikitude.architect.ArchitectView.e  at
com.wikitude.architect.ArchitectView.a  at
com.wikitude.architect.ArchitectView.<init>  at
com.wikitude.architect.ArchitectView.<init>  at
Thank you very much
Please have a look at the provided Sample application, which also uses relative paths within the assets folder.
Calling architectView.load("index.html") will load file from application's assets folder and architectView.load("yourpath/index.html") the index html-file in yourpath, relative to the assets root directory, no need to use absolute assets-directory.
There may even be an error in your JS code. Have you tried loading it from assets root folder?
Please first try loading a valid sample application and then replace it with your own source code.
Also have a look at Android remote dubugging to find potential JS errors.
Kind regards,
Andreas
PS.: Please avoid posting same question to different forums, you already asked this question it in Wikitude Forum.

How to make Numeric Edit Text default to blank and then use getText

Please Help!I have Edit Text field whose inputType is number. On the click of Submit button in the firstActivity I want to send the value via Bundle to next activity. I want to make the field compulsory. Before sending the value I am checking if the EditText is blank. if its blank I want to give a Toast message that "Please enter number". I am using below code line to get the numeric text
editTextValue= (EditText) findViewById(R.id.edit_attendance);
editTextValue.setText("0");
var1 = Integer.valueOf((editTextValue.getText().toString()));
For this code to work I have to first set the default value to 0
But due to this when user is entering value in this EditText, he has to first erase 0 and then enter number. I dont want to set it to 0 value. Is there any other way to make this work.
Below is my Code in XML:
<EditText
android:id="#+id/edit_1"
android:layout_marginLeft="5sp"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="0"
android:textColor="#android:color/white" />
FirstActivity.java
onCreate(){
editTextValue = (EditText) findViewById(R.id.edit_1);
editTextValue.setText("0");
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
var1 = Integer.valueOf((editTextValue.getText()
.toString()));
if (var1 == 0) // when no value is entered
Toast.makeText(getApplicationContext(),
"Please enter value",
Toast.LENGTH_SHORT).show();
else
{
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
dataBundle.putInt(SecondActivity.VAR1,var1);
i.putExtras(dataBundle);
startActivity(i);
}
}
The below code worked for me:
// editTextValue.setText("0");
editTextValue.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE) {
var1 = Integer.valueOf((editTextValue
.getText().toString()));
}
return false;
}
});

Why is my WPF binding not working in state change?

I have some items in a WrapPanel. I want to be able to click on an item and have it expand to the full width of the wrap panel. I tried doing this by creating two states, Expanded and Colapsed, in the control that is used for each item. For the Expanded state, I bound the Width of the control to be equal to the ActualWidth of the WrapPanel.
When I didn't get the result I expected, I tried setting Expanded value to a specific number (instead of the the binding). That is working. The items toggle between the two Colapsed and Exapanded widths. I still want to have the Expanded state be equal to the width of the WrapPanel though, not an arbitrary fixed width. I know my binding works because if I just bind the Width property directly (not via visual states), the items in the WrapPanel match its width.
Expanded state with Binding - Doesn't work:
<VisualState x:Name="Expanded">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="JobMaster">
<EasingDoubleKeyFrame KeyTime="0">
<EasingDoubleKeyFrame.Value>
<Binding
Path="ActualWidth"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type WrapPanel}}" />
</EasingDoubleKeyFrame.Value>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
Expanded State with hard coded value - Works
<VisualState x:Name="Expanded">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="JobMaster">
<EasingDoubleKeyFrame KeyTime="0" Value="800" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
Bind the control Width property directly works
<UserControl.Width>
<Binding
Path="ActualWidth"
RelativeSource="{RelativeSource AncestorType={x:Type WrapPanel}}" />
</UserControl.Width>
So why doesn't the Binding in the state work or is there another way to do this?
I was never able to get this working using visual states. Instead I wrote a behavoir.
public class TileExpandColapseBehavoir : Behavior<Control>
{
private ITile _data;
#region Properties
public static readonly DependencyProperty TileControlProperty = DependencyProperty.Register("TileControl", typeof(object), typeof(TileExpandColapseBehavoir), new PropertyMetadata(null));
public static readonly DependencyProperty DefaultWidthProperty = DependencyProperty.Register("DefaultWidth", typeof(Double), typeof(TileExpandColapseBehavoir), new PropertyMetadata(null));
public object TileControl
{
get { return (object)this.GetValue(TileControlProperty); }
set { this.SetValue(TileControlProperty, value); }
}
public double DefaultWidth
{
get { return (double)this.GetValue(DefaultWidthProperty); }
set { this.SetValue(DefaultWidthProperty, value); }
}
#endregion
public TileExpandColapseBehavoir()
{
}
protected override void OnAttached()
{
this.AssociatedObject.PreviewMouseDown +=new MouseButtonEventHandler(AssociatedObject_MouseUp);
}
private void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e)
{
UIElement child = (UIElement)sender;
WrapPanel parentWrap = FindAncestorUtil.TryFindAcestor<WrapPanel>(child);
if (parentWrap != null && TileControl is UserControl)
{
GetData();
if (_data.IsExpanded == false)
{
Binding newBinding = new Binding();
newBinding.Source = parentWrap;
newBinding.Path = new PropertyPath("ActualWidth");
UserControl thisTile = (UserControl)TileControl;
BindingOperations.SetBinding(thisTile, UserControl.WidthProperty, newBinding);
_data.IsExpanded = true;
}
else
{
UserControl thisTile = (UserControl)TileControl;
BindingOperations.ClearBinding(thisTile, UserControl.WidthProperty);
thisTile.Width = DefaultWidth;
_data.IsExpanded = false;
}
}
}
private void GetData()
{
if (_data == null && AssociatedObject.DataContext is ITile)
{
_data = (ITile)AssociatedObject.DataContext;
}
}
}
Your RelativeSource binding is looking for an ancestor of the animation, not the target of the animation. Try giving your WrapPanel a name and use Element binding instead.
<Binding Path="ActualWidth" ElementName="MyWrapPanel"/>

Resources