I need to introduce data in an EditText but i want to use an virtual keyboard, not the android keyboard. If I use setKeyListener(null) the cursor is invisible even after using setCursorVisible(true).
Is it possible to make an EditText where even if it isn't editable the cursor is visible ?
EDIT 2 :
I found an partial method to do that, but it's not working when i'm double taping the EditText.
I made an setOnClickListner() and an setOnLongClickListner() method for the EditText. In this methods I hide the Soft Input from the Window, also i use setTextIsSelectable(false). My only problem is that when I double tap the EditText the soft input keyboard shows and I dont know how to hide it, I tried to use android:windowSoftInputMode="stateAlwaysHidden" in manifest, but it doesn't work either.
EDIT :
Here is the code that I'm using at this moment for my base converter calculator.
public class MainActivity extends AppCompatActivity {
EditText number;
EditText base;
boolean baseB = false;
String numberS = "0";
String baseS = "10";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
//make the EditText for number and base not editable
number = (EditText) findViewById(R.id.number);
number.setKeyListener(null);
base = (EditText) findViewById(R.id.base);
base.setKeyListener(null);
//... more code here (changing fonts for each EditText and changing status bar color
}
// I have a function for each button all are the same
public void onClickBaseChange(View v) {
if (baseB) {
baseB = false;
// i use toasts at this moment to know when i'm on number or base field
Toast.makeText(this, "Number", Toast.LENGTH_SHORT).show();
} else {
baseB = true;
Toast.makeText(this, "Base", Toast.LENGTH_SHORT).show();
}
}
public void onClickB0(View v) {
if (numberS.length() > 0 && !numberS.equals("0") && !baseB) {
numberS += "0";
number = (EditText) findViewById(R.id.number);
number.setText(numberS, TextView.BufferType.EDITABLE);
number.setSelection(numberS.length());
} else {
if (Integer.valueOf(baseS) >= 1) {
baseS += "0";
base = (EditText) findViewById(R.id.base);
base.setText(baseS, TextView.BufferType.EDITABLE);
}
}
}
public void onClickB1(View v) {
if (numberS.equals("0")) {
numberS = "1";
} else {
numberS += "1";
}
number = (EditText) findViewById(R.id.number);
number.setText(numberS, TextView.BufferType.EDITABLE);
number.requestFocus();
number.setSelection(numberS.length());
}
And the xml looks like this :
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorBackground"
tools:context="manastur.calculator.MainActivity">
<EditText
android:id="#+id/base"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="120dp"
android:background="#android:color/transparent"
android:cursorVisible="true"
android:text=""
android:textColor="#color/text"
android:textSize="30dp" />
<EditText
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#android:color/transparent"
android:cursorVisible="true"
android:text=""
android:textColor="#color/text"
android:textSize="50dp" />
<LinearLayout
android:id="#+id/secondRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/firstRow"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/b1"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/b1"
android:onClick="onClickB1" />
<Button
android:id="#+id/b2"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/b2"
android:onClick="onClickB2" />
<!-- from this point on is the same, there are 5 LinearLayouts which
represents the 5 rows of button of the num pad -->
Use this code to achieve that,
While develop I took reference from native Dialpad code
KeypadlessKeypad.java
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.MotionEventCompat;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class KeypadlessKeypad extends EditText {
private static final Method mShowSoftInputOnFocus = getSetShowSoftInputOnFocusMethod(
EditText.class, "setShowSoftInputOnFocus", boolean.class);
public static Method getSetShowSoftInputOnFocusMethod(Class<?> cls, String methodName, Class<?>... parametersType) {
Class<?> sCls = cls.getSuperclass();
while (sCls != Object.class) {
try {
return sCls.getDeclaredMethod(methodName, parametersType);
} catch (NoSuchMethodException e) {
// Just super it again
}
sCls = sCls.getSuperclass();
}
return null;
}
private Context mContext;
/**
* Listener for Copy, Cut and Paste event
* Currently callback only for Paste event is implemented
*/
private OnEditTextActionListener mOnEditTextActionListener;
public KeypadlessKeypad(Context context) {
super(context);
mContext = context;
init();
}
public KeypadlessKeypad(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public KeypadlessKeypad(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
}
public final void appendText(CharSequence text) {
append(text, 0, text.length());
}
/***
* Initialize all the necessary components of TextView.
*/
private void init() {
setSingleLine(true);
synchronized (this) {
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
setFocusableInTouchMode(true);
}
reflexSetShowSoftInputOnFocus(false); // Workaround.
// Ensure that cursor is at the end of the input box when initialized. Without this, the
// cursor may be at index 0 when there is text added via layout XML.
setSelection(getText().length());
}
#Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
hideKeyboard();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
final boolean ret = super.onTouchEvent(event);
// Must be done after super.onTouchEvent()
hideKeyboard();
return ret;
}
private void hideKeyboard() {
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
}
}
private void reflexSetShowSoftInputOnFocus(boolean show) {
if (mShowSoftInputOnFocus != null) {
invokeMethod(mShowSoftInputOnFocus, this, show);
} else {
// Use fallback method. Not tested.
hideKeyboard();
}
}
public static Object invokeMethod(Method method, Object receiver, Object... args) {
try {
return method.invoke(receiver, args);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
return null;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int textViewWidth = View.MeasureSpec.getSize(widthMeasureSpec);
int height = getMeasuredHeight();
this.setMeasuredDimension(textViewWidth, height);
}
#Override
protected void onTextChanged(CharSequence text, int start, int before,
int after) {
super.onTextChanged(text, start, before, after);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
}
#Override
public boolean onTextContextMenuItem(int id) {
boolean consumed = super.onTextContextMenuItem(id);
switch (id) {
case android.R.id.paste:
if (mOnEditTextActionListener != null) {
mOnEditTextActionListener.onPaste();
}
break;
}
return consumed;
}
/**
* Setter method for {#link #mOnEditTextActionListener}
*
* #param onEditTextActionListener
* Instance of the {#link OnEditTextActionListener}
*/
public void setOnEditTextActionListener(OnEditTextActionListener onEditTextActionListener) {
this.mOnEditTextActionListener = onEditTextActionListener;
}
private Rect mRect = new Rect();
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = MotionEventCompat.getActionMasked(event);
int[] location = new int[2];
getLocationOnScreen(location);
mRect.left = location[0];
mRect.top = location[1];
mRect.right = location[0] + getWidth();
mRect.bottom = location[1] + getHeight();
int x = (int) event.getX();
int y = (int) event.getY();
if (action == MotionEvent.ACTION_DOWN && !mRect.contains(x, y)) {
InputMethodManager input = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getWindowToken(), 0);
}
return super.dispatchTouchEvent(event);
}
#Override
public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) {
// Since we're replacing the text every time we add or remove a
// character, only read the difference. (issue 5337550)
final int added = event.getAddedCount();
final int removed = event.getRemovedCount();
final int length = event.getBeforeText().length();
if (added > removed) {
event.setRemovedCount(0);
event.setAddedCount(1);
event.setFromIndex(length);
} else if (removed > added) {
event.setRemovedCount(1);
event.setAddedCount(0);
event.setFromIndex(length - 1);
} else {
return;
}
} else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) {
// The parent EditText class lets tts read "edit box" when this View has a focus, which
// confuses users on app launch (issue 5275935).
return;
}
super.sendAccessibilityEventUnchecked(event);
}
/**
* Interface to get callback from the Edittext copy, cut and paste event
* For time being only the Paste Event callback is generated
*/
public interface OnEditTextActionListener {
/**
* If Edittext get paste event then this method will be called
*/
void onPaste();
}
}
In your xml you can give like this,
<[package name].KeypadlessKeypad
android:id="#+id/dialnumbertv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cursorVisible="false"
android:ellipsize="start"
android:gravity="center"
android:inputType="phone"
android:singleLine="true"
android:textIsSelectable="true"
android:textSize="30sp"
android:textStyle="italic"
android:visibility="visible"/>
And in your fragment you can implement like this,
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDialNumbertv = view.findViewById(R.id.dialnumbertv);
mDialNumbertv.setCursorVisible(false);
mDialNumbertv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isDigitsEmpty()) {
mDialNumbertv.setCursorVisible(true);
}
}
});
mDialNumbertv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (isDigitsEmpty()) {
mDialNumbertv.setCursorVisible(false);
}
// updateDeleteButton();
}
});
mDialNumbertv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Ref https://android.googlesource.com/platform/packages/apps/Contacts/+/39948dc7e34dc2041b801058dada28fedb80c388/src/com/android/contacts/dialpad/DialpadFragment.java
// Right now EditText does not show the "paste" option when cursor is not visible.
// To show that, make the cursor visible, and return false, letting the EditText
// show the option by itself.
mDialNumbertv.setCursorVisible(true);
return false;
}
});
mDialNumbertv.setOnEditTextActionListener(
new KeypadlessKeypad.OnEditTextActionListener() {
#Override
public void onPaste() {
// If some content pasted on mDialNumbertv
// we need to run some search on Contact and Price
String mobileNumber = mDialNumbertv.getText().toString();
if (TextUtils.isEmpty(mobileNumber)) {
return;
}
// updateContactName(mobileNumber);
}
});
}
private KeypadlessKeypad mDialNumbertv;
private boolean isDigitsEmpty() {
return mDialNumbertv.length() == 0;
}
private void setClickedDigit(final String digitToSet) {
if (!TextUtils.isEmpty(digitToSet)) {
char digit = digitToSet.charAt(0);
String mobileNumber = mDialNumbertv.getText() + digitToSet;
mDialNumbertv.getText().insert(mDialNumbertv.getSelectionStart(), digitToSet);
// If the cursor is at the end of the text we hide it.
final int length = mDialNumbertv.length();
if (length == mDialNumbertv.getSelectionStart() && length == mDialNumbertv.getSelectionEnd()) {
mDialNumbertv.setCursorVisible(false);
}
}
}
I wanted the same behavior which I achieved as follows -
Make a custom class that will override 2 methods of AppCompatEditText.
class CustomEditText(context: Context?, attrs: AttributeSet) : AppCompatEditText(context, attrs) {
override fun onCheckIsTextEditor(): Boolean {
return true
}
override fun isTextSelectable(): Boolean {
return true
}
}
In the XML file, create EditText using this custom view.
<com.ui.custom.CustomEditText
android:id="#+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="none"
android:focusable="true"
android:gravity="center"
android:focusableInTouchMode="true"/>
Now, just add onFocusChangeListener and set editText.setKeyListener = null.
binding.etEmail.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
binding.etEmail.keyListener = null
}
}
You can add the same on onTouch if that is the requirement.
The main issue here is that onCheckIsTextEditor() of View class always returns false, which leads to cursor never blinking or being visible even if setCursorVisible(true) was called in code.
I hope it helps.
You can use edittext.setselection(0)
or
maybe you can request focus using requestfocus()
Related
I am working on an Android app and processing user input from an edittext field. I want to check if the user entered a correct date format; however, for reasons that I don´t understand, I need to convert the type of the edittext type date to a string and then convert it back to a float number, to check against a regular expression and that is not working correctly.
The editactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_normal_text"
android:hint="Enter the title of your selfie"
android:inputType="text"
android:textStyle="bold"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_name"
android:hint="Enter the title of your selfie"
android:inputType="text"
android:textStyle="bold"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:hint="Enter the title of your selfie"
android:inputType="text"
android:textStyle="bold"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_location"
android:hint="Enter the title of your selfie"
android:inputType="text"
android:textStyle="bold"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_email_address"
android:textStyle="bold"
android:inputType="textEmailAddress"
android:hint="Enter Email Address"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ed_date"
android:hint="Enter Date"
android:inputType="date"
android:textStyle="bold"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/location"
android:hint="Enter Location"
android:inputType="textAutoComplete"
android:textStyle="bold"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/btn_submit" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="106dp"
android:padding="1dp"
android:layout_weight="0.98"></ImageView>
<Button
android:id="#+id/btnCapture"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:text="Take picture" />
<Button
android:id="#+id/save"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="#+id/cancel"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Cancel" />
<Button
android:id="#+id/image"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:text="See Image" />
</LinearLayout>
The EditActivity:
package gmbh.packagename.myselfieme;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.icu.util.Calendar;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import static gmbh.packagename.myselfieme.R.drawable.marker;
import static gmbh.packagename.myselfieme.R.id.date;
import static gmbh.packagename.myselfieme.R.id.time;
import static gmbh.packagename.myselfieme.R.string.snippet;
public class EditActivity extends Activity {
public static int count = 0;
static final int REQUEST_TAKE_PHOTO = 1;
private static final String KEY_MARKER_ID = "id";
private static final String KEY_MARKER_TITLE = "title";
private static final String KEY_MARKER_DATE = "date";
private static final String KEY_MARKER_LOC = "location";
private static final String KEY_MARKER_LAT = "latlng";
private static final String KEY_MARKER_NAME = "name";
private static final String KEY_MARKER_PIC = "picture";
private static final int CAMERA_REQUEST = 1888;
private EditText etNormalText;
private EditText etName;
private EditText etLocation;
private EditText etEmailAddrss;
private EditText etDate;
private Button btnSubmit;
private ImageView imageView;
Bitmap photo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editactivity);
PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);
helper.getReadableDatabase();
registerViews();
this.imageView = (ImageView) this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.btnCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
private void registerViews() {
etNormalText = (EditText) findViewById(R.id.et_normal_text);
etName = (EditText) findViewById(R.id.et_name);
etLocation = (EditText) findViewById(R.id.et_location);
// TextWatcher would let us check validation error on the fly
etNormalText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Validation.hasText(etNormalText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etName.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Validation.hasText(etName);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etLocation.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Validation.hasText(etLocation);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etEmailAddrss = (EditText) findViewById(R.id.et_email_address);
etEmailAddrss.addTextChangedListener(new TextWatcher() {
// after every change has been made to this editText, we would like to check validity
public void afterTextChanged(Editable s) {
Validation.isEmailAddress(etEmailAddrss, true);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
etDate = (EditText) findViewById(R.id.ed_date);
etDate.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Validation.isDate(etDate, false);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.w("EditActivity", "clicking on btn_submit");
/*
Validation class will check the error and display the error on respective fields
but it won't resist the form submission, so we need to check again before submit
*/
if ( checkValidation () )
submitForm();
else
Toast.makeText(EditActivity.this, "Form contains error", Toast.LENGTH_LONG).show();
}
});
final LatLng latlng = getIntent().getParcelableExtra("location");
final EditText title = (EditText) findViewById(R.id.title);
final EditText date = (EditText) findViewById(R.id.ed_date);
final EditText location = (EditText) findViewById(R.id.location);
final EditText name = (EditText) findViewById(R.id.name);
Button button = (Button) findViewById(R.id.save);
Button cancelbutton = (Button) findViewById(R.id.cancel);
Button imagebutton = (Button) findViewById(R.id.image);
imagebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(EditActivity.this, ImageActivity.class);
myIntent.putExtra("marker", marker);
myIntent.putExtra("BitmapImage", photo);
EditActivity.this.startActivity(myIntent);
setResult(Activity.RESULT_OK, myIntent);
}
});
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(date.getText().length()<1){
// Display toast
Toast.makeText(getApplicationContext(), "Please enter something !",Toast.LENGTH_LONG).show();
}
}
});
date.addTextChangedListener(new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(date.getText().length()<1){
// Display toast
Toast.makeText(getApplicationContext(), "Please enter something !",Toast.LENGTH_LONG).show();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//String ss = date.getText().toString();
int o = 0;
//String regEx ="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d{2}$";
/*
if ((ss.charAt(2) == '/') && (ss.charAt(4) == '/')) {
}*/
int ss = date.getInputType();
if (ss == (InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE))
{
int dateInput = Integer.parseInt(date.getText().toString());
//(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)
Toast.makeText(EditActivity.this, "Format Is right", Toast.LENGTH_LONG).show();
}
/* if (ss.matches("\\d{4}-\\d{2}-\\d{2}")) {
Toast.makeText(EditActivity.this, "Format Is right", Toast.LENGTH_LONG).show();
}*/
/*if(date.getText().length()<4){
// Display toast
Toast.makeText(getApplicationContext(), "Please enter something !",Toast.LENGTH_LONG).show();
}*/
else {
date.setTextColor(Color.RED);
date.setText("Invalid Format");
}
//ss = "";
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Bitmap resized = Bitmap.createScaledBitmap(photo, (int) (photo.getWidth() * 0.5), (int) (photo.getHeight() * 0.5), true);
//Bitmap.Config conf = Bitmap.Config.ARGB_8888;
//Bitmap bmp = Bitmap.createBitmap(150, 150, conf);
Canvas canvas1 = new Canvas();
Rect rectangle = new Rect(0,0,100,100);
canvas1.drawBitmap(resized, new Rect(0,0,100,100), rectangle, null);
resized = addBorderToBitmap(resized, 10, Color.WHITE);
// Add a border around the bitmap as shadow
resized = addBorderToBitmap(resized, 3, Color.LTGRAY);
MarkerOptions marker = new MarkerOptions().position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(resized))
.draggable(true);
if (title.getText() != null) {
marker.title(title.getText().toString());
}
if( date.getText().toString().length() == 0 )
date.setError( "date is required!" );
marker.snippet(date.getText().toString());
if (name.getText() !=null) {
marker.snippet(name.getText().toString());
}
if (location.getText() !=null) {
marker.snippet(location.getText().toString());
}
Intent resultIntent = new Intent();
resultIntent.putExtra("marker", marker);
resultIntent.putExtra("BitmapImage", photo); // passing the bitmap to the next activity . and retrieve it to the next activity
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});}
public static Bitmap overlay(Bitmap bmp, Bitmap resized) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp, new Matrix(), null);
canvas.drawBitmap(resized, 0, 0, null);
return bmOverlay;
}
private void submitForm() {
// Submit your form here. your form is valid
Toast.makeText(this, "Submitting form...", Toast.LENGTH_LONG).show();
Log.w("EditActivity", "submitForm");
}
private boolean checkValidation() {
Log.w("EditActivity", "checkValidation");
boolean ret = true;
if (!Validation.hasText(etNormalText)) ret = false;
if (!Validation.isEmailAddress(etEmailAddrss, true)) ret = false;
if (!Validation.isDate(etDate, false)) ret = false;
return ret;
}
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to start
Intent myIntent = new Intent(EditActivity.this, MapsActivity.class);
// Launch the Activity using the intent
EditActivity.this.startActivity(myIntent);
}
protected Bitmap addBorderToBitmap(Bitmap resized, int borderWidth, int borderColor) {
// Initialize a new Bitmap to make it bordered bitmap
Bitmap dstBitmap = Bitmap.createBitmap(
resized.getWidth() + borderWidth * 2, // Width
resized.getHeight() + borderWidth * 2, // Height
Bitmap.Config.ARGB_8888 // Config
);
/*
Canvas
The Canvas class holds the "draw" calls. To draw something, you need 4 basic
components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing
into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint
(to describe the colors and styles for the drawing).
*/
// Initialize a new Canvas instance
Canvas canvas = new Canvas(dstBitmap);
// Initialize a new Paint instance to draw border
Paint paint = new Paint();
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderWidth);
paint.setAntiAlias(true);
Rect rect = new Rect(
borderWidth / 2,
borderWidth / 2,
canvas.getWidth() - borderWidth / 2,
canvas.getHeight() - borderWidth / 2
);
// Draw a rectangle as a border/shadow on canvas
canvas.drawRect(rect, paint);
// Draw source bitmap to canvas
canvas.drawBitmap(resized, borderWidth, borderWidth, null);
resized.recycle();
// Return the bordered circular bitmap
return dstBitmap;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
this.imageView.setImageBitmap(photo);
}
}
}
The problem is in this snippet of code here:
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//String ss = date.getText().toString();
int o = 0;
//String regEx ="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d{2}$";
/*
if ((ss.charAt(2) == '/') && (ss.charAt(4) == '/')) {
}*/
int ss = date.getInputType();
if (ss == (InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE))
{
int dateInput = Integer.parseInt(date.getText().toString());
//(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)
Toast.makeText(EditActivity.this, "Format Is right", Toast.LENGTH_LONG).show();
}
/* if (ss.matches("\\d{4}-\\d{2}-\\d{2}")) {
Toast.makeText(EditActivity.this, "Format Is right", Toast.LENGTH_LONG).show();
}*/
/*if(date.getText().length()<4){
// Display toast
Toast.makeText(getApplicationContext(), "Please enter something !",Toast.LENGTH_LONG).show();
}*/
else {
date.setTextColor(Color.RED);
date.setText("Invalid Format");
}
//ss = "";
}
});
This is the validation.java:
package gmbh.package.myselfieme;
import android.widget.EditText;
import java.util.regex.Pattern;
public class Validation {
// Regular Expression
// you can change the expression based on your need
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final String DATE_REGEX = "^\\d{4}-\\d{2}-\\d{2}$";
// Error Messages
private static final String REQUIRED_MSG = "required";
private static final String EMAIL_MSG = "invalid email";
private static final String DATE_MSG = "###-#######";
// call this method when you need to check email validation
public static boolean isEmailAddress(EditText editText, boolean required) {
return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required);
}
// call this method when you need to check phone number validation
public static boolean isDate(EditText editText, boolean required) {
return isValid(editText, DATE_REGEX, DATE_MSG, required);
}
// return true if the input field is valid, based on the parameter passed
public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {
String text = editText.getText().toString().trim();
// clearing the error, if it was previously set by some other values
editText.setError(null);
// text required and editText is blank, so return false
if ( required && !hasText(editText) ) return false;
// pattern doesn't match so returning false
if (required && !Pattern.matches(regex, text)) {
editText.setError(errMsg);
return false;
};
return true;
}
// check the input field has any text or not
// return true if it contains text otherwise false
public static boolean hasText(EditText editText) {
String text = editText.getText().toString().trim();
editText.setError(null);
// length 0 means there is no text
if (text.length() == 0) {
editText.setError(REQUIRED_MSG);
return false;
}
return true;
}
}
This is the error message:
Process: gmbh.packagename.myselfieme, PID: 17795
java.lang.NumberFormatException: Invalid int: "2505250525"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:413)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at gmbh.packagename.myselfieme.EditActivity$10.afterTextChanged(EditActivity.java:203)
Actually, why do I need to read the input in an input type of date, cast it to an integer and check if this integer value has the right format? Isn`t there any other, better way? I just want to check that the users does not enter more than 6 digits and that these digits have the right format.
Any hints or help would be appreciated, thanks!
I just want to check that the users does not enter more than 6 digits and that these digits have the right format.
Lets say, required format is dd-mm-yy
Use regex - \d{2}-\d{2}-\d{2}
For example,
if (inputSTR.matches("\\d{2}-\\d{2}-\\d{2}")) {
// input matches to required pattern
} else {
// Show error msg
}
First, Tim Biegeleisen’s comment that you should use a date picker rather than an EditText is probably well worth considering.
Anyway, if you get your date as a string, the recommended way of validating it is to try to parse it into a date and see if you succeed — not through a regular expression. And you certainly shouldn’t try to parse it as an integer, that is bound to fail. My code would go something like:
String ss = date.getText().toString();
if (ss.isEmpty()) {
// tell the user to enter something
} else {
try {
LocalDate.parse(ss);
// tell the user the format is right
} catch (DateTimeParseException dtpe) {
// tell the user that this is not a valid date
}
}
I am assuming that your date should be in the format yyyy-MM-dd. This is the default parse format for LocalDate, so it’s rather straightforward if you know about exceptions and try/catch constructs. If you require a different format, you will need to specify it through a DateTimeFormatter.
The advantage over the regex approach is it gives you much better validation. ^\\d{4}-\\d{2}-\\d{2}$ will accept 2017-29-11 and 2017-11-92 as dates, where the parsing approach will catch if the month is not 1 through 12 or the day-of-month is not within the number of days in that month.
Now you are at it, if you need to keep your date (likely since you asked the user to enter it), keep it as a LocalDate object rather than a string. This will prepare your app for doing all sorts of operations on the date. Whenever you need a string, just use LocalDate.toString(). Or use a DateTimeFormatter if you want the string as for example “Sunday November 19, 2017” or even in German or some other language.
I am using java.time, the modern Java date and time API also known as JSR-310. Unfortunately this doesn’t come with most Android devices yet. The solution is the ThreeTenABP, the backport of JSR-310 to Android. I encourage you to get this and start coding.
Are there any additional libraries that contain button graphics for the Android ExoPlayer found here. There are the options for fast forward, rewind, play, pause and a few others but there are no options for a mute/unmute or fast forward 2x/4x/etc. I want those options as I build an alternative UI.
Change the Media Controller to provide the options that going to mute/unmute or fast forward. For adding these view(Buttons) in to the view of controller then for that customize the media controller. Check this also Custom Media Controller
VideoControllerView
public class VideoControllerView extends FrameLayout {
private static final String TAG = "VideoControllerView";
private MediaPlayerControl mPlayer;
private Context mContext;
private ViewGroup mAnchor;
private View mRoot;
private ProgressBar mProgress;
private TextView mEndTime, mCurrentTime;
private boolean mShowing;
private boolean mDragging;
private static final int sDefaultTimeout = 3000;
private static final int FADE_OUT = 1;
private static final int SHOW_PROGRESS = 2;
private boolean mUseFastForward;
private boolean mFromXml;
private boolean mListenersSet;
private OnClickListener mNextListener;
StringBuilder mFormatBuilder;
Formatter mFormatter;
private ImageButton mPauseButton;
private ImageButton mVolumeButton;
private ImageButton mRewButton;
private ImageButton mSettingButton;
private Button mQualityButton;
private ImageButton mFullscreenButton;
private Handler mHandler = new MessageHandler(this);
private SeekBar seekbar;
private AudioManager audioManager;
private TextView txtVolume;
public VideoControllerView(Context context, AttributeSet attrs) {
super(context, attrs);
mRoot = null;
mContext = context;
mUseFastForward = true;
mFromXml = true;
Log.i(TAG, TAG);
}
public VideoControllerView(Context context, boolean useFastForward) {
super(context);
mContext = context;
mUseFastForward = useFastForward;
Log.i(TAG, TAG);
}
public VideoControllerView(Context context) {
this(context, true);
Log.i(TAG, TAG);
}
#SuppressLint("MissingSuperCall")
#Override
public void onFinishInflate() {
if (mRoot != null)
initControllerView(mRoot);
}
public void setMediaPlayer(MediaPlayerControl player) {
mPlayer = player;
updatePausePlay();
updateFullScreen(false);
}
/**
* Set the view that acts as the anchor for the control view.
* This can for example be a VideoView, or your Activity's main view.
*
* #param view The view to which to anchor the controller when it is visible.
*/
public void setAnchorView(ViewGroup view) {
mAnchor = view;
LayoutParams frameParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
removeAllViews();
View v = makeControllerView();
addView(v, frameParams);
}
/**
* Create the view that holds the widgets that control playback.
* Derived classes can override this to create their own.
*
* #return The controller view.
* #hide This doesn't work as advertised
*/
protected View makeControllerView() {
LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRoot = inflate.inflate(R.layout.custom_media_controller, null);
initControllerView(mRoot);
return mRoot;
}
#SuppressLint("WrongViewCast")
private void initControllerView(View v) {
mPauseButton = (ImageButton) v.findViewById(R.id.pause);
if (mPauseButton != null) {
mPauseButton.requestFocus();
mPauseButton.setOnClickListener(mPauseListener);
}
mFullscreenButton = (ImageButton) v.findViewById(R.id.fullscreen);
if (mFullscreenButton != null) {
mFullscreenButton.requestFocus();
mFullscreenButton.setOnClickListener(mFullscreenListener);
}
mVolumeButton = (ImageButton) v.findViewById(R.id.volume);
if (mVolumeButton != null) {
mVolumeButton.setOnClickListener(mVolumeListener);
if (!mFromXml) {
//mVolumeButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
}
}
mRewButton = (ImageButton) v.findViewById(R.id.rew);
if (mRewButton != null) {
mRewButton.setOnClickListener(mRewListener);
if (!mFromXml) {
mRewButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
}
}
// By default these are hidden. They will be enabled when setPrevNextListeners() is called
mSettingButton = (ImageButton) v.findViewById(R.id.settings);
if (mSettingButton != null && !mFromXml && !mListenersSet) {
// mSettingButton.setVisibility(View.GONE);
}
mQualityButton = (Button) v.findViewById(R.id.quality);
if (mQualityButton != null && !mFromXml && !mListenersSet) {
//mQualityButton.setVisibility(View.GONE);
}
mProgress = (ProgressBar) v.findViewById(R.id.mediacontroller_progress);
if (mProgress != null) {
if (mProgress instanceof SeekBar) {
SeekBar seeker = (SeekBar) mProgress;
seeker.setOnSeekBarChangeListener(mSeekListener);
}
mProgress.setMax(1000);
}
mEndTime = (TextView) v.findViewById(R.id.time);
mCurrentTime = (TextView) v.findViewById(R.id.time_current);
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
installPrevNextListeners();
}
/**
* Show the controller on screen. It will go away
* automatically after 3 seconds of inactivity.
*/
public void show() {
show(sDefaultTimeout);
}
/**
* Disable pause or seek buttons if the stream cannot be paused or seeked.
* This requires the control interface to be a MediaPlayerControlExt
*/
private void disableUnsupportedButtons() {
if (mPlayer == null) {
return;
}
try {
if (mPauseButton != null && !mPlayer.canPause()) {
mPauseButton.setEnabled(false);
}
if (mRewButton != null && !mPlayer.canSeekBackward()) {
mRewButton.setEnabled(false);
}
if (mVolumeButton != null && !mPlayer.canSeekForward()) {
mVolumeButton.setEnabled(false);
}
} catch (IncompatibleClassChangeError ex) {
// We were given an old version of the interface, that doesn't have
// the canPause/canSeekXYZ methods. This is OK, it just means we
// assume the media can be paused and seeked, and so we don't disable
// the buttons.
}
}
/**
* Show the controller on screen. It will go away
* automatically after 'timeout' milliseconds of inactivity.
*
* #param timeout The timeout in milliseconds. Use 0 to show
* the controller until hide() is called.
*/
public void show(int timeout) {
if (!mShowing && mAnchor != null) {
setProgress();
if (mPauseButton != null) {
mPauseButton.requestFocus();
}
disableUnsupportedButtons();
LayoutParams tlp = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM
);
mAnchor.addView(this, tlp);
mShowing = true;
}
updatePausePlay();
// cause the progress bar to be updated even if mShowing
// was already true. This happens, for example, if we're
// paused with the progress bar showing the user hits play.
mHandler.sendEmptyMessage(SHOW_PROGRESS);
Message msg = mHandler.obtainMessage(FADE_OUT);
if (timeout != 0) {
mHandler.removeMessages(FADE_OUT);
mHandler.sendMessageDelayed(msg, timeout);
}
}
public boolean isShowing() {
return mShowing;
}
/**
* Remove the controller from the screen.
*/
public void hide() {
if (mAnchor == null) {
return;
}
try {
mAnchor.removeView(this);
mHandler.removeMessages(SHOW_PROGRESS);
} catch (IllegalArgumentException ex) {
Log.w("MediaController", "already removed");
}
mShowing = false;
}
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
private int setProgress() {
if (mPlayer == null || mDragging) {
return 0;
}
int position = mPlayer.getCurrentPosition();
int duration = mPlayer.getDuration();
//Log.d(TAG, "Duration->" + duration);
//Log.d(TAG, "Duration Current->" + position);
if (mProgress != null) {
if (duration > 0) {
// use long to avoid overflow
long pos = 1000L * position / duration;
mProgress.setProgress((int) pos);
}
int percent = mPlayer.getBufferPercentage();
mProgress.setSecondaryProgress(percent * 10);
}
if (mEndTime != null)
mEndTime.setText(stringForTime(duration));
if (mCurrentTime != null)
mCurrentTime.setText(stringForTime(position));
return position;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
show(sDefaultTimeout);
return true;
}
#Override
public boolean onTrackballEvent(MotionEvent ev) {
show(sDefaultTimeout);
return false;
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mPlayer == null) {
return true;
}
int keyCode = event.getKeyCode();
final boolean uniqueDown = event.getRepeatCount() == 0
&& event.getAction() == KeyEvent.ACTION_DOWN;
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
|| keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keyCode == KeyEvent.KEYCODE_SPACE) {
if (uniqueDown) {
doPauseResume();
show(sDefaultTimeout);
if (mPauseButton != null) {
mPauseButton.requestFocus();
}
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
if (uniqueDown && !mPlayer.isPlaying()) {
mPlayer.start();
updatePausePlay();
show(sDefaultTimeout);
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
|| keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
if (uniqueDown && mPlayer.isPlaying()) {
mPlayer.pause();
updatePausePlay();
show(sDefaultTimeout);
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
// don't show the controls for volume adjustment
return super.dispatchKeyEvent(event);
} else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
if (uniqueDown) {
hide();
}
return true;
}
show(sDefaultTimeout);
return super.dispatchKeyEvent(event);
}
private OnClickListener mPauseListener = new OnClickListener() {
public void onClick(View v) {
doPauseResume();
show(sDefaultTimeout);
}
};
private OnClickListener mQualityChangeListener = new OnClickListener() {
public void onClick(View v) {
mPlayer.updateVideoQuality();
}
};
private OnClickListener mFullscreenListener = new OnClickListener() {
public void onClick(View v) {
doToggleFullscreen();
show(sDefaultTimeout);
}
};
public void updatePausePlay() {
if (mRoot == null || mPauseButton == null || mPlayer == null) {
return;
}
if (mPlayer.isPlaying()) {
mPauseButton.setImageResource(R.drawable.ic_pause);
} else {
mPauseButton.setImageResource(R.drawable.ic_play);
}
}
public void updateFullScreen(boolean isFullScreen) {
if (mRoot == null || mFullscreenButton == null || mPlayer == null) {
return;
}
if (isFullScreen) {
mFullscreenButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_unstretch));
} else {
mFullscreenButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_stretch));
}
}
private void doPauseResume() {
if (mPlayer == null) {
return;
}
if (mPlayer.isPlaying()) {
mPlayer.pause();
} else {
mPlayer.start();
}
updatePausePlay();
}
private void doToggleFullscreen() {
if (mPlayer == null) {
return;
}
if (mPlayer.isFullScreen()) {
mFullscreenButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_unstretch));
} else {
mFullscreenButton.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_stretch));
}
mPlayer.toggleFullScreen();
}
// There are two scenarios that can trigger the seekbar listener to trigger:
//
// The first is the user using the touchpad to adjust the posititon of the
// seekbar's thumb. In this case onStartTrackingTouch is called followed by
// a number of onProgressChanged notifications, concluded by onStopTrackingTouch.
// We're setting the field "mDragging" to true for the duration of the dragging
// session to avoid jumps in the position in case of ongoing playback.
//
// The second scenario involves the user operating the scroll ball, in this
// case there WON'T BE onStartTrackingTouch/onStopTrackingTouch notifications,
// we will simply apply the updated position without suspending regular updates.
private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
public void onStartTrackingTouch(SeekBar bar) {
show(3600000);
mDragging = true;
// By removing these pending progress messages we make sure
// that a) we won't update the progress while the user adjusts
// the seekbar and b) once the user is done dragging the thumb
// we will post one of these messages to the queue again and
// this ensures that there will be exactly one message queued up.
mHandler.removeMessages(SHOW_PROGRESS);
}
public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) {
if (mPlayer == null) {
return;
}
if (!fromuser) {
// We're not interested in programmatically generated changes to
// the progress bar's position.
return;
}
long duration = mPlayer.getDuration();
long newposition = (duration * progress) / 1000L;
mPlayer.seekTo((int) newposition);
if (mCurrentTime != null)
mCurrentTime.setText(stringForTime((int) newposition));
}
public void onStopTrackingTouch(SeekBar bar) {
mDragging = false;
setProgress();
updatePausePlay();
show(sDefaultTimeout);
// Ensure that progress is properly updated in the future,
// the call to show() does not guarantee this because it is a
// no-op if we are already showing.
mHandler.sendEmptyMessage(SHOW_PROGRESS);
}
};
#Override
public void setEnabled(boolean enabled) {
if (mPauseButton != null) {
mPauseButton.setEnabled(enabled);
}
if (mVolumeButton != null) {
mVolumeButton.setEnabled(enabled);
}
if (mRewButton != null) {
mRewButton.setEnabled(enabled);
}
if (mSettingButton != null) {
mSettingButton.setEnabled(enabled && mNextListener != null);
}
if (mQualityButton != null) {
mQualityButton.setEnabled(enabled && mQualityChangeListener != null);
}
if (mProgress != null) {
mProgress.setEnabled(enabled);
}
disableUnsupportedButtons();
super.setEnabled(enabled);
}
private OnClickListener mRewListener = new OnClickListener() {
public void onClick(View v) {
if (mPlayer == null) {
return;
}
int pos = mPlayer.getCurrentPosition();
pos -= 15000; // milliseconds
mPlayer.seekTo(pos);
setProgress();
show(sDefaultTimeout);
}
};
private OnClickListener mVolumeListener = new OnClickListener() {
public void onClick(View v) {
if (mPlayer == null) {
return;
}
showPopup(mContext);
}
};
private void installPrevNextListeners() {
if (mSettingButton != null) {
mSettingButton.setOnClickListener(mNextListener);
mSettingButton.setEnabled(mNextListener != null);
}
if (mQualityButton != null) {
mQualityButton.setOnClickListener(mQualityChangeListener);
mQualityButton.setEnabled(mQualityChangeListener != null);
}
}
public void setPrevNextListeners(OnClickListener next, OnClickListener prev) {
mNextListener = next;
mQualityChangeListener = prev;
mListenersSet = true;
if (mRoot != null) {
installPrevNextListeners();
if (mSettingButton != null && !mFromXml) {
mSettingButton.setVisibility(View.VISIBLE);
}
if (mQualityButton != null && !mFromXml) {
mQualityButton.setVisibility(View.VISIBLE);
}
}
}
public interface MediaPlayerControl {
void start();
void pause();
int getDuration();
int getCurrentPosition();
void seekTo(int pos);
boolean isPlaying();
int getAudioSessionId();
int getBufferPercentage();
boolean canPause();
boolean canSeekBackward();
boolean canSeekForward();
boolean isFullScreen();
void toggleFullScreen();
void updateVideoQuality();
}
private static class MessageHandler extends Handler {
private final WeakReference<VideoControllerView> mView;
MessageHandler(VideoControllerView view) {
mView = new WeakReference<VideoControllerView>(view);
}
#Override
public void handleMessage(Message msg) {
VideoControllerView view = mView.get();
if (view == null || view.mPlayer == null) {
return;
}
int pos;
switch (msg.what) {
case FADE_OUT:
view.hide();
break;
case SHOW_PROGRESS:
pos = view.setProgress();
if (!view.mDragging && view.mShowing && view.mPlayer.isPlaying()) {
msg = obtainMessage(SHOW_PROGRESS);
sendMessageDelayed(msg, 1000 - (pos % 1000));
}
break;
}
}
}
// The method that displays the popup.
private void showPopup(final Context context) {
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_volumebar, viewGroup);
seekbar = (SeekBar) layout.findViewById(R.id.seekBar1);
txtVolume = (TextView) layout.findViewById(R.id.txtPopupVolume);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER | Gravity.END);
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialog.setTitle(null);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(layout);
dialog.setCancelable(true);
dialog.show();
initControls();
}
private void initControls() {
try {
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
seekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
txtVolume.setText("" + audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
txtVolume.setText("" + progress);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
and custom_media_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/TvControllerBackground"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="4dip">
<Button
android:id="#+id/quality"
style="#style/MediaButton.Previous"
android:background="#null"
android:contentDescription="#string/description"
android:paddingLeft="#dimen/size_5"
android:paddingRight="#dimen/size_5"
android:text="#string/hd"
android:textColor="#color/white"
android:textStyle="bold"
android:visibility="gone" />
<ImageButton
android:id="#+id/rew"
style="#style/MediaButton.Rew"
android:contentDescription="#string/description"
android:visibility="gone" />
<ImageButton
android:id="#+id/pause"
style="#style/MediaButton.Play"
android:contentDescription="#string/description" />
<ImageButton
android:id="#+id/volume"
style="#style/MediaButton.Ffwd"
android:contentDescription="#string/description" />
<ImageButton
android:id="#+id/settings"
style="#style/MediaButton.Setting"
android:contentDescription="#string/description"
android:visibility="gone" />
<ImageButton
android:id="#+id/fullscreen"
style="#style/MediaButton.FullScreen"
android:contentDescription="#string/description"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="#dimen/size_5">
<TextView
android:id="#+id/time_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="4dip"
android:paddingRight="4dip"
android:paddingTop="4dip"
android:textSize="14sp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/mediacontroller_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dip"
android:layout_height="32dip"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:progressDrawable="#drawable/apptheme_scrubber_progress_horizontal_holo_light"
android:thumb="#drawable/apptheme_scrubber_control_selector_holo_light" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="4dip"
android:paddingRight="4dip"
android:paddingTop="4dip"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
I'm trying to make a custom number picker but i am getting a android.view.ViewRootImpl$CalledFromWrongThreadException when trying to auto decrease value.
my_number.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="#dimen/numberpicker_button_size"
android:layout_height="#dimen/numberpicker_button_size"
android:enabled="true"
android:focusable="true"
android:background="#drawable/number_picker_top_btn"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:id="#+id/top_button" />
<EditText
android:layout_width="#dimen/numberpicker_middle"
android:layout_height="#dimen/numberpicker_middle"
android:inputType="number"
android:textSize="#dimen/text_size_small"
android:clickable="false"
android:textColor="#color/white"
android:background="#drawable/number_picker_middle"
android:gravity="center"
android:layout_marginTop="#dimen/numberpicker_gap"
android:id="#+id/middle_text" />
<Button
android:layout_width="#dimen/numberpicker_button_size"
android:layout_height="#dimen/numberpicker_button_size"
android:enabled="true"
android:focusable="true"
android:background="#drawable/number_picker_bottom_btn"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/numberpicker_gap"
android:id="#+id/bottom_button" />
</LinearLayout>
my CustomNumberPicker class:
public class CustomNumberPicker extends LinearLayout {
private Button plus_button, minus_button;
private EditText text;
private float text_size_sp;
private int MIN,MAX, current_value;
private boolean plus_pressed, minus_pressed;
private OnValueChangedListener mChangedListener;
private Timer minusTimer;
View rootView;
public CustomNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mChangedListener = null;
plus_pressed = false;
minus_pressed = false;
rootView = inflate(context,R.layout.my_numberpicker,this);
//I'm new to this so this may not be best way to do this
text = (EditText) rootView.findViewById(R.id.middle_text);
minus_button = (Button)rootView.findViewById(R.id.top_button);
plus_button = (Button)rootView.findViewById(R.id.bottom_button);
text.setEnabled(false);
minus_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
decreaseValue();
}
});
minus_button.setOnLongClickListener(new OnLongClickListener() {
#Override
/*The method set's the boolean state as pressed and starts the new thread which auto decrements values */
public boolean onLongClick(View v) {
minus_pressed = true;
Thread minus = new Thread(new autoValueChanger());
minus.start();
return false;
}
});
minus_button.setOnTouchListener(new OnTouchListener() {
#Override
/* the method used to stop decreasing the value */
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
minus_pressed = false;
}
return false;
}
});
plus_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
increaseValue();
}
});
/* set the min value for this number picker */
public void setMinValue(final int min){
MIN = min;
if(current_value != MIN){
minus_button.setEnabled(true); //updates state
}
}
public void setMaxValue(final int max){
MAX = max;
if(current_value != MAX){
plus_button.setEnabled(true);
}
}
/* change text size */
public void setTextSize(final int size_sp){
text_size_sp = size_sp;
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,size_sp);
}
/* change text colour */
public void setTexColor(int color){
text.setTextColor(color);
}
/* set a new value */
public void setValue(int value){
if(value == MIN){
current_value = value;
text.setText("" + current_value);
minus_button.setEnabled(false); //to stop user from decreasing futher
plus_button.setEnabled(true);
}else if (value == MAX) {
current_value = value;
text.setText("" + current_value);
plus_button.setEnabled(false);
minus_button.setEnabled(true);
}else if(value > MIN && value < MAX){
plus_button.setEnabled(true);
minus_button.setEnabled(true);
current_value = value;
text.setText("" + current_value);
}
}
increases value and notify the Listener
private void increaseValue() {
if(current_value + 1 <= MAX) {
int oldValue = current_value;
current_value++;
setValue(current_value);
notifyListener(oldValue,current_value);
}
}
private void decreaseValue(){
if(current_value - 1 >= MIN) {
int oldValue = current_value;
current_value--;
setValue(current_value);
notifyListener(oldValue,current_value);
}
}
public int getValue(){
return current_value;
}
public int getMinValue(){
return MIN;
}
public int getMaxValue(){
return MAX;
}
/* fires the call back method is there is a listener object instantiated */
private void notifyListener(int oldValue, int newValue){
if(mChangedListener != null){
mChangedListener.OnValueChanged(this,oldValue,newValue);
}
}
/* allows ability to set a new custom event listener */
public void setOnValueCahangedListener(OnValueChangedListener mChangedListener){
this.mChangedListener = mChangedListener;
}
/* custom event listener */
public interface OnValueChangedListener {
void OnValueChanged(CustomNumberPicker cnp, int oldValue, int newValue);
}
/* the class which allows the value to be decreased automaticaly and causing the error mentioned at the top */
private class autoValueChanger implements Runnable{
/**
* Starts executing the active part of the class' code. This method is
* called when a thread is started that has been created with a class which
* implements {#code Runnable}.
*/
#Override
public void run() {
minusTimer = new Timer();
minusTimer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if(minus_pressed){
if(current_value > MIN)
decreaseValue();
else{
minusTimer.cancel();
}
}else {
minusTimer.cancel();
}
}
},0,100);
}
}
}
What am i doing wrong and how can i fix this.
I was able o fix this by using an android handler object instead :
import android.os.Handler;
public class CustomNumberPicker extends LinierLayout{
private final Handler handler = new Handler
private void init(Context context){
minus_button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (!plus_held) {
minus_held = true;
handler.post(new autoValueChanger());
}
return false;
}
});
minus_button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!plus_held && (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)) {
minus_held = false;
}
return false;
}
});
}
private class autoValueChanger implements Runnable {
/**
* Starts executing the active part of the class' code. This method is
* called when a thread is started that has been created with a class which
* implements {#code Runnable}.
*/
#Override
public void run() {
if (minus_held) {
if (current_value > MIN) {
decreaseValue();
handler.postDelayed(new autoValueChanger(), 100);
}else{
minus_held = false;
}
}else if(plus_held) {
if (current_value < MAX){
increaseValue();
handler.postDelayed(new autoValueChanger(),100);
}else {
plus_held = false;
}
}
}
}
private class getArticles extends AsyncTask<Void, Void, Void> {
String url;
getArticles(String paramUrl) {
this.url = paramUrl;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(App.this);
mProgressDialog.setMessage("Učitavanje artikala...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
arraylist = new ArrayList<>();
try {
Document document = Jsoup.connect(url).get();
Elements els = document.select("ul.category3 > li");
for (Element el : els) {
HashMap<String, String> map = new HashMap<>();
Elements slika = el.select("div.category3-image > a > img");
Elements naslov = el.select("div.category3-text > a.main-headline");
Element datum_noformat = el.select("div.category3-text > div.headlines-info > ul.headlines-info > li").first();
Element datum = datum_noformat.html(datum_noformat.html().replaceAll("Posted ", ""));
Elements desc = el.select("div.category3-text > p");
Elements link = el.select("div.category3-text > a.main-headline");
Element br_kom = el.select("div.category3-text > div.headlines-info > ul.headlines-info > li.comments-icon").first();
map.put("naslov", naslov.text());
map.put("datum", datum.text());
map.put("desc", desc.text());
map.put("ikona", slika.attr("src"));
map.put("link", link.attr("abs:href"));
map.put("brkom", br_kom.text());
arraylist.add(map);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
listview = (ListView) findViewById(R.id.listview);
adapter = new ArtikliAdapter(App.this, arraylist);
listview.setAdapter(adapter);
mProgressDialog.dismiss();
}
I searched for a lot of codes for onlistview scrolling, but didn't know how to implement it. The problem is, when I call my asynctask, I have an url param,
like new getArticles("http://example.com").execute();
I want to implement an onscrolllistener, but it goes like this, my param is usually set to: http://www.example.com/category/categoryname/, so the second page goes like http://www.example.com/category/categoryname/page/2/, the third one goes http://www.example.com/category/categoryname/page/3/ and so on. Each page has got 7 items that need to be parsed.
How could I implement onscrolllistener, because of the url param?
Thanks in advance.
Base on this link, I have written following solution to add elements ( 30 elements at a time, i.e page size = 30) to listview asynchronously.
Create a class named EndlessListView as follows:
public class EndlessListView extends ListView implements OnScrollListener {
private View footer;
private boolean isLoading;
private EndlessListener listener;// listner
private EndlessAdapter endlessAdapter;// adapter.
private int maxNoOfElement;
public EndlessListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setOnScrollListener(this);
}
public EndlessListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnScrollListener(this);
}
public EndlessListView(Context context) {
super(context);
this.setOnScrollListener(this);
}
public void setListener(EndlessListener listener) {
this.listener = listener;
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() == null)
return;
if (getAdapter().getCount() == 0 || getAdapter().getCount() <= 5)
return;
int l = visibleItemCount + firstVisibleItem;
if (l >= totalItemCount && !isLoading) {
// It is time to add new data. We call the listener
Log.e("LOAD", "DATA");
isLoading = true;
listener.loadData();
}
}
public void setMaxElemnt(int maxNoOfElement) {
this.maxNoOfElement = maxNoOfElement;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void setLoadingView(int resId) {
LayoutInflater inflater = (LayoutInflater) super.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
footer = (View) inflater.inflate(resId, null);
this.addFooterView(footer);
}
public void setAdapter(EndlessAdapter adapter) {
super.setAdapter(adapter);
this.endlessAdapter = adapter;
}
public void addNewData(List<Integer> data) {
endlessAdapter.setData(data);
endlessAdapter.notifyDataSetChanged();
isLoading = false;
}
public static interface EndlessListener {
public void loadData();
}
}
After this we have to create the adapter for it,called
EndlessAdapter
public class EndlessAdapter extends BaseAdapter {
private List<Integer> mIntegers;
private Context context;
public EndlessAdapter(Context context) {
this.context = context;
}
public void setData(List<Integer> mIntegers) {
this.mIntegers = mIntegers;
}
#Override
public int getCount() {
if (mIntegers == null)
return 0;
return mIntegers.size();
}
#Override
public Integer getItem(int index) {
if (mIntegers == null) {
return null;
} else {
return mIntegers.get(index);
}
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView textView;
}
#Override
public View getView(int index, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = ((LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.rows, viewGroup, false);
holder.textView = (TextView) view.findViewById(R.id.mtext);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(getItem(index) + "");
return view;
}
}
Now in xml of the activity we could use EndlessListView(in com.example.stackoverflow package) as follows :
<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" >
<com.example.stackoverflow.EndlessListView
android:id="#+id/endlessListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
After this step we need to make following change in the MainActivity
public class MainActivity extends Activity implements EndlessListener {
private EndlessAdapter adapter;
private EndlessListView endlessListView;
private List<Integer> data;
private int gStartIndex = 30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
endlessListView = (EndlessListView) findViewById(R.id.endlessListView);
adapter = new EndlessAdapter(this);
data = new ArrayList<Integer>();
endlessListView.setLoadingView(R.layout.footer);
// endlessListView.showFooter();
endlessListView.setListener(this);
endlessListView.setAdapter(adapter);
gStartIndex = 0;
fillData(gStartIndex);
}
private void fillData(int startIndex) {
new AsyncLoadData().execute(startIndex);
}
#Override
public void loadData() {
fillData(gStartIndex);
}
private class AsyncLoadData extends AsyncTask<Integer, Integer, Void> {
#Override
protected Void doInBackground(Integer... params) {
int gendIndex = params[0] + 30;
gStartIndex = gendIndex;
/***
* Here you could add your n/w code. To simulate the n/w comm. i am
* adding elements to list and making it sleep for few sec.
* */
for (int i = params[0]; i < gendIndex; i++) {
publishProgress(i);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
data.add(values[0]);
}
#Override
protected void onPostExecute(Void result) {
endlessListView.addNewData(data);
}
}
}
This custom ScrollListView that I just found has OnBottomReachedListener which you can implement from your Activity or Fragment and receive events when user hits the bottom of the page. You would also need to track the current page and when bottom is hit to download the next page. The latest data should be added to your existing ArrayList and you should call notifyDataSetChanged() on your adapter so ListView can render the new data. You don't have to create new adapter, you just need to update the data source which is your ArrayList.
If you support orientation change you would must to save in onSaveInstanceState() your current page number so when Activity or Fragment is recreated it can continue from correct page. And you would have to keep the ArrayList data source safe of configuration changes because you don't want to downloaded it again. I would suggest using the Fragment with setRetainInstance() set to true to persist ArrayList.
Here is my custom code for keeping data around using RetainFragment:
/**
* A simple non-UI Fragment that stores a single Object
* and is retained over configuration changes.
*/
public class RetainFragment<E extends Object> extends Fragment {
/** Object for retaining. */
private E mObject;
/**
* Empty constructor as per the Fragment documentation
*/
public RetainFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure this Fragment is retained over a configuration change
setRetainInstance(true);
}
/**
* Store a single object in this Fragment.
*
* #param object The object to store
*/
public void setObject(E object) {
mObject = object;
}
/**
* Get the stored object.
*
* #return The stored object
*/
public E getObject() {
return mObject;
}
}
Example of RetainFragment usage in your Activity:
FragmentManager fm = getFragmentManager();
mRetainFragment = (RetainFragment<ArrayList>) fm.findFragmentByTag(RETAIN_FRAG);
if (mRetainFragment == null) {
mRetainFragment = new RetainFragment<>();
mRetainFragment.setObject(new ArrayList());
fm.beginTransaction().add(mRetainFragment, RETAIN_FRAG).commit();
}
ArrayList yourArrayList = mRetainFragment.getObject();
// Now your ArrayList is saved accrossed configuration changes
here what you want fist add onscrolllistner to listview
boolean loading_flage = false;
yourlistview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(
AbsListView view,
int scrollState) {
// TODO Auto-generated method stub
}
#Override
public void onScroll(AbsListView view,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount) {
final int lastItem = firstVisibleItem
+ visibleItemCount;
if ((lastItem == totalItemCount)
& loading_flage == false) {
//this to prevent the list to make more than one request in time
loading_flage = true;
//you can put the index for your next page here
loadMore(int page);
}
}
});
and then in loading more after loading the page set the loading with false and parse the data add it to the data that you aleardy have
//notify the adapter
adapter.notifyDataSetChanged();
loading_flage = false;
You can achieve by adding the scrolllistener on listview and check condition if list last visible item is last in ArrayList then call the asynctask with incremented page number and update the listview,mean while add the view on listview and after getting the result from server remove the loading more view from the listview like this -
AndroidListViewLoadMoreActivity.java
public class AndroidListViewLoadMoreActivity extends Activity {
ArrayList<Country> countryList;
MyCustomAdapter dataAdapter = null;
int page = 0;
boolean loadingMore = false;
View loadMoreView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
loadMoreView = ((LayoutInflater)this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.loadmore, null, false);
listView.addFooterView(loadMoreView);
//create an ArrayAdaptar from the String Array
countryList = new ArrayList<Country>();
dataAdapter = new MyCustomAdapter(this,
R.layout.country_info, countryList);
listView.setAdapter(dataAdapter);
//enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Country country = (Country) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
country.getCode(), Toast.LENGTH_SHORT).show();
}
});
listView.setOnScrollListener(new OnScrollListener(){
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount) && !(loadingMore)){
String url = "http://10.0.2.2:8080/CountryWebService" +
"/CountryServlet";
grabURL(url);
}
}
});
String url = "http://example.com";
grabURL(url);
}
public void grabURL(String url) {
Log.v("Android Spinner JSON Data Activity", url);
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Void, String> {
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
private final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
HttpResponse response;
private String content = null;
private boolean error = false;
private ProgressDialog dialog =
new ProgressDialog(AndroidListViewLoadMoreActivity.this);
protected void onPreExecute() {
dialog.setMessage("Getting your data... Please wait...");
dialog.show();
}
protected String doInBackground(String... urls) {
String URL = null;
loadingMore = true;
try {
URL = urls[0];
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
HttpPost httpPost = new HttpPost(URL);
//add name value pair for the country code
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("page",String.valueOf(page)));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
content = out.toString();
} else{
//Closes the connection.
Log.w("HTTP1:",statusLine.getReasonPhrase());
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.w("HTTP2:",e );
content = e.getMessage();
error = true;
cancel(true);
} catch (IOException e) {
Log.w("HTTP3:",e );
content = e.getMessage();
error = true;
cancel(true);
}catch (Exception e) {
Log.w("HTTP4:",e );
content = e.getMessage();
error = true;
cancel(true);
}
return content;
}
protected void onCancelled() {
dialog.dismiss();
Toast toast = Toast.makeText(AndroidListViewLoadMoreActivity.this,
"Error connecting to Server", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
}
protected void onPostExecute(String content) {
dialog.dismiss();
Toast toast;
if (error) {
toast = Toast.makeText(AndroidListViewLoadMoreActivity.this,
content, Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else {
displayCountryList(content);
}
}
}
private void displayCountryList(String response){
JSONObject responseObj = null;
try {
Gson gson = new Gson();
responseObj = new JSONObject(response);
JSONArray countryListObj = responseObj.getJSONArray("countryList");
//countryList = new ArrayList<Country>();
if(countryListObj.length() == 0){
ListView listView = (ListView) findViewById(R.id.listView1);
listView.removeFooterView(loadMoreView);
}
else {
for (int i=0; i<countryListObj.length(); i++){
//get the country information JSON object
String countryInfo = countryListObj.getJSONObject(i).toString();
//create java object from the JSON object
Country country = gson.fromJson(countryInfo, Country.class);
//add to country array list
countryList.add(country);
dataAdapter.add(country);
}
page++;
dataAdapter.notifyDataSetChanged();
loadingMore = false;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Make MyCustomAdapter.java as adapter
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
TextView name;
TextView continent;
TextView region;
}
public void add(Country country){
Log.v("AddView", country.getCode());
this.countryList.add(country);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.continent = (TextView) convertView.findViewById(R.id.continent);
holder.region = (TextView) convertView.findViewById(R.id.region);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Country country = this.countryList.get(position);
holder.code.setText(country.getCode());
holder.name.setText(country.getName());
holder.continent.setText(country.getContinent());
holder.region.setText(country.getRegion());
return convertView;
}
}
}
Create Country.Java as pojo -
public class Country {
String code = null;
String name = null;
String continent = null;
String region = null;
Double lifeExpectancy = null;
Double gnp = null;
Double surfaceArea = null;
int population = 0;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public Double getLifeExpectancy() {
return lifeExpectancy;
}
public void setLifeExpectancy(Double lifeExpectancy) {
this.lifeExpectancy = lifeExpectancy;
}
public Double getGnp() {
return gnp;
}
public void setGnp(Double gnp) {
this.gnp = gnp;
}
public Double getSurfaceArea() {
return surfaceArea;
}
public void setSurfaceArea(Double surfaceArea) {
this.surfaceArea = surfaceArea;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
Create main.xml for main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Create country_info.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Code: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Name: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="Continent: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Region: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="TextView" />
<TextView
android:id="#+id/region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/continent"
android:text="TextView" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="TextView" />
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/name"
android:text="TextView" />
</RelativeLayout>
Footer View Text - loadmore.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:padding="3dp"
android:layout_height="fill_parent">
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="5dp"
android:text="Loading more data..."/>
</LinearLayout>
Don't forget to mention internet permission with -
<uses-permission android:name="android.permission.INTERNET" />
First, you need a OnScrollListener method like this:
private OnScrollListener onScrollListener() {
return new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int threshold = 1;
int count = listView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= count - threshold && pageCount < 2) {
Log.i(TAG, "loading more data");
// Execute LoadMoreDataTask AsyncTask
//It reached ListView bottom
getDataFromUrl(url_page2);
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
}
};
}
set List View scroll listener by listView.setOnScrollListener(onScrollListener());
I have a full tutorial post HERE! You can visit it!
I need to add a touchable image view inside an edit text like ms word screen.How can we design an android layout screen for this purpose?I have tried the code shown below:
public class edittext extends EditText
{
public String defaultValue = "";
final Drawable imgX = getResources().getDrawable(android.R.drawable.presence_offline ); // X image
private Html.ImageGetter imageGetter;
public edittext(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
// TODO Auto-generated constructor stub
}
public edittext(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
// TODO Auto-generated constructor stub
}
public edittext(Context context)
{
super(context);
init();
// TODO Auto-generated constructor stub
}
void init() {
// Set bounds of our X button
imgX.setBounds(0, 0, imgX.getIntrinsicWidth(), imgX.getIntrinsicHeight());
// There may be initial text in the field, so we may need to display the button
manageClearButton();
edittext.this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
edittext et = edittext.this;
// Is there an X showing?
if (et.getCompoundDrawables()[2] == null) return false;
// Only do this for up touches
if (event.getAction() != MotionEvent.ACTION_UP) return false;
// Is touch on our clear button?
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgX.getIntrinsicWidth()) {
et.setText("");
edittext.this.removeClearButton();
}
return false;
}
});
edittext.this.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
edittext.this.manageClearButton();
}
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
}
void manageClearButton() {
if (this.getText().toString().equals("") )
removeClearButton();
else
addClearButton();
}
void addClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
imgX,
this.getCompoundDrawables()[3]);
}
void removeClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
null,
this.getCompoundDrawables()[3]);
}
}
if anyone knows about it please help me with thanks.
I am doing it like that. The Drawable will be at the right side of the EditText. Please try the code.
EditText contactLine = new EditText(getActivity());
Drawable drawable = getActivity().getResources().getDrawable(...);
drawable.setBounds(new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
contactLine.setCompoundDrawables(null, null, drawable, null);
contactLine.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Drawable co = v.getCompoundDrawables()[2];
if (co == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_DOWN) {
return false;
}
if (event.getX() > v.getMeasuredWidth() - v.getPaddingRight()
- co.getIntrinsicWidth()) {
whatYouWantToDo();
return true;
} else {
return false;
}
}
});
or you can simply use ImageButton , example :
public void addListenerOnImageButton() {
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// called when imageButton Clicked
}
});}
May be you could do that in a layout using RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/et1"
android:layout_width="200dp"
android:layout_height="100dp"
android:clickable="false"
android:focusable="false"
android:background="#drawable/ic_action_search"/>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et1"
android:paddingLeft="5dp"
android:background="#drawable/ic_launcher"
/>
</RelativeLayout>
For a moveable view in a RelativeLayout, you ca do this. The DragView can be a ImageView.
View DragView;
private boolean inDrag;
int xDragTouchOffset, yDragTouchOffset;
#Override
public boolean onTouchEvent(View View, MotionEvent event) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
if (action == MotionEvent.ACTION_DOWN)
inDrag = true;
xDragTouchOffset = x;
yDragTouchOffset = y;
result = true;
} else if (action == MotionEvent.ACTION_MOVE && inDrag == true) {
setDragImagePosition(x, y);//HERE YOU HANDLE THE POSITION OF YOUR VIEW
result = true;
} else if (action == MotionEvent.ACTION_UP && inDrag ==true) {
inDrag = false;
result = true;
}
return result;
}
private void setDragImagePosition(int x, int y){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) DragView
.getLayoutParams();
lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}