w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Time picker only showing current time not setting time according to user in textview

Use a DialogFragment and a interface as a call back to the activity and set the time to textview.

Modify the below according to your requirement.

activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="180dp"
        android:text="TextView" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements Returndata
{
    TextView tv;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                DialogFragment newFragment = new TimePickerFragment();
                newFragment.show(getFragmentManager(), "timePicker");

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is
present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void setText(String result) {
        // TODO Auto-generated method stub
        tv.setText(result);
    }

}

TimePickerFragment

public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
String AM_PM;
Returndata rd;
    public interface Returndata{
        void setText(String result);
    }
@Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        rd= (Returndata) activity;
    }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
Log.i("...............","..........."+hour+"......."+minute);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
     if(hourOfDay < 12) {
         AM_PM = "AM";
     } else {
         AM_PM = "PM";
     }
    StringBuilder sb = new StringBuilder();
    sb.append(hourOfDay);
    sb.append(":");
    sb.append(minute);
    sb.append(AM_PM);
    rd.setText(sb.toString());
}
}

Snap

enter image
description here





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.