Android onDraw doesn't draw anything in custom EditText - android-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.

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>

Spinner onItemSelected to clear the EditText Text

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

Unstable of Custom Edit Field

I built custom edit field because i want to change the background color.
first, second and third shown unstable, sometime display when focusing, disappear when lost focusing.
I want the result like third image where the focusing at where also static display all the field.
Here is my Custom_EditField:
public class Custom_EditField extends EditField {
private int width, row, color;
private boolean isfocus;
Custom_EditField(long style, int width, int row) {
super(style);
this.width = width;
this.row = row;
}
public int getPreferredHeight() {
return Font.getDefault().getHeight() * row;
}
public int getPreferredWidth() {
return width;
}
protected void onFocus(int direction) {
color = Color.GRAY;
isfocus = true;
}
protected void onUnfocus() {
color = Color.GOLD;
isfocus = false;
}
protected void layout(int maxWidth, int maxHeight) {
super.layout(maxWidth,
Math.min(maxHeight, Font.getDefault().getHeight() * row));
super.setExtent(maxWidth,
Math.min(maxHeight, Font.getDefault().getHeight() * row));
}
protected void paint(Graphics graphics) {
int rectHeight = getPreferredHeight();
int rectWidth = getPreferredWidth();
try {
if (isfocus) {
graphics.setBackgroundColor(color);
} else {
graphics.setBackgroundColor(color);
}
color = Color.BLACK;
graphics.setColor(color);
graphics.drawRect(0, 0, rectWidth, rectHeight);
super.paint(graphics);
} finally {
graphics.setColor(color);
}
}
}
I'm not 100% sure which problem you're asking about:
how to draw your own focus background colors for an EditField, or
how to fix the problem with fields disappearing as you move focus
1) If it's the second problem (disappearing), then I would guess that you're having the same problem as in your other question, with the Custom_TopField buttons disappearing
So, if these Custom_EditField objects are created by a manager that extends VerticalFieldManager, but also implements sublayout() itself to perform all the positioning, then try the solution I suggested in the other question (don't extend VerticalFieldManager, just extend Manager).
2) If that doesn't work, it's possible that your paint() method is not getting triggered when it should. Try adding a call to invalidate() in your focus methods, along with a call to the superclass methods onFocus() and onUnfocus():
protected void onFocus(int direction) {
color = Color.GRAY;
isfocus = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
color = Color.GOLD;
isfocus = false;
invalidate();
super.onUnfocus();
}
3) And, you might need to implement this, too:
public boolean isFocusable() {
return true;
}
But, first, check your manager class, to be sure this isn't the same problem as in your other question.
Also ...
In this class, you are basing the height of the edit field on the row number. Is this really what you want? Row 0 would be 0 size, and each row after that would get taller and taller. Is that really the UI? Normally, you would have all the rows' edit fields be the same size, and simply lay them out with a different y offset. Most likely, this class should not need to know which row it has been placed on. That information is normally the responsibilty of a manager object.
protected EditField getEditField() {
EditField edit_field = new EditField("", "", 10, EditField.NO_NEWLINE
| BasicEditField.FIELD_VCENTER) {
protected boolean navigationClick(int status, int time) {
}
protected void onFocus(int direction) {
Set the boolean during focus
}
protected void onUnfocus() {
}
protected void paintBackground(Graphics graphics_paint) {
if(that boolean is true)
{
Set your custom background during focus.
}else{
Set your custom background during unfocus.
}
}
protected void paint(Graphics graphics_paint) {
int old_color = graphics_paint.getColor();
try {
if(that boolean is true)
{
Set your custom Font color during any event like focus and click.
}else{
Set your custom background Font color during any event like unfocus.
}
super.paint(graphics_paint);
} finally {
graphics_paint.setColor(old_color);
}
}
public void layout(int width, int height) {
}
};

Scrollable field with non-editable text

In my app I need a scrollable field with non-editable text with custom size. I'm trying to do this like below:
final int sx2 = Display.getWidth()-Display.getWidth()*2/51-kButtonWidgh;
final int sy2 = Display.getHeight()*7/12;
VerticalFieldManager vfm3 = new VerticalFieldManager()
{
public void sublayout( int maxWidth, int maxHeight )
{
super.sublayout(sx2, sy2);
}
};
text = new TextField((Field.FOCUSABLE & Field.READONLY & Field.STATUS_MOVE_FOCUS_HORIZONTALLY)){
public void layout( int maxWidth, int maxHeight ) {
super.layout(sx2, sy2); //my custom frame
}
};
vfm3.add(text);
vfm2.add(vfm3);
But it editable, and not scrollable. Text prints from beginning field and ends far away from it's frame. there is cursor, which goes out of field frame, and there is no scrolling. How should I do this?
try this:
EditField _text = new EditField(Field.HORIZONTAL_SCROLL|Field.VERTICAL_SCROLL);
_text.setEditable(false);

Blackberry EditField - height changing not effective

What I try to do is to simply change the height of an EditField.
This sounds pretty easy but I facing a problem that drives me crazy.
Here is my code:
public class CMainManager extends HorizontalFieldManager
{
EditField mtxt=new EditField(EditField.FOCUSABLE | EditField.FILTER_DEFAULT);
/* Constructeur */
public CMainManager()
{
add(mtxt);
}
public int getPreferredHeight()
{
return Display.getHeight();
}
public int getPreferredWidth()
{
return Display.getWidth();
}
protected void sublayout(int width, int height)
{
layoutChild(mtxt, 50, 500);
setPositionChild(mtxt, 0, 10);
mtxt.setBackground(
BackgroundFactory.createSolidBackground(0x000000FF));
setExtent(width, height);
}
}
The line layoutChild(mtxt, 50, 500); should set the width and height of my EditField. It does, but only with the width. The height is constant. If I set it to 500 I get the same result as if I set it to 10.
Any idea of what I'm missing ? because I'm sure this is a stupid error I'm doing...
Thanks
==edit
After jproffit help, I've understood what I did wrong. However now that it works (I can set the height) I have another problem. Here is me code:
private EditField m_Txt=new EditField(EditField.FOCUSABLE |
EditField.FILTER_DEFAULT) {
protected void layout(int width, int height)
{
setExtent(Display.getWidth(), m_TxtHeight);
}
public boolean isFocusable()
{
return true;
}
protected void onFocus(int direction)
{
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
};
With this code I cannot manage the focus correctly. What do I forget ? I cannot see the cursor in my field.
Any idea ?
Thanks
You aren't telling it the dimensions to take, you're specifying the maximum amount of space it can take up. So when you tell it 10, 50, or 500, if it only want to use a height of 8 that's all it will be. You'll have to override layout() on the EditField if you want to control its dimensions as well.
Try doing like this..
create a custom editfield and inside the layout method
_editField = new EditField("", initialValueOfTextField)
{
protected void layout(int maxWidth, int maxHeight)
{
int myWidth = getMaxSize() * getFont().getAdvance("W");// define your req width
int myHeight= 50 // your required height
super.layout(myWidth, myHeight);
}
};

Resources