Android text to speech not working on mobile - android

i am having problem in text to speech. have this code in java class
package com.techblogon.loginexample;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class listenActivity extends Activity implements OnClickListener, OnInitListener {
private TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
tts = new TextToSpeech(this, this);
findViewById(R.id.button1).setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (tts!=null) {
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();
if (text!=null) {
if (!tts.isSpeaking()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
#Override
public void onInit(int code) {
if (code==TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
tts = null;
Toast.makeText(this, "Failed to initialize TTS engine.", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDestroy() {
if (tts!=null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
and xml file has this code
<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=".listenActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TTS Demonstration" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="Say It" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:ems="10"
android:text="Hello world!" />
</RelativeLayout>
but issue is when i click button no sound heard on my mobile. does i need any permission for that
any help
Many thanks

Related

Custom EditTextDialog is broken in Android 4.3

I'm currently working on an application and I have and unwanted behavior on my custom EditText Dialog. Here's the XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText_dialog"
android:layout_width="fill_parent"
android:inputType="textCapSentences"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_dialog"
android:id="#+id/cancel_dialog"
android:textSize="20sp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:layout_toLeftOf="#+id/valid_dialog" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/validate_dialog"
android:id="#+id/valid_dialog"
android:textSize="20sp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:textColor="#android:color/holo_blue_light"
android:layout_alignParentRight="true" />
</RelativeLayout>S7
</LinearLayout>
And here is the class for the TextEditDialog
package com.fitme.staffbooker;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
public class EditTextDialog extends Dialog {
private EditText editText;
public boolean dismissed = false;
public EditTextDialog(Context context, String title) {
super(context, android.R.style.Theme_Holo_Light_Dialog);
setTitle(title);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);
editText = (EditText) findViewById(R.id.editText_dialog);
findViewById(R.id.valid_dialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismissed = true;
dismiss();
}
});
findViewById(R.id.cancel_dialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
}
public void setHint(String str) {
editText.setHint(str);
}
public void setText(String str) {
editText.setText(str);
}
public void setInputType(int type) {
editText.setInputType(type);
}
public String getValue() {
return editText.getText().toString();
}
}
Here's the output i'm having on my Galaxy S7 Edge :
But when I'm running my app on a Galaxy SIII Emulator it seems broken :
It seems that this only happens on 4.3 and below. I have no idea what is happening.

Unable to run multiple bluetooth connection

this the main activity java file of my code
package com.ramimartin.sample.multibluetooth;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ToggleButton;
import com.ramimartin.multibluetooth.activity.BluetoothFragmentActivity;
import com.ramimartin.multibluetooth.bluetooth.mananger.BluetoothManager;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class MainActivity extends BluetoothFragmentActivity implements DiscoveredDialogFragment.DiscoveredDialogListener {
#InjectView(R.id.listview)
ListView mListView;
ArrayAdapter<String> mAdapter;
List<String> mListLog;
#InjectView(R.id.communication)
EditText mEditText;
#InjectView(R.id.send)
ImageButton mSendBtn;
#InjectView(R.id.client)
ToggleButton mClientToggleBtn;
#InjectView(R.id.serveur)
ToggleButton mServerToggleBtn;
#InjectView(R.id.connect)
Button mConnectBtn;
#InjectView(R.id.disconnect)
Button mDisconnect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mListLog = new ArrayList<String>();
mAdapter = new ArrayAdapter<String>(this, R.layout.item_console, mListLog);
mListView.setAdapter(mAdapter);
}
#Override
public int myNbrClientMax() {
return 7;
}
#OnClick(R.id.serveur)
public void serverType() {
setLogText("===> Start Server ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_3600_SEC);
selectServerMode();
mServerToggleBtn.setChecked(true);
mClientToggleBtn.setChecked(false);
mConnectBtn.setEnabled(true);
mConnectBtn.setText("Scan Devices");
}
#OnClick(R.id.client)
public void clientType() {
setLogText("===> Start Client ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_120_SEC);
selectClientMode();
mServerToggleBtn.setChecked(false);
mClientToggleBtn.setChecked(true);
mConnectBtn.setEnabled(true);
}
#OnClick(R.id.connect)
public void connect() {
setLogText("===> Start Scanning devices ...");
if (getTypeBluetooth() == BluetoothManager.TypeBluetooth.Client) {
showDiscoveredDevicesDialog();
}
scanAllBluetoothDevice();
}
#OnClick(R.id.disconnect)
public void disconnect() {
setLogText("===> Disconnect");
disconnectClient();
disconnectServer();
}
#OnClick(R.id.send)
public void send() {
if (isConnected()) {
sendMessage(mEditText.getText().toString());
setLogText("===> Send : " + mEditText.getText().toString());
}
}
#Override
public void onBluetoothStartDiscovery() {
setLogText("===> Start discovering ! Your mac address : " + mBluetoothManager.getYourBtMacAddress());
}
#Override
public void onBluetoothDeviceFound(BluetoothDevice device) {
if(getTypeBluetooth() == BluetoothManager.TypeBluetooth.Server) {
setLogText("===> Device detected and Thread Server created for this address : " + device.getAddress());
}else{
setLogText("===> Device detected : "+ device.getAddress());
}
}
#Override
public void onClientConnectionSuccess() {
setLogText("===> Client Connexion success !");
mEditText.setText("Client");
mSendBtn.setEnabled(true);
mConnectBtn.setEnabled(false);
mDisconnect.setEnabled(true);
}
#Override
public void onClientConnectionFail() {
setLogText("===> Client Connexion fail !");
mServerToggleBtn.setChecked(false);
mClientToggleBtn.setChecked(false);
mDisconnect.setEnabled(false);
mConnectBtn.setEnabled(false);
mConnectBtn.setText("Connect");
mEditText.setText("");
}
#Override
public void onServeurConnectionSuccess() {
setLogText("===> Serveur Connexion success !");
mEditText.setText("Server");
mDisconnect.setEnabled(true);
}
#Override
public void onServeurConnectionFail() {
setLogText("===> Serveur Connexion fail !");
}
#Override
public void onBluetoothCommunicator(String messageReceive) {
setLogText("===> receive msg : " + messageReceive);
}
#Override
public void onBluetoothNotAviable() {
setLogText("===> Bluetooth not aviable on this device");
mSendBtn.setEnabled(false);
mClientToggleBtn.setEnabled(false);
mServerToggleBtn.setEnabled(false);
mConnectBtn.setEnabled(false);
mDisconnect.setEnabled(false);
}
public void setLogText(String text) {
mListLog.add(text);
mAdapter.notifyDataSetChanged();
mListView.setSelection(mListView.getCount() - 1);
}
private void showDiscoveredDevicesDialog() {
String tag = DiscoveredDialogFragment.class.getSimpleName();
DiscoveredDialogFragment fragment = DiscoveredDialogFragment.newInstance();
fragment.setListener(this);
showDialogFragment(fragment, tag);
}
private void showDialogFragment(DialogFragment dialogFragment, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialogFragment, tag);
ft.commitAllowingStateLoss();
}
#Override
public void onDeviceSelectedForConnection(String addressMac) {
setLogText("===> Connect to " + addressMac);
createClient(addressMac);
}
#Override
public void onScanClicked() {
scanAllBluetoothDevice();
}
}
****************************************************************************************************************************************************
here is layout of the application
stackoverflow does not allow me to ask question , i am beginner , very tough to ask question plz try to find out error
ur response will be appreciated
<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:background="#android:color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/rami_logo"
android:layout_centerHorizontal="true"/>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/communication"
android:divider="#android:color/transparent"
android:layout_alignParentTop="true">
</ListView>
<EditText
android:id="#+id/communication"
android:layout_above="#+id/connexion"
android:singleLine="true"
android:layout_toLeftOf="#+id/send"
android:layout_marginBottom="2dp"
android:textColor="#android:color/white"
android:dividerHeight="0dip"
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageButton
android:id="#+id/send"
android:layout_above="#+id/connexion"
android:layout_alignTop="#+id/communication"
android:layout_alignParentRight="true"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:src="#drawable/send"/>
<LinearLayout
android:id="#+id/connexion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/connect"
android:padding="2dp">
<ToggleButton
android:id="#+id/client"
android:layout_weight="1"
android:enabled="true"
android:textColor="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Client"
android:textOn="Client"/>
<ToggleButton
android:id="#+id/serveur"
android:layout_weight="1"
android:enabled="true"
android:textColor="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Serveur"
android:textOn="Serveur"/>
</LinearLayout>
<Button
android:id="#+id/connect"
android:layout_weight="1"
android:layout_margin="2dp"
android:enabled="false"
android:textColor="#android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/disconnect"
android:text="Connect"/>
<Button
android:id="#+id/disconnect"
android:layout_margin="2dp"
android:enabled="false"
android:textColor="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disconnect"/>
</RelativeLayout>
log errorerror log

Make EditText callable across Activitys

I'm trying to call an EditText from one activity to another but when i do my app crashes.
I've tried using the addPreferencesFromResource method using 'import android.preference.PreferenceActivity' and import android.preference.PreferenceFragment' but the 'addPreferencesFromResource' stays red.
How can I make it callable ?
settings_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:id="#+id/linearLayout">
<TextView
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="website A"
android:id="#+id/textView3"
android:layout_weight="0.07"
android:layout_marginTop="115dp"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:onClick="showZaire"
android:id="#+id/button5"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/webA"
android:layout_alignBottom="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
<TextView
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="website B"
android:id="#+id/textView2"
android:layout_weight="0.07"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/webB"
android:layout_alignBottom="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_toEndOf="#+id/textView2" />
</LinearLayout
Second Activity
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class secondActivity extends Activity {
EditText webs = (EditText)findViewById(R.id.webA);// this is what im trying to call from settings
String web = webs.getText().toString();
TextView titleText;
String title = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seccond_layout);
Button button = (Button) findViewById(R.id.refresh);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
restartActivity(seccondActivity.this);
}
});
//initialize variables
titleText = (TextView) findViewById(R.id.titleText);
;
//run on new thread because we cannot do network operation on main thread
new Thread(new Runnable() {
#Override
public void run() {
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect(web).get(); // this is the website string
//Get the title of blog using title tag
title = document.select("h6").text().toString();
//set the title of text view
//Run this on ui thread because another thread cannot touch the views of main thread
runOnUiThread(new Runnable() {
#Override
public void run() {
//set both the text views
titleText.setText(title);
titleText.setMovementMethod(new ScrollingMovementMethod());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void showZaire(View view) {
String button_test;
button_test = ((Button) view).getText().toString();
if (button_test.equals("Home")) {
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
} else if (button_test.equals("Directions")) {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
}
settingsActivity
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.text.Editable;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Shane on 10/01/2016.
*/
public class SettingsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);}
public void showZaire(View view) {
String button_test;
button_test = ((Button) view).getText().toString();
if (button_test.equals("Back")) {
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
} else if (button_test.equals("Help")) {
Intent intent2 = new Intent(this, Help.class);
startActivity(intent2);
}
}
}

Android TextToSpeech doesn't work

I would like to use TTS in my application. First of all I'm trying to run some examples, but every time TTS doesn't work on my device. In example I have the code:
MainActivity:
package com.example.tts;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
private TextToSpeech textToSpeech;
private Button button;
private EditText inputText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
inputText = (EditText) findViewById(R.id.inputText);
textToSpeech = new TextToSpeech(this, this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
convertTextToSpeech();
}
});
convertTextToSpeech();
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("error", "This Language is not supported");
} else {
convertTextToSpeech();
}
} else {
Log.e("error", "Initilization Failed!");
}
}
#Override
public void onDestroy() {
textToSpeech.shutdown();
super.onDestroy();
}
private void convertTextToSpeech() {
//String text = "test";
String text = inputText.getText().toString();
if (null == text || "".equals(text)) {
text = "Please give some input.";
}
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
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"
tools:context=".TextToSpeechActivity" >
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="28dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/inputText"
android:layout_below="#+id/inputText"
android:layout_marginTop="14dp"
android:text="Speak" />
</RelativeLayout>
After typing the word and clicking button application says nothing.
Does anyone have an idea how to solve it? Thanks in advance.

TimePicker + CheckBox

I've got a TimePicker in my app and i want to add above it a CheckBox that if it checked the TimePicker won't work. This is my TimePicker: http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker.
How can I do that? I want the timePicker to look the same as the example.
I will put here the entire project also for others to use it if they ever need it:
Short description: A program that let's you get the time set by user and show it in the bottom of the page, after you press Done button.Used intent to get from one activity to another.
--------Main Activity-------
package com.example.chxboxtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button start_intent_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start_intent_button = (Button) findViewById(R.id.start_intent_button);
start_intent_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_intent_button: {
Intent intent = new Intent(this,TimePickerTest.class);
startActivity(intent);
}
break;
}
}
}
-------------Main Activity 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"
>
<Button
android:id="#+id/start_intent_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Intent Time Picker"
android:layout_centerInParent="true"
/>
</RelativeLayout>
-------------TimePicker class ---------------
package com.example.chxboxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerTest extends Activity implements OnClickListener{
private CheckBox cBox;
private TimePicker tPicker;
private TextView showTime;
private Button done;
private String hour;
private String minute;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_picker_layout);
initButtons();
}
private void initButtons() {
tPicker = (TimePicker) findViewById(R.id.time_picker);
showTime = (TextView) findViewById(R.id.get_time);
done = (Button)findViewById(R.id.done);
cBox = (CheckBox) findViewById(R.id.time_picker_checkbox);
cBox.setOnClickListener(this);
done.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
//If check enable or disble timePicker
case R.id.time_picker_checkbox: {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "CHECKED", Toast.LENGTH_SHORT).show();
tPicker.setEnabled(false);
} else {
Toast.makeText(this, "NOT CHECKED", Toast.LENGTH_SHORT).show();
tPicker.setEnabled(true);
}
}
break;
//If Done button pressed get time selected by user
case R.id.done:{
tPicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
hour = tPicker.getCurrentHour().toString();
minute = tPicker.getCurrentMinute().toString();
if(tPicker.getCurrentMinute().intValue() < 10){
String setTimeText = hour+ " : " + "0" + minute;
showTime.setText(setTimeText);
}else{
String setTimeText = hour+ " : " + minute;
showTime.setText(setTimeText);
}
}
break;
default:
break;
}
}
}
-------------TimerPicker XML------------
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/timer_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
android:layout_toRightOf="#+id/time_picker_checkbox"
android:text="Check this to Cancel Alarm" />
<CheckBox
android:id="#+id/time_picker_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
android:layout_centerHorizontal="true" />
<TimePicker
android:id="#+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_picker"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Done" />
<TextView
android:id="#+id/get_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="Time"
android:textColor="#FF0000"
android:textSize="25sp" />
</RelativeLayout>
-------------Manifest.xml-----------
**<activity
android:name="com.example.chxboxtest.TimePickerTest"
android:configChanges="orientation|keyboardHidden">
</activity>**
Dont forget to add TimePicker class as activity.
BestPracice add all xml text in strings.xml
Cheers
Here's an functional example with all your requirements.
Tested and 100% functional :)
Put this in 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"
>
<TextView
android:id="#+id/timer_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check this to Cancel Alarm"
android:layout_above="#+id/time_picker"
android:layout_toRightOf="#+id/time_picker_checkbox"
/>
<CheckBox
android:id="#+id/time_picker_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/time_picker"
/>
<TimePicker
android:id="#+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
And Here is your java code:
package com.example.chxboxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private CheckBox cBox;
private TimePicker tPicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initButtons();
}
private void initButtons() {
tPicker = (TimePicker) findViewById(R.id.time_picker);
cBox = (CheckBox) findViewById(R.id.time_picker_checkbox);
cBox.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.time_picker_checkbox: {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "Deactivate",Toast.LENGTH_SHORT).show();
tPicker.setEnabled(false);
} else {
Toast.makeText(this, "Activate",Toast.LENGTH_SHORT).show();
tPicker.setEnabled(true);
}
}
break;
default:
break;
}
}
}
If you want this example in a custom dialog please let me know.
Cheers,

Categories

Resources