Cross Button in EditText Android
Cross Button in EditText Android for deleting clearing text in EditText Example source code:
Example allow delete text in EditText by cross button, or do Button click performance.
main.xml type your package name and class
Put into drawable folder cross and ok image.
CustomEditText.java
YourActivity.java
//android.okhelp.cz/wiktionary-aplikace-pro-android/
Example allow delete text in EditText by cross button, or do Button click performance.
main.xml type your package name and class
Put into drawable folder cross and ok image.
<cz.okhelp.wiktionary.CustomEditText
android:id="@+id/editTextZadejSlovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="TypeAndPressGreen"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:drawableLeft="@drawable/cross"
android:drawableRight="@drawable/ok" />
<!--button is invisible 0 height 0 width for performance click on button in EditText-->
<Button
android:id="@+id/btnGO"
android:layout_width="0sp"
android:layout_height="0sp"
android:layout_weight="0"
android:text="GO" />
CustomEditText.java
package cz.okhelp.wiktionary; // your package name
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.EditText;
public class CustomEditText extends EditText
{
private Drawable dLeft,dRight;
private Rect lBounds,rBounds;
private static Button btnOk;
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top,
Drawable right, Drawable bottom)
{
if(left !=null) {
dLeft = left;
}
if(right !=null){
dRight = right;
}
super.setCompoundDrawables(left, top, right, bottom);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
btnOk.requestFocus();
btnOk.performClick();
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
final int x = (int)event.getX();
final int y = (int)event.getY();
if(event.getAction() == MotionEvent.ACTION_UP && dLeft!=null) {
lBounds = dLeft.getBounds();
int n1 = this.getLeft();
int n2 = this.getLeft()+lBounds.width();
int n3 = this.getPaddingTop();
int n4 = this.getHeight()-this.getPaddingBottom();
// leva strana
if( x>=(this.getLeft())
&& x<=(this.getLeft()+lBounds.width())
&& y>=this.getPaddingTop()
&& y<=(this.getHeight()-this.getPaddingBottom()))
{
this.setText("");
event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
}
}
if(event.getAction() == MotionEvent.ACTION_UP && dRight!=null)
{
rBounds = dRight.getBounds();
int n1 = this.getRight()-rBounds.width();
int n2 = this.getRight()-this.getPaddingRight();
int n3 = this.getPaddingTop();
int n4 = this.getHeight()-this.getPaddingBottom();
// prava strana
if(x>=(this.getRight()-rBounds.width()) && x<=(this.getRight()-this.getPaddingRight())
&& y>=this.getPaddingTop() && y<=(this.getHeight()-this.getPaddingBottom()))
{
btnOk.requestFocus();
btnOk.performClick();
event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
}
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable
{
dRight = null;
rBounds = null;
super.finalize();
}
public void setBtnOk(Button btnOk) {
this.btnOk = btnOk;
}
public Button getBtnOk() {
return btnOk;
}
}
YourActivity.java
//onCreate
Button mBtnGO = (Button)findViewById(R.id.btnGO);
CustomEditText mEditZadani = (CustomEditText)this.findViewById(R.id.editTextZadejSlovo);
mEditZadani.setBtnOk(mBtnGO);
mBtnGO.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff for signInButtonClick
}
});
//android.okhelp.cz/wiktionary-aplikace-pro-android/
396LW NO topic_id
AD
Další témata ....(Topics)
if , else if, else statement with string as a switch statement in Java example source code.
MainClass.java
MainClass.java
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = {"nothing", "Hello", "people"
, "bye-bye", "hello", "world!", "end" };
for (int i = 0; i < arrayOfString.length; i++) {
if (arrayOfString[i].equals("Hello"))
System.out.println(arrayOfString[i]);
else if (arrayOfString[i].equals("people"))
System.out.println(arrayOfString[i]);
else if (arrayOfString[i].equals("hello"))
System.out.println(arrayOfString[i]);
else if (arrayOfString[i].equals("world!"))
System.out.println(arrayOfString[i]);
else // default
System.out.println("Default");
}
}
}
/*
Default
Hello
people
Default
hello
world!
Default
*/
Terms Screen size, density, density independent pixel, resolution as a picture - pictogram.
Test your knowledge
Q: How to find out the phone screen size?
A: (By length of display diagonale in inch - Not to measure a diagonal of device!!!)
Q: What resolution has 720 x 1280 display?
A: (921600 pixels)
Q: What does it mean "240 dpi" screen density?
A: (Display have density 240 x 240 dots - "Tri-color LED etc." - per every physical (real) square inch. If you have icon 240x240 pixels, this will just occupy an area of one square inch on the display.)
Q: Phone have screen density 240 dpi. Image for 160 dpi screen density have size 128x 128 pixels. What will be the size of the image for 240 dpi screen density?
A: (Calculate the virtual pixels size. 128 * (240/160) = 192. You have to resize image to new size 192 x 192 physical pixels and put into folder drawable-hdpi (high) ~240dpi for phone with screen density 240 dpi. ) or use density independend pixels 128dp x 128dp.
Test your knowledge
Q: How to find out the phone screen size?
A: (By length of display diagonale in inch - Not to measure a diagonal of device!!!)
Q: What resolution has 720 x 1280 display?
A: (921600 pixels)
Q: What does it mean "240 dpi" screen density?
A: (Display have density 240 x 240 dots - "Tri-color LED etc." - per every physical (real) square inch. If you have icon 240x240 pixels, this will just occupy an area of one square inch on the display.)
Q: Phone have screen density 240 dpi. Image for 160 dpi screen density have size 128x 128 pixels. What will be the size of the image for 240 dpi screen density?
A: (Calculate the virtual pixels size. 128 * (240/160) = 192. You have to resize image to new size 192 x 192 physical pixels and put into folder drawable-hdpi (high) ~240dpi for phone with screen density 240 dpi. ) or use density independend pixels 128dp x 128dp.
SeekBar setOnSeekBarChangeListener Example. Change TextView font size by SeekBar Example.
TextView mTextView01 = (TextView)findViewById(R.id.textView01);
SeekBar mSeekBarTexSize = (SeekBar)findViewById(R.id.seekBarTextSize);
mSeekBarTexSize.setMax(100);
mSeekBarTexSize.setProgress(25);
mSeekBarTexSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mTextView01.setTextSize((float)progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
RadioButton RadioGroup Android example source code for Android developer
Example for *.xml files
Example for *.java files
Example for *.xml files
<RadioGroup android:id="@+id/idRadio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<RadioButton android:id="@+id/idRadio_1"
android:text="@string/textLabel_1"/>
<RadioButton android:id="@+id/idRadio_2"
android:text="@string/textLabel_2"/>
<RadioButton android:id="@+id/idRadio_3"
android:text="@string/textLabel_3"/>
</RadioGroup>
Example for *.java files
// import
import android.widget.RadioGroup;
// get handle of RadioGroup
RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.idRadio_group);
// which RadioButton is selected for example in some function body
int nUnits = 10; // decimetry
int nIdRadio = mRadioGroup.getCheckedRadioButtonId();
if(nIdRadio == R.id.idRadio_1) nUnits = 1; // metr
else if(nIdRadio == R.id.idRadio_2) nUnits = 10; // decimetr
else if(nIdRadio == R.id.idRadio_3) nUnits = 100; // cm
else if(nIdRadio == R.id.idRadio_4) nUnits = 1000; // mm
// listener for RadioGroup Java Android example
mRadioGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,
int checkedId) {
Log.v("Selected", "New radio item selected: " + checkedId);
recordNewUIState();
}
});
Editace: 2013-12-09 13:02:09
Počet článků v kategorii: 396
Url:cross-button-in-edittext-android