0

i am creating an app that has a tabbed interface using TabHost and fragments, and the user needs to select the time using a TimePickerDialog and the selected time is populated into an EditText view, so i moved the time picker to a fragment so i can use it in multiple tabs which works but the problem is when i set the time using the dialog EditText view that is populated is the one in the last tab not the one the TimePickerDialog was opened from, how do i deal with this?

here is the layout for time picker fragment

<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"
         tools:context="com.ztech.fakecallm.TimePickerFragment">
<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="time"
        android:ems="10"
        android:id="@+id/timePicker"
        android:focusable="false"
        android:hint="@string/time"
        android:onClick="onClickTime"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

and here is the code that populates the EditText view which is stored in MainActivity.java

public void onClickTime(View view) {

    final Calendar c = Calendar.getInstance();

    int cHour = c.get(Calendar.HOUR_OF_DAY);

    int cMinute = c.get(Calendar.MINUTE);

    TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            c.set(Calendar.HOUR_OF_DAY, hourOfDay);

            c.set(Calendar.MINUTE, minute);

            scheduleDate = c.getTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a", Locale.US);

            EditText timeInput = (EditText)findViewById(R.id.timePicker);

            timeInput.setText(dateFormat.format(scheduleDate));

        }
    }, cHour, cMinute, false);

    timePickerDialog.show();

}

and here is the layout of the fragment that represents the first tab

<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"
         tools:context="com.ztech.fakecallm.CallsFragment">
<fragment
        android:id="@+id/contact_picker_frag"
        class="com.ztech.fakecallm.ContactPickerFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_contact_picker"/>
<RadioGroup android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:id="@+id/radioGroup"
            android:layout_marginTop="137dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true">
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/incoming"
            android:id="@+id/incomingRadioButton"
            android:checked="true"
            android:layout_margin="20dp"/>
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/outgoing"
            android:id="@+id/outgoingRadioButton"
            android:checked="false"
            android:layout_margin="20dp"/>
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/missed"
            android:id="@+id/missedRadioButton"
            android:checked="false"
            android:layout_margin="20dp"/>
</RadioGroup>

<fragment
        android:id="@+id/time_picker_fragment_calls"
        class="com.ztech.fakecallm.TimePickerFragment"
        tools:layout="@layout/fragment_time_picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/radioGroup"
        android:layout_alignParentStart="true"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/schedule"
        android:id="@+id/setCallScheduleButton"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"/>


</RelativeLayout>

and layout for the fragment that represents the second tab

 <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"
         tools:context="com.ztech.fakecallm.SMSFragment">

<fragment
        android:id="@+id/contact_picker_frag_sms"
        class="com.ztech.fakecallm.ContactPickerFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_contact_picker"/>

<EditText
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:id="@+id/message_input"
        android:gravity="top"
        android:hint="@string/message"
        android:layout_marginTop="140dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"/>

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:id="@+id/radioGroup"
        android:layout_below="@+id/message_input"
        android:layout_alignParentStart="true">
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/inbox"
            android:id="@+id/inbox_radio_button"
            android:checked="true"
            android:layout_margin="20dp"/>
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sent"
            android:id="@+id/sent_radio_button"
            android:checked="false"
            android:layout_margin="20dp"/>
</RadioGroup>

<fragment
        android:id="@+id/time_picker_fragment_sms"
        class="com.ztech.fakecallm.TimePickerFragment"
        tools:layout="@layout/fragment_time_picker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/radioGroup"
        android:layout_alignParentStart="true"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/schedule"
        android:id="@+id/setSMSScheduleButton"
        android:layout_margin="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"/>

 </RelativeLayout>

and after all that MainActivity looks like this

package com.ztech.fakecallm;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.provider.ContactsContract.CommonDataKinds.*;

import java.text.SimpleDateFormat;
import java.util.*;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import com.ztech.fakecallm.CallsFragment;
import com.ztech.fakecallm.SMSFragment;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private static final int PICK_CONTACT = 1001;
    Date scheduleDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new CallsFragment(), "Calls");
    adapter.addFragment(new SMSFragment(), "SMS");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}


public void onClickTime(View view) {

    final Calendar c = Calendar.getInstance();

    int cHour = c.get(Calendar.HOUR_OF_DAY);

    int cMinute = c.get(Calendar.MINUTE);

    TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            c.set(Calendar.HOUR_OF_DAY, hourOfDay);

            c.set(Calendar.MINUTE, minute);

            scheduleDate = c.getTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a", Locale.US);

            EditText timeInput = (EditText)findViewById(R.id.timePicker);

            timeInput.setText(dateFormat.format(scheduleDate));

        }
    }, cHour, cMinute, false);

    timePickerDialog.show();

}

public void onClickSelectContact(View view) {

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

    startActivityForResult(intent, PICK_CONTACT);

}

}
zola
  • 5,737
  • 8
  • 33
  • 48

1 Answers1

0

use this

enter link description here

   dependencies {
  compile 'com.wdullaer:materialdatetimepicker:2.3.0'
}

and

Implement your fragment an OnTimeSetListener

and in fragment to show TimePickerDialog

Calendar now2 = Calendar.getInstance();
                TimePickerDialog tpd = TimePickerDialog.newInstance(
                        NewActivity.this, now2.get(Calendar.HOUR_OF_DAY), now2.get(Calendar.MINUTE),
                        now2.get(Calendar.SECOND), false
                );
                tpd.show(getFragmentManager(), "Select Time");

And

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
  String time = "You picked the following time: "+hourOfDay+"h"+minute;
  timeTextView.setText(time);
}
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35