Android - Changing colours in override methods - java

I'm making a drawing app. I have two brushes (img_brush and img_brush2). The initial colour is set to black in PaintView() (on the line 'this.colour = Color.BLACK;'). What I want to do is set different colours for both brushes in the override '(onClick) methods, for example blue for img_brush and red for img_brush2, how can I do it? This is the code I have got:
public class Draw_Activity extends Activity {
private PaintView pv;
protected View layout;
protected int progress;
protected Dialog dialog;
protected Dialog textdialog;
private String[] arrImagesStrings;
protected float stroke = 6;
int postion;
private String fileName;
private int ColorAh = Color.BLACK;
String Foldername;
RelativeLayout re;
private AdView mAdView;
ImageView img_brush, img_brush2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawpic_activity);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
re = (RelativeLayout) findViewById(R.id.rell);
img_brush = (ImageView) findViewById(R.id.image_brush);
img_brush2 = (ImageView) findViewById(R.id.image_brush2);
this.fileName = null;
super.onCreate(savedInstanceState);
this.pv = new PaintView(this);
re.addView(pv);
//setContentView(this.pv);
this.pv.togglePencil(true);
Intent i = getIntent();
Foldername = i.getStringExtra("Folder");
img_brush.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Draw_Activity.this.pv.togglePencil(true);
}
});
img_brush2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Draw_Activity.this.pv.togglePencil(true);
}
});
}
//for color
private void showColorPickerDialogDemo() {
int initialColor = Color.WHITE;
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(Draw_Activity.this, initialColor, new OnColorSelectedListener() {
public void onColorSelected(int color) {
Draw_Activity.this.pv.setColor(color);
}
});
colorPickerDialog.show();
}
private PaintView(Context c) {
super(c);
setDrawingCacheEnabled(true); // to save images
this.context = c;
this.colour = Color.BLACK;
this.path = new Path();
this.bmpPaint = new Paint();
this.paint = new Paint();
this.paint.setAntiAlias(true);
this.paint.setDither(true);
this.paint.setColor(this.colour);
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setStrokeJoin(Paint.Join.ROUND);
this.paint.setStrokeCap(Paint.Cap.ROUND);
this.paint.setStrokeWidth(50);
this._paintBlur = new Paint();
this._paintBlur.set(paint);
this._paintBlur.setAntiAlias(true);
this._paintBlur.setDither(true);
this._paintBlur.setStyle(Paint.Style.STROKE);
this._paintBlur.setStrokeJoin(Paint.Join.ROUND);
this._paintBlur.setStrokeCap(Paint.Cap.ROUND);
this._paintBlur.setColor(Color.parseColor("#FA2B2B"));
this._paintBlur.setStrokeWidth(6.0F);
this._paintBlur.setMaskFilter(new BlurMaskFilter(10.0F, BlurMaskFilter.Blur.OUTER));
this.bitmapforground = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
this.bitmapforground = BitmapFactory.decodeResource(getResources(),
R.color.textbg);
}
public void setColor(int c) {
this.paint.setColor(c);
this.colour = c;
}
protected int getColor() {
return this.colour;
}
}

Related

How can I do AsyncLayoutInflater onCreate

