0

I am trying to mimic the new Oreo Timepicker

I have created the layout:

<?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="horizontal">

<LinearLayout
    android:id="@+id/hour_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/hour_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="2"
        android:textAlignment="center"
        android:focusable="true"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="hour" />
</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:paddingBottom="10dp"
    android:text=":"
    android:textSize="24sp" />

<LinearLayout
    android:id="@+id/minute_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/minute_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="2"
        android:textAlignment="center" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="minute" />
</LinearLayout>

<Spinner
    android:id="@+id/ampm_spinner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_weight="1" />

I also created the supporting class(setters and getters not included below:

public class ManualTimePicker extends LinearLayout {
private final static String TAG = "ManualTimePicker():";

TextView hour;
TextView minute;
Spinner ampm;
private Integer fieldInputType;

public ManualTimePicker(Context context) {
    super(context);
    init(context);
}

public ManualTimePicker(Context context, @Nullable AttributeSet  
 attributeSet) {
    super(context, attributeSet);

    TypedArray attrs = context.obtainStyledAttributes
 (attributeSet, R.styleable.ManualTimePicker, 0, 0);

    fieldInputType = attrs.getInt
 (R.styleable.ManualTimePicker_H_android_text,InputType.TYPE_CLASS_NUMBER);

    attrs.recycle();
    init(context);
}

public ManualTimePicker
 (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

public ManualTimePicker
 (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init(final Context context) {


    View.inflate(context, R.layout.manual_time_picker, this);
    setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

    hour = findViewById(R.id.hour_input);
    minute = findViewById(R.id.minute_input);
    hour.setText("7");
    minute.setText("00");

    hour.setFocusable(true);

    hour.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "Hour field is clicked");
            hour.requestFocus();
        }
    });


    hour.setInputType(fieldInputType);



    ampm = findViewById(R.id.ampm_spinner);
    final ArrayAdapter methodAdapter = 
ArrayAdapter.createFromResource(getContext(), 
R.array.ampm,android.R.layout.simple_spinner_item);






methodAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
    ampm.setAdapter(methodAdapter);

    SimpleDateFormat dateFormat = new SimpleDateFormat("aa");
    String formattedDate = dateFormat.format(new Date());

    if(formattedDate.equals("AM"))
        ampm.setSelection(0);
     else
        ampm.setSelection(1);
}

@Override
public boolean onTouchEvent(final MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){
        return performClick();
    }
    return true;
}

Now I have the problem that the EditTexts does not work at all - when I click them, they gain focus for a second, I see a blink from the cursor and then the go "dark" again. requestFocus() does not work either.

How can I make those two edittext act like just normal edit texts? I implemented getters and setters, so it won't be a problem to get and set the value, but I don't see them acting properly.

Thanks!

Milo
  • 3,365
  • 9
  • 30
  • 44

2 Answers2

1

change this to

TextView hour;
TextView minute;

hour = findViewById(R.id.hour_input);
minute = findViewById(R.id.minute_input);

this.

EditText hour;
EditText minute;

hour = (EditText)findViewById(R.id.hour_input);
minute = (EditText)findViewById(R.id.minute_input);
0

I removed

setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

And that solved the issues. It seems that I didn't read properly the tutorial I was using. Here is what it does - Can someone explain descendantFocusability = afterDescendants?