A Simple SeekBar Program is not running.I'm not sure what i'm missing.
The java file is given as
package com.tree;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class tree extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SeekBar sb=(SeekBar)findViewById(R.id.sb);
final TextView tv=new TextView(this);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
tv.setText(progress);
}
});
}
}
and the layout xml file is given as
Any help would be appreciated.The error thrown is "that the Program has stopped unexpectedly".
The problem is in tv.setText(progress);.
setText(int) expects a resource identifier, not an integer.
Instead use tv.setText(Integer.toString(progress)); to convert to a string first.
Related
I am trying to define setOnSeekBarChangeListener method which gives me an error to implement OnStopTouchTracking meethod. When I try to override it it throws an error saying cannot override method from superclass.
Am I missing any libraries to include for seekbar event listeners? or should the class be declared abstract?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
public class task4_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
setContentView(R.layout.task4_layout);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void OnstartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void OnstopTrackingTouch(SeekBar seekBar)
{
}
});
}
}
check methods inside listener, all methods are wrong.
It should be onStartTrackingTouch instead of OnstartTrackingTouch
It should be onStopTrackingTouch instead of OnstopTrackingTouch
you have written capital letter in starting of all the methods.
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Toast.makeText(this, String.valueOf(progress), Toast.LENGTH_SHORT).show();
changeColor(viewOne, progress);
changeColor(viewTwo, progress);
changeColor(viewThree, progress);
changeColor(viewFour, progress);
changeColor(viewFive, progress);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
I am trying to update the value of the seekbar and display it in a toast. however, the toast updates the value extremely slowly. I would not like to use a textbox. Is there a way to accomplish this?
Using popup window.You can customize the view of popup window, like a textview to show the progress.
In method onStartTrackingTouch, show the popup window.
In method onStopTrackingTouch, dismiss the popup window.
In method onProgressChanged, change the progress.
Sample code:
package com.example.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private SeekBar seek;
private PopupWindow window;
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar) findViewById(R.id.seek);
seek.setOnSeekBarChangeListener(this);
window = new PopupWindow(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textview = new TextView(this);
textview.setTextSize(16);
textview.setBackgroundColor(Color.BLACK);
textview.setTextColor(Color.WHITE);
window.setContentView(textview);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
textview.setText("progress : " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.showAsDropDown(seek);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
window.dismiss();
}
}
Hope it helps.
Declare the toast at class level
Toast mToast;
Initialize the toast in constructor or onCreate
protected void onCreate(Bundle savedInstanceState) {
...
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
then when you need to show toast
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mToast.setText("progress : " + progress);
mToast.show();
}
now your toast updates will be instantaneous.
In my activity I am trying to play a video which is stored in raw folder. below is my activity.
In the first button I am not getting error also like can't play this video, but only black screen appears. while clicking the second button I am getting a message that can't play this video. below is my activity
package com.example.college;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class FirstYear extends Activity {
Button cse,it,ece,eee;
VideoView vv;
MediaController mc;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_year);
cse=(Button)findViewById(R.id.button1);
it=(Button)findViewById(R.id.button2);
ece=(Button)findViewById(R.id.button3);
eee=(Button)findViewById(R.id.button4);
vv=(VideoView)findViewById(R.id.videoView1);
mc=new MediaController(this);
mc.setAnchorView(vv);
cse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.c));
vv.start();
}
});
it.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.k));
vv.start();
}
});
ece.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.a));
vv.start();
}
});
eee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.s));
vv.start();
}
});
}
}
The code is correct. All the errors are related to video resolution.
I have replaced video with different resolution , and it is working
My code for implementing speech recognition using SpeechRecognizer class seem correct....yet it does not function whatsoever. None of the log statements in any of the RecognitionListener methods are being logged.
I have follow the Android reference docs on this quite carefully and I am not seeing what I am missing.
I have added the record audio permission.
package com.example.voicetest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class VoiceActivity extends Activity {
SpeechRecognizer recognizer;
TextView display;
private boolean listening;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
listening=false;
display=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
recognizer=SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(
new RecognitionListener()
{
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onBeginning");
}
#Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
Log.i("test","onBuffer");
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onEndOfSpeech");
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
Log.i("test","onError");
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
Log.i("test","onPartial");
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
display.setText("Ready and waiting..");
Log.i("test","onReady");
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
Log.i("test","onResults");
String result=stringListToString(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
display.setText(result);
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.voice, menu);
return true;
}
public void buttonOnClick(View v)
{
if(!listening)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
recognizer.startListening(intent);
Log.i("test","recognizer listening");
listening=true;
button1.setText("Stop Listening");
}
else
{
recognizer.stopListening();
listening=false;
Log.i("test","recognizer stopped");
button1.setText("Listen");
}
}
private String stringListToString(ArrayList<String> strings)
{
String result="";
for(int i=0;i<strings.size();i++)
{
result=result+strings.get(i)+'\n';
}
return result;
}
#Override
public void onPause()
{
recognizer.stopListening();
}
}
You also need
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
Try the following:
Log out the error code. If it is giving server error then you are not connected to the internet.
If you want to use offline speech recognition, you need language models downloaded. Just verify that Speech input works on your keyboard (Press the mic button, say something and see if it is getting converted). If it is not getting recognized, then there is no error with your code, you just need to download speech models.
I am developing a Keyboard. When i am inflating a ListView in onCreateInputView() and returning same View(or parent View) to it. I have implemented setOnItemClickListener for the ListView i am not getting callback to it.
What might be the issue?
android framework doesn't work for listview for keyboard(InputMethodService) ?
FY Reference i am attaching the code please have look at it.
NOTE: i am able to get ontouchlistner of it.
package com.listkeyboard;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListKeyBoardIME extends InputMethodService {
private LinearLayout mainLayout;
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("onCreate");
}
#Override
public View onCreateCandidatesView() {
// TODO Auto-generated method stub
System.out.println("onCreateCandidatesView");
return super.onCreateCandidatesView();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
#Override
public void onFinishInput() {
// TODO Auto-generated method stub
super.onFinishInput();
System.out.println("onFinishInput");
}
#Override
public void onFinishInputView(boolean finishingInput) {
// TODO Auto-generated method stub
super.onFinishInputView(finishingInput);
System.out.println("onFinishInputView");
}
#Override
public View onCreateInputView() {
// TODO Auto-generated method stub
System.out.println(" onCreateInputView");
LayoutInflater layoutInflater = (LayoutInflater) ListKeyBoardIME.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mMainKeyboardLayout = layoutInflater.inflate(R.layout.keyboard_list_view, null);
mainLayout = (LinearLayout)mMainKeyboardLayout.findViewById(R.id.main_layout);
ListView listView = (ListView)mainLayout.findViewById(R.id.list);
ArrayList<Character> alphabets = new ArrayList<Character>();
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabets.add(ch);
if (ch == 'n') {
break;
}
}
ArrayAdapter<Character> adapter = new ArrayAdapter<Character>(ListKeyBoardIME.this, android.R.layout.simple_list_item_1,alphabets);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "on itemclicked clicked" , Toast.LENGTH_SHORT).show();
}
});
setInputView(mainLayout);
return mainLayout;
}
#Override
public void onStartCandidatesView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartCandidatesView(info, restarting);
System.out.println("onStartCandidatesView ");
}
#Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInput(attribute, restarting);
System.out.println("onStartInput ");
}
#Override
public void onStartInputView(EditorInfo info, boolean restarting) {
// TODO Auto-generated method stub
super.onStartInputView(info, restarting);
System.out.println("onStartInputView");
}
}
I am also face the same problem & after lot of searching when no answer work for me, i implement below code in my list adapter & its work for me. If anyone find any other better answer please let us know.
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});