The content part of the android application consisted of a lot of code structure. (3000 lines) I divided them into 3 viewstubs. I started 3 handler nested and then inflated the viewstubs.
But it did not accelerate again. it still opens very slowly. Now, while researching, I encountered asyntasklayoutinflater, but I think I couldn't do this job properly. No view appears in the content. // I deleted setContentView I also want to move my viewstubs into asynctask, but it is not static, how can I do this? Could you help?
Thanks in advance!
ViewStub viewStub1;
ViewStub viewStub2;
ViewStub viewStub3;
View coachStub1;
View coachStub2;
View coachStub3;
#SuppressLint("InflateParams")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
new AsyncLayoutInflater(this).inflate(R.layout.activity_main, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
#Override
public void onInflateFinished(#NonNull View view, int resid, #Nullable ViewGroup parent) {
long start = System.currentTimeMillis();
viewStub1 = view.findViewById(R.id.viewStub1);
viewStub2 = view.findViewById(R.id.viewStub2);
viewStub3 = view.findViewById(R.id.viewStub3);
viewStub1.setLayoutResource(R.layout.viewstub1);
coachStub1 = viewStub1.inflate();
viewStub2.setLayoutResource(R.layout.viewstub2);
coachStub2 = viewStub2.inflate();
viewStub3.setLayoutResource(R.layout.viewstub3);
coachStub3 = viewStub3.inflate();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Objects.requireNonNull(notificationManager).cancelAll();
initialize(view);
new MainAsyncTask(MainActivity.this).execute();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
VKIPagerAdapter = new MyViewPagerAdapter();
vkipager.setAdapter(VKIPagerAdapter);
VKIPagerAdapter.notifyDataSetChanged();
vkipager.setOffscreenPageLimit(10);
vkipager.addOnPageChangeListener(viewPagerPageChangeListener);
long finish = System.currentTimeMillis();
Log.d("Handler Displayed", "\t" + (finish - start));
}
}, 100);
menu1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
///
}
});
inters_ad1 = new InterstitialAd(context);
inters_ad2 = new InterstitialAd(context);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (blink_settings && !MainActivity.this.isFinishing()) {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(500);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
profile.startAnimation(anim);
}
}
}, 8000);
}
});
}
AsyncTask
private static class MainAsyncTask extends AsyncTask<Integer, Integer, String> {
private WeakReference<MainActivity> activityWeakReference;
MainAsyncTask(MainActivity activity) {
activityWeakReference = new WeakReference<>(activity);
}
#Override
protected String doInBackground(Integer... integers) {
MainActivity activity = activityWeakReference.get();
activity.sharedPreferencesKeys();
activity.alertDialogClickListener();
activity.changeListener();
return "MainAsyncTask Worked!";
}
#Override
protected void onPostExecute(String s) {
Log.d("MainAsyncTask", "" + s);
}
}
init method
private void initialize(View view) {
loadframe = view.findViewById(R.id.loadframe);
menu = view.findViewById(R.id.menu);
bottombar = view.findViewById(R.id.bottombar);
menu1 = view.findViewById(R.id.menu1);
pageIndicator = coachStub1.findViewById(R.id.pageIndicator);
vkisonuctw = coachStub1.findViewById(R.id.vkisonuctw);
numberpicker = coachStub2.findViewById(R.id.numberpicker);
radiogrouphareket = coachStub2.findViewById(R.id.radiogrouphareket);
belkalcasonuctw = coachStub3.findViewById(R.id.belkalcasonuctw);
belkalcasonuctwinfo = coachStub3.findViewById(R.id.belkalcasonuctwinfo);
// much more
}
You have to load the view asynchronously,Call this method setcontentview (view) to set it to activity
like this:
new AsyncLayoutInflater(context).inflate(layout, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
#Override
public void onInflateFinished(#NonNull View view, int resid, #Nullable ViewGroup parent) {
//load view into activity
activity.setContentView(view);
}
});

Tell me the solution to the simple grammatical errors of Android Studios

public void onClick (View v) {
There is a simple grammatical error in this area and I don't know how to solve it.
i don't know sorry
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView view = new MyView();
public void onClick (View v) {
switch (view.getId()) {
case R.id.btn :
view.isDraw = true;
break;
}
}
}
MyView class
public class MyView extends View {
public boolean isDraw = false;
public MyView() {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
Random random = new Random();
int x = random.nextInt(500);
int y = random.nextInt(800);
int r = (random.nextInt(3)+1) * 100;
#Override
protected void onDraw(Canvas canvas) {
isDraw = false;
super.onDraw(canvas);
canvas.drawColor(Color.LTGRAY);
Paint Pnt = new Paint();
Pnt.setColor(Color.BLUE);
if(isDraw == true) {
canvas.drawCircle(x,y,r,Pnt);
}
}
}
I want to draw a circle on the coordinates of the random value when I press the button.
You can't have a void on your onCreate. Try to put it outside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView view = new MyView();
}
public void onClick (View v) {
switch (view.getId()) {
case R.id.btn :
view.isDraw = true;
break;
}
}
I want to draw a circle on the coordinates of the random value when I press the button.
If you want to do that, first, you need to find your button using findViewById an example coulld be :
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = findViewById(R.id.IdfromActivity_main);
}
Then if you want to make an onClickListener there are multiple ways to do this.
In your code an example could be :
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = findViewById(R.id.IdfromActivity_main);
MyView view = new MyView();
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button clicked!",
Toast.LENGTH_LONG).show();
//Add whatever action you need
}
});
}
And then you have also an error, that you have a method inside another method, let's say you have inside the onCreate method onClick method, so you should split them and put it outside of this onCreate method.

