I'm trying to make a popup menu appear when the user clicks on an EditText but I don't want the EditText itself to be editable. I've tried many things like setting its KeyListener to null, setting it's InputType to null, but what always happens is that the first click gives the View focus and the second click actually registers with my OnClickListener. So user has to click twice to get the menu to popup. Any ideas?
public class PopupEditText extends EditText implements OnClickListener
{
private PopupMenu mMenu;
private Context mContext;
public PopupEditText(Context context)
{
super(context);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
mContext = context;
setKeyListener(null);
mMenu = new PopupMenu(context, this);
}
#Override
public void onClick(View view)
{
mMenu.show();
}
You're not far off. I'd recommend just overriding onTouchEvent() directly, and responding only to ACTION_UP events. Optionally, force it to be disabled and non-focusable. For example:
public class UnmodifiableEditText extends EditText {
private PopupMenu mPopupMenu;
public UnmodifiableEditText(Context context) {
super(context);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
super.setEnabled(false);
super.setFocusable(false);
mPopupMenu = new PopupMenu(ctx, this);
}
#Override public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getContext(), "Showing Popup", Toast.LENGTH_SHORT).show();
mPopupMenu.show();
}
return true;
}
#Override public void setEnabled(boolean enabled) {
// Do not allow enabling the EditText
}
#Override public void setFocusable(boolean focusable) {
// Do not allow focusability changes
}
}
Related
I am trying to subclass TextInputLayout, in a CustomTextInputLayout. This is the code:
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextInputLayout(Context context) {
super(context);
}
public View getComponent(){
return this.getChildAt(0);
}
public void setHint(String hint){
super.setHint(hint);
}
}
I am using it to wrap another custom component, with a button and an EditText:
public class ComponentesSeleccion extends RelativeLayout {
private EditText editText;
private Button button;
public ComponentesSeleccion(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ComponentesSeleccion(Context context) {
super(context);
inicializar();
}
public ComponentesSeleccion(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.seleccionlayout, this, true);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button2);
}
public Button getButton() {
return button;
}
public EditText getEditText() {
return editText;
}
public void setInputType(int type){
editText.setInputType(type);
}
public void setImagen(Drawable drawable){
button.setBackgroundDrawable(drawable);
}
}
With this, I am not able to do in my code:
CustomTextInputLayout ctil=(CustomTextInputLayout)view.findViewById(R.id.ctil);
ctil.setHint(“a hint");
No exception, no errors, but the hint is not drawn. i can set the normal hint on the EditText, but I want the animated hint of the TextInputLayout.
How could I get it working?
Thank you.
I want to make a custom class based on ImageButton. Also, determine the action when click the button. But I regret to say the method onClick does not work in the class. What could be the problem?
public class CircleButton extends ImageButton implements View.OnClickListener{
private boolean checked;
public CircleButton(Context context) {
super(context);
init(context);
}
public CircleButton(Context context, AttributeSet set) {
super(context, set);
init(context);
}
public CircleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
#Override
public void onClick(View v) {
//setChecked(!isChecked());
Log.d("CircleButton", "setBackground()");
setBackground();
}
private void init (Context context) {
setOnClickListener(this);
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
setBackground();
}
private void setBackground() {
setBackgroundResource(R.drawable.ic_about);
}
protected void onDraw(Canvas canvas) {
}
}
i have the following code, and the problem, that my function rlposition() isn't available from outside the class.
public class RLbadge extends TextView {
public RLbadge(Context context) {
super(context);
this.setTypeface(null, Typeface.BOLD);
this.setTextColor(Color.WHITE);
this.setBackgroundResource(R.drawable.badge);
this.setTextSize(18);
}
public void rlposition(Button pButton) {
// THIS FUNCTION ISNT SEEN FROM OUTSIDE WHY?
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
Why is the function rlposition not visible from outside of the class?
Isn't it possible to add functionality to an extended TextView?
<YOURPACKAGENAME.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="TEASTING" />
Here is Class
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
// Do your staff
}
}
}
i now answered the question for myself and put it here maybe somebody needs the answer in the future.
The problem was this line of calling:
TextView badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // here the error comes
changed to
RLbadge badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // the function is visible
I have a class which extends LinearLayout. This class has a method called addSubview and the warning message "Method 'actionXYZ' is never used" shows, but the method have public modifier!
Furthermore, this method does not appear when this class is instantiated in a activity, for example.
Why?
public class dummyLayout extends RelativeLayout {
LayoutInflater mInflater;
RelativeLayout rootItem;
public dummyLayout(Context context) {
super(context);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context ctx) {
mInflater = LayoutInflater.from(ctx);
rootItem = (RelativeLayout) mInflater.inflate(R.layout.dummyLayout, this, true);
}
public void addSubView(View v) {
rootItem.addView(v);
}
}
I'm trying something like this
public class CustomViewSubclass extends HorizontalScrollView{
private LinearLayout layout;
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
// This is called from the `Activity`
public void startAsyncTask() { // code }
// This method is called in the `onPostExecute()` of an `AsyncTask` subclass
public void doSomething(Context context) {
ImageView image = ImageView(context);
layout.addView(image); // NullPointerException here, layout seems to be null
}
but it seems that layout on doSomething() is null. How is that even possible? I'm initializing it on the constructor... and I never re-initialize it again;
I'm adding my custom view via XML
<com.mypackage.CustomViewSubclass
android:layout_width="wrap_content"
android:layout_width="match_parent" />
Ok I fixed it, it was an stupid mistake made by me:
I used super() on the 3 methods, instead of using this().
public CustomViewSubclass(Context context) {
super(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
super(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}
Solution:
public CustomViewSubclass(Context context) {
this(context,null,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs) {
this(context,attr,0);
}
public CustomViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
layout = new LinearLayout(context);
}