Creating the 'next' button based on another activity

I am making a drawing app. I have two files : 'CatList_Activity.java' and 'Draw_Activity.java'. CatList_Activity is the page with the menu where all the images are displayed, and by clicking on one (method 'onItemClick') it sends me to the page Draw_Activity and displays that image, id being the attribute 'position' in the override method. What I want to do is to create the 'next' button (via imageView with onClickListener) in Draw_Activity, which will straight away send me to the next image. My guess would be to somehow increment 'position' value in CatList_Activity file, but I am not sure how to do it
CatList_Activity.java
public class CatList_Activity extends ActionBarActivity {
GridView grid;
private String[] arrImagesStrings;
String Foldername;
CatListAdapter adapter;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.catlist_activity);
Toolbar toolbar = (android.support.v7.widget.Toolbar)
this.findViewById(R.id.toolbar);
toolbar.setTitle("");
this.setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
grid = (GridView) findViewById(R.id.lsv_catlist);
Intent i = getIntent();
Foldername = i.getStringExtra("Folder");
arrImagesStrings = listAssetFiles(Foldername);
adapter = new CatListAdapter(CatList_Activity.this, R.layout.catlist_item, arrImagesStrings, Foldername);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), ""+selectedImagePath, Toast.LENGTH_SHORT).show();
//Log.e("name", ""+Foldername+"/"+arrImagesStrings[position]);
Intent intentdraw = new Intent(getApplicationContext(), Draw_Activity.class);
intentdraw.putExtra("Folder", Foldername + "/" + arrImagesStrings[position]);
startActivity(intentdraw);
}
}
}
}
Draw_Activity.java
public class Draw_Activity extends Activity {
private PaintView pv;
protected View layout;
protected int progress;
protected Dialog dialog;
protected Dialog textdialog;
private String[] arrImagesStrings;
protected float stroke = 6;
int postion;
private String fileName;
private int ColorAh = Color.BLACK;
String Foldername;
RelativeLayout re;
private AdView mAdView;
ImageView img_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawpic_activity);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
re = (RelativeLayout) findViewById(R.id.rell);
img_next = (ImageView) findViewById(R.id.image_next);
this.fileName = null;
super.onCreate(savedInstanceState);
this.pv = new PaintView(this);
re.addView(pv);
//setContentView(this.pv);
this.pv.togglePencil(true);
Intent i = getIntent();
Foldername = i.getStringExtra("Folder");
img_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
???
}
});
}
}
In your CatList_Activity, change
Intent intentdraw=new Intent(getApplicationContext(),Draw_Activity.class);
intentdraw.putExtra("Folder", Foldername+"/"+arrImagesStrings[position]);
to
Intent intentdraw=new Intent(getApplicationContext(),Draw_Activity.class);
intentdraw.putExtra("FolderName", Foldername);
intentdraw.putExtra("position", position);
intentdraw.putExtra("images", arrImagesStrings);
In your Draw_Activity, handle onClick to increment the position and get the desired path.
Foldername=i.getStringExtra("FolderName");
position = i.getIntExtra("position", 0);
images = i.getStringArrayExtra("images");
path = Foldername + "/" + images[position];
Make list of images .... Do something like this
mDrawerList = (ListView) findViewById(R.id.slider_list);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menutitles.length; i++) {
RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(

specific sound must play when item is clicked

I am making dictionary app having sound sample, when an item in the list is click, a new activity will open containing details view and a button. Can someone help me how can I assign the specific sound to the button. The sound files are placed in raw folder. for example, I click the item 'araw',the details will show and the button must play the sound araw...Pls help me...
heres the codes:
ListView lv;
SearchView sv;
String[] tagalog= new String[] {"alaala (png.)","araw (png.)","baliw (png.)","basura (png.)",
"kaibigan (png.)","kakatuwa (pu.)", "kasunduan (png.)","dambuhala (png.)",
"dulo (png.)","gawin (pd.)","guni-guni (png.)","hagdan (png.)","hintay (pd.)",
"idlip (png.)","maganda (pu.)","masarap (pu.)", "matalino (pu.)"};
ArrayAdapter<String> adapter;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
sv = (SearchView) findViewById(R.id.searchView1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tagalog);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tagword =tagalog[position];
String[] definition = getResources().getStringArray(R.array.definition);
final String definitionlabel = definition[position];
String[] cuyuno = getResources().getStringArray(R.array.cuyuno);
final String cuyunodefinition = cuyuno[position];
String[] english = getResources().getStringArray(R.array.english);
final String englishdefinition = english[position];
Intent intent = new Intent(getApplicationContext(), DefinitionActivity.class);
intent.putExtra("tagword", tagword);
intent.putExtra("definitionlabel", definitionlabel);
intent.putExtra("cuyunodefinition",cuyunodefinition);
intent.putExtra("englishdefinition", englishdefinition);
startActivity(intent);
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
DefinitionActivity.java
public class DefinitionActivity extends AppCompatActivity {
String tagalogword;
String worddefinition;
String cuyunoword;
String englishword;
int[] sounds = new int[]{R.raw.alaala,
R.raw.araw,
R.raw.baliw,
R.raw.basura,
R.raw.kaibigan,
R.raw.kakatuwa,
R.raw.kasunduan,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_definition);
TextView wordtv = (TextView) findViewById(R.id.wordtv);
TextView definitiontv = (TextView) findViewById(R.id.definitiontv);
TextView cuyunotv = (TextView) findViewById(R.id.cuyunotv);
TextView englishtv = (TextView) findViewById(R.id.englishtv);
Button play = (Button) findViewById(R.id.playbty);
final Bundle extras = getIntent().getExtras();
if (extras != null) {
tagalogword = extras.getString("tagword");
wordtv.setText(tagalogword);
worddefinition = extras.getString("definitionlabel");
definitiontv.setText(worddefinition);
cuyunoword = extras.getString("cuyunodefinition");
cuyunotv.setText(cuyunoword);
englishword = extras.getString("englishdefinition");
englishtv.setText(englishword);
}
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I do not know how to play the audio when button click
}
});
}
}
Like Pedif said, you should use SoundPool for playing short sounds.
Here is Code from my Tetris like game, the Activity creates the Instance and get a callback when sounds were loaded.
Example as Singleton:
public class Sounds {
private Context ctx;
private static Sounds mSounds = null;
private static SoundPool mSPool = null;
private int sound_gameover;
private int sound_teil_drehen;
private int sound_1_zeile;
private int sound_2_zeile;
private int sound_3_zeile;
private int sound_4_zeile;
private int soundsToLoad = 6;
/**
* Volumecontrol
*/
private AudioManager audioManager;
private float actualVolume;
private float maxVolume;
private float volume;
private IOnSoundReady callback = null;
public static Sounds getInstance(Context ctx, IOnSoundReady mCallback){
if(mSPool == null){
mSounds = new Sounds(ctx, mCallback);
}
return mSounds;
}
private Sounds(Context ctx, IOnSoundReady mIOSoundReady) {
this.ctx = ctx;
this.callback = mIOSoundReady;
initVolume();
AsyncTask<Void, Void, Void> mTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
initSoundPool();
return null;
}
};
mTask.execute();
}
public void unprepare() {
if (mSPool == null) return;
mSPool.release();
mSPool = null;
}
private void initSoundPool(){
mSPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
mSPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Log.w(TAG, "loaded soundid: " + sampleId);
if(--soundsToLoad == 0 && callback != null){
callback.onSoundReady();
}
}
});
sound_gameover = mSPool.load(ctx, R.raw.game_over, 1);
sound_teil_drehen = mSPool.load(ctx, R.raw.rotate, 1);
sound_1_zeile = mSPool.load(ctx, R.raw.line_1,1);
sound_2_zeile = mSPool.load(ctx, R.raw.line_2, 1);
sound_3_zeile = mSPool.load(ctx, R.raw.line_3, 1);
sound_4_zeile = mSPool.load(ctx, R.raw.line_4, 1);
}
/**
* calculate volume
*/
private void initVolume(){
audioManager = (AudioManager) ctx.getSystemService(SpielActivity.AUDIO_SERVICE);
actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actualVolume / maxVolume;
}
/**
* plays a sound
* #param soundid
*/
public void playSound(int soundid){
mSPool.play(soundid, volume, volume,1, 0, 1f);
}
}
Interface used when sounds are loaded, implemented by activity:
public interface IOnSoundReady {
void onSoundReady();
}
Usage in your activity:
Sounds mySounds = Sounds.getInstance(this, this);
Play sound:
public static void playSound(int soundid) {
mySounds.playSound(soundid);
}
Hope I could help a bit.
// Try this way
ListView lv;
SearchView sv;
String[] tagalog= new String[] {"alaala (png.)","araw (png.)","baliw (png.)","basura (png.)",
"kaibigan (png.)","kakatuwa (pu.)", "kasunduan (png.)","dambuhala (png.)",
"dulo (png.)","gawin (pd.)","guni-guni (png.)","hagdan (png.)","hintay (pd.)",
"idlip (png.)","maganda (pu.)","masarap (pu.)", "matalino (pu.)"};
ArrayAdapter<String> adapter;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
sv = (SearchView) findViewById(R.id.searchView1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tagalog);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tagword =tagalog[position];
String[] definition = getResources().getStringArray(R.array.definition);
final String definitionlabel = definition[position];
String[] cuyuno = getResources().getStringArray(R.array.cuyuno);
final String cuyunodefinition = cuyuno[position];
String[] english = getResources().getStringArray(R.array.english);
final String englishdefinition = english[position];
Intent intent = new Intent(getApplicationContext(), DefinitionActivity.class);
intent.putExtra("tagword", tagword);
intent.putExtra("definitionlabel", definitionlabel);
intent.putExtra("cuyunodefinition",cuyunodefinition);
intent.putExtra("englishdefinition", englishdefinition);
// put position of the file
intent.putExtra("position", position);
startActivity(intent);
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
// DictionaryActivity.class
public class DefinitionActivity extends AppCompatActivity {
String tagalogword;
String worddefinition;
String cuyunoword;
String englishword;
int[] sounds = new int[]{R.raw.alaala,
R.raw.araw,
R.raw.baliw,
R.raw.basura,
R.raw.kaibigan,
R.raw.kakatuwa,
R.raw.kasunduan,
};
private int songPosition = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_definition);
TextView wordtv = (TextView) findViewById(R.id.wordtv);
TextView definitiontv = (TextView) findViewById(R.id.definitiontv);
TextView cuyunotv = (TextView) findViewById(R.id.cuyunotv);
TextView englishtv = (TextView) findViewById(R.id.englishtv);
Button play = (Button) findViewById(R.id.playbty);
final Bundle extras = getIntent().getExtras();
if (extras != null) {
tagalogword = extras.getString("tagword");
wordtv.setText(tagalogword);
worddefinition = extras.getString("definitionlabel");
definitiontv.setText(worddefinition);
cuyunoword = extras.getString("cuyunodefinition");
cuyunotv.setText(cuyunoword);
englishword = extras.getString("englishdefinition");
englishtv.setText(englishword);
songPosition = extras.getInt("position");
}
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Play it here like
MediaPlayer mediaPlayer=MediaPlayer.create(DictionaryActivity.class,sounds[position]);
mediaPlayer.start();
}
});
}
}
You have to use SoundPool.
For sound effects which are short you would use SoundPool otherwise you should use MediaPlayer.

Android: Drawing a circle on bitmap

I'm trying to make an app that draws a circle on a bitmap. Right now, I have a button that produces the circle. Instead, I would like to draw the circle where the user double-taps (instead of pressing a button). How can I do this programmatically? Here is the content of the activity so far:
public static final String KEY_PATH = "img.jpg";
private ZoomInZoomOut touch;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_display);
Intent intent = getIntent();
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
try {
java.io.FileInputStream in = this.openFileInput(path);
bitmap = BitmapFactory.decodeStream(in);
bitmap = bitmap.copy(bitmap.getConfig(), true);
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
Button draw = (Button) findViewById(R.id.draw);
draw.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(),
bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
p.setStrokeWidth(2);
p.setStyle(Paint.Style.STROKE);
canvas.drawBitmap(bitmap,new Matrix(),null);
canvas.drawCircle(1000, 1000, 20, p);
touch.setImageBitmap(bmOverlay);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
You have to implement GestureDetector and put your code in single/double click. Here you can replace button with bitmap.
TestActivity.java
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//putyour first activity call.
}
}
iv.setOnTouchListener(new OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(context));
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
Now you have to create GestureDetector.java class.
public class MyGestureDetector extends SimpleOnGestureListener {
public Context context;
public String phno;
public MyGestureDetector(Context con)
{
this.context=con;
}
#Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
public MyGestureDetector(Context con) {
this.context=con;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("in Double tap");
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("in single tap up");
//put your second activity.
return super.onSingleTapUp(e);
}
}

Categories