I'm working on this App and currently testing it and I'm not quite sure what's wrong. As I tap or hold the Deck Button absolutely nothing happens. Only thing showing up in logcat are Touch Events. I think it might be something wrong with my formatting, but since this is my first time with Java (Only have used assembly for a microprocessors class in the past) I am not sure what it could be.
EDIT: Removed a lot of the extra code unrelated to the problem per suggestion
Here is the MainActivity.java
public class MainActivity extends AppCompatActivity {
int i=0;
int VOA=0;
int VOB=0;
int [] c ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// The Draw Control for the Deck. It will only draw if it is in it's 0 state
ImageButton Deck =(ImageButton)(findViewById(R.id.Deck));
assert Deck != null;
Deck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( VOA==0){
VOA=c[i];
i=i+1;
} else if (VOB==0){
VOB=c[i];
i=i+1;
}
}
});
// Uses Collections shuffle to shuffle c
Deck.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
List c= new ArrayList();
Collections.shuffle(c);
return false;
}});
//Discard for Objective A by setting it to 0 and thus updating it's case statement.
ImageButton ObjectiveA =(ImageButton)(findViewById(R.id.ObjectiveA));
assert ObjectiveA != null;
ObjectiveA.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
VOA=0;
return false;
}
});
//Discard for Objective B by setting it to 0 and thus updating it's case statement.
ImageButton ObjectiveB =(ImageButton) (findViewById(R.id.ObjectiveB));
assert ObjectiveB != null;
ObjectiveB.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
VOB=0;
return false;
}
});
}
}
}
Here is the activity_main.xml as well
<?xml version="1.0" encoding="utf-8"?>
<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="gallostsp.darkagedeck.MainActivity"
android:background="#drawable/background"
android:screenOrientation="landscape">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Deck"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/VerticalCenterLine"
android:layout_toStartOf="#+id/VerticalCenterLine"
android:background="#drawable/deck"
android:contentDescription="#string/objectives_deck"
android:clickable="true"
android:longClickable="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ObjectiveA"
android:background="#drawable/objectivearea"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/VerticalCenterLine"
android:layout_toEndOf="#+id/VerticalCenterLine"
android:layout_above="#+id/ObjectiveB"
android:contentDescription="#string/objective_a"
android:longClickable="true"
android:clickable="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ObjectiveB"
android:layout_below="#+id/HorizontalCenterLine"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/objectivearea"
android:layout_toRightOf="#+id/VerticalCenterLine"
android:layout_toEndOf="#+id/VerticalCenterLine"
android:layout_alignParentBottom="true"
android:contentDescription="#string/objective_b"
android:longClickable="true"
android:clickable="false" />
</RelativeLayout>
Return true if you consumed the longclickListener()
Deck.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
List c= new ArrayList();
Collections.shuffle(c);
return true; // change to true
}});
Related
I have implemented a BottomNavigationView which allows the user to switch between fragments. But now I want to trigger this manually in the program using another button. This button is located in a snackbar. (See code)
Snackbar.make(BLEFragment.this.getView(),"BLE IS NOT ENABLE ! \n go to the settings-tab,...",Snackbar.LENGTH_SHORT)
.setBackgroundTint(Color.rgb(244, 78, 63))
.setTextColor(Color.rgb(255,255,255))
.setAction("Settigs", new View.OnClickListener() {
#Override
public void onClick(View view) {
}
})
.show();
Maybe you can help me here.
If I have formulated the question wrong. I would like to apologize. I do not know how to formulate it differently.
Thank you very much.
Ramsauer René
Snackbar is a control in the Android Support Design Library, which can quickly pop up messages at the bottom of the screen.
Snackbar.make(View view, CharSequence text, int duration)
You can check the parameters and corresponding API
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/galaxyContainer">
</FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/galaxyBottomBar"
android:layout_margin="20dp"
android:paddingVertical="6dp"
app:fabCradleMargin="8dp"
app:fabCradleRoundedCornerRadius="30dp"
app:fabAnchorMode="cradle"
app:fabAlignmentMode="center"
app:elevation="10dp"
app:addElevationShadow="true"
app:fabAnimationMode="slide"
app:fabCradleVerticalOffset="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="18dp"
android:orientation="horizontal"
android:weightSum="2">
<com.google.android.material.button.MaterialButton
android:id="#+id/galaxyExploreButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/galaxy_btn_explore"
android:background="#android:color/transparent"
android:text="Explore"
android:textColor="#drawable/galaxy_tab_text"
android:layout_weight="1"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/galaxyGalleryButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/galaxy_btn_gallery"
android:background="#android:color/transparent"
android:text="Gallery"
android:textColor="#drawable/galaxy_tab_text"
android:layout_weight="1"/>
</LinearLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here goes my java code
public void switchTab(boolean isExplore) {
exploreButton.setSelected(isExplore);
galleryButton.setSelected(!isExplore);
FragmentTransaction ft = `enter code here`getSupportFragmentManager().beginTransaction();
ft.replace(R.id.galaxyContainer, isExplore ? new ExploreFragment() : new GalleryFragment());
ft.commit();
}
#Override
public void onClick(View v) {
switchTab(v.getId() == R.id.galaxyExploreButton);
}
Hope this would help.
bottomNavigationView.getMenu() will resolve your issue.try this
Snackbar.make(BLEFragment.this.getView(),"BLE IS NOT ENABLE ! \n go to the settings-tab,...",Snackbar.LENGTH_SHORT)
.setBackgroundTint(Color.rgb(244, 78, 63))
.setTextColor(Color.rgb(255,255,255))
.setAction("Settigs", new View.OnClickListener() {
#Override
public void onClick(View view) {
//Move to next fragment
switchFragment(R.id.more_setting);
}
})
.show();
public void switchFragment(int idd) {
Menu menu = bottomNavigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
if(item.getItemId() == idd){
item.setChecked(true);
getSupportFragmentManager().beginTransaction().replace(R.id.container, new ProgramFragment()).addToBackStack(null).commit();
}
}
}
in activity which holds fragment views
#Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
}
first of all, I'll explain the expected behavior of the App.
What does the App do?
When I run the App I see this view:
Now if you hit the "+" button you'll see this Dialog:
By clicking Add this entry will be added to a ListView. I added two items. This looks like this:
Expected Result
Now when I touch and hold the mic icon of the list items it should start recording audio through the device microphone and then save this recording with the name of the bold entry, that means e.g. the first recording will get the name A.3gp, the second one B.3gp and so on..
Actual Result (UPDATE 07.12.2017)
The current state is:
Every item in the list gets his "own" recording. That means if I touch and hold the microphone icon of the first item in the list, it is doing what it should do. The same goes for all other items on the list.
When I add the first item A, then touch its record icon, a file will be created with the name A.3gp (which is correct behavior).
When I add the second item B then do nothing and then add a third item C and touch the record icon for B, a file will be created with the name C.3gp (which is not a correct behavior, it should be B.3gp).
Now to the fun part.
Code (UPDATE 07.12.2017)
1. The Model
public class Word {
private String mForeignTranslation;
private String mDefaultTranslation;
private ImageView mRecordIconImageResourceId;
private MediaRecorder mMediaRecorder;
public Word(String foreignTranslation, String defaultTranslation, ImageView recordIconImageResourceId) {
this.mForeignTranslation = foreignTranslation;
this.mDefaultTranslation = defaultTranslation;
this.mRecordIconImageResourceId = recordIconImageResourceId;
}
public Word(String foreignTranslation, String defaultTranslation) {
this.mForeignTranslation = foreignTranslation;
this.mDefaultTranslation = defaultTranslation;
}
public String getDefaultTranslation() {
return mDefaultTranslation;
}
public String getForeignTranslation() {
return mForeignTranslation;
}
public ImageView getRecordIconImageResourceId() {
return mRecordIconImageResourceId;
}
public MediaRecorder getMediaRecorder() {
return mMediaRecorder;
}
public void setDefaultTranslation(String mDefaultTranslation) {
this.mDefaultTranslation = mDefaultTranslation;
}
public void setForeignTranslation(String mForeignTranslation) {
this.mForeignTranslation = mForeignTranslation;
}
public void setRecordIconImageResourceId(ImageView recordIconImageResourceId) {
this.mRecordIconImageResourceId = recordIconImageResourceId;
}
public void setMediaRecorder(MediaRecorder mMediaRecorder) {
this.mMediaRecorder = mMediaRecorder;
}
}
2. The Adapter
public class WordAdapter extends ArrayAdapter<Word> {
private ArrayList<Word> wordsArrayList = new ArrayList<>();
private MediaRecorder mediaRecorder = new MediaRecorder();
public WordAdapter(#NonNull Context context, ArrayList<Word> words) {
super(context, 0, words);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.my_word_list_items,parent,false);
}
Word currentWord = getItem(position);
TextView foreignWord = listItemView.findViewById(R.id.myForeignWord);
foreignWord.setText(currentWord.getForeignTranslation());
TextView defaultWord = listItemView.findViewById(R.id.myDefaultWord);
defaultWord.setText(currentWord.getDefaultTranslation());
final ImageView recordIconImageView = listItemView.findViewById(R.id.recordIconImageView);
wordsArrayList = MyWordsActivity.getWordsArrayList();
recordIconImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
recordIconImageView.setImageResource(R.drawable.mic_red);
startAudioRecording();
break;
}
case MotionEvent.ACTION_UP: {
recordIconImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
return listItemView;
}
private ArrayList<Word> getWordsArrayList() {
return wordsArrayList;
}
private void startAudioRecording() {
if (wordsArrayList != null) {
Log.i("ArrayListe", wordsArrayList.toArray().toString());
getMediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Toast.makeText(this.getContext(), "Recording started", Toast.LENGTH_SHORT).show();
}
}
private void stopAudioRecording() {
try {
mediaRecorder.stop();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this.getContext(), "Recording stopped", Toast.LENGTH_SHORT).show();
}
private void getMediaRecorderReady() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"speakmylanguage"+"/"+MyWordsActivity.getForeignWord()+".3gp");
}
}
3. The Activity
public class MyWordsActivity extends AppCompatActivity {
private static String defaultWord, foreignWord;
private static ArrayList<Word> wordsArrayList = new ArrayList<>();
WordAdapter wordAdapter;
// Main Activity Views
TextView hintTextView;
ListView myWordsListView;
FloatingActionButton floatingButtonAddNewWord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_words);
// Init Main Activity Views
hintTextView = findViewById(R.id.hintTextView);
myWordsListView = findViewById(R.id.myWordsList);
floatingButtonAddNewWord = findViewById(R.id.fabAddNewWord);
floatingButtonAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addNewWordsDialog = new Dialog(MyWordsActivity.this);
addNewWordsDialog.setContentView(R.layout.activity_add_new_words);
final EditText addForeignWordEditText = addNewWordsDialog.findViewById(R.id.addForeignWordEditText);
final EditText addDefaultWordEditText = addNewWordsDialog.findViewById(R.id.addDefaultWordEditText);
final Button addNewWordButton = addNewWordsDialog.findViewById(R.id.addNewWordButton);
addNewWordsDialog.show();
addNewWordButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!addDefaultWordEditText.getText().toString().equals("") &&
!addForeignWordEditText.getText().toString().equals("")) {
foreignWord = addForeignWordEditText.getText().toString();
defaultWord = addDefaultWordEditText.getText().toString();
wordsArrayList.add(new Word(foreignWord, defaultWord));
hintTextView.setVisibility(View.GONE);
addNewWordsDialog.dismiss();
} else {
Toast.makeText(MyWordsActivity.this, "Please enter two words", Toast.LENGTH_SHORT).show();
}
}
});
wordAdapter = new WordAdapter(MyWordsActivity.this, getWordsArrayList());
myWordsListView.setAdapter(wordAdapter);
}
});
}
public static String getDefaultWord() {
return defaultWord;
}
public static void setDefaultWord(String defaultWord) {
MyWordsActivity.defaultWord = defaultWord;
}
public static String getForeignWord() {
return foreignWord;
}
public static void setForeignWord(String foreignWord) {
MyWordsActivity.foreignWord = foreignWord;
}
public static ArrayList<Word> getWordsArrayList() {
return wordsArrayList;
}
}
4. The Layouts
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/myWordsListItems"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:orientation="vertical">
<TextView
android:id="#+id/myForeignWord"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="24sp"
android:textStyle="bold"
tools:text="foreign word" />
<TextView
android:id="#+id/myDefaultWord"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="24sp"
tools:text="default word" />
</LinearLayout>
<ImageView
android:id="#+id/playIconImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:clickable="true"
android:src="#drawable/play_icon" />
<ImageView
android:id="#+id/recordIconImageView"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="72dp"
android:clickable="true"
android:src="#drawable/mic_black" />
</RelativeLayout>
activity_my_words.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yousef.mustafa.speakmylanguage.View.MyWordsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="#string/app_name"
app:titleTextColor="#color/colorWhite" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myWordsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:drawSelectorOnTop="true"
android:orientation="vertical"
tools:context=".View.MyWordsActivity" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAddNewWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="32dp"
android:tint="#color/colorWhite"
app:backgroundTint="#color/colorGrey"
app:srcCompat="#drawable/icon_add" />
<TextView
android:id="#+id/hintTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="155dp"
android:gravity="center"
android:text="#string/hint"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
activity_add_new_words.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:background="#color/colorDialogBackground">
<LinearLayout
android:id="#+id/addNewWordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="12dp"
android:text="#string/add_word_title"
android:textColor="#color/colorBlack"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/addForeignWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="4dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="#string/enter_foreign_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<EditText
android:id="#+id/addDefaultWordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:layout_weight="5"
android:gravity="center_horizontal"
android:hint="#string/enter_default_word"
android:inputType="textPersonName|textCapWords"
android:textSize="24sp" />
<Button
android:id="#+id/addNewWordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/button_add"
android:textAllCaps="false"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
I would appreciate any help. Thanks in advance.
In the startAudioRecording() method of the Activity, you set the output filename from the foreignWord field. The problem is that the foreignWord field is set to the most recently added foreign word each time the user submits a word pair in the Add Word dialog. Thus each time the user starts a recording, the name of the output file will be set to the last foreign word added.
Instead of using fields for recordingImageView, foreignWord, defaultWord, foreignWord, audioSavePath, and mediaRecorder, you should make them local variables, and pass them as parameters to your record(), startAudioRecording(), stopAudioRecording() methods with the following steps:
Just delete those fields from the beginning of the class so that you see some compilation errors where the fields were used.
Where you see an undefined variable on the left side of an assignment, make the variable local (Ctrl+V in Android Studio).
Where you see an undefined variable elsewhere, make the variable a parameter of the method (Ctrl+P).
It should work after those changes.
`
Kindly move this snippet :
recordingImageView.setOnTouchListener(new View.OnTouchListener()
{
#Override public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
{ recordingImageView.setImageResource(g startAudioRecording(position);
break;
}
case MotionEvent.ACTION_UP:
{
recordingImageView.setImageResource(R.drawable.mic_black);
stopAudioRecording();
break;
}
}
return true;
}
});
In Adapter , getView(...) Below accessing recordImageView.
And put startAudioRecording() , stopAudioRecording() in same adapter or all relevant method for recording is to be in Adapter.
So by this you will get click on in each individual image view. Previously when you are fetching it will only take instance of any one image view.
Now update code on "startAudioRecording()" as:
private void startAudioRecording()
{
if (checkPermission())
{
if (wordsArrayList != null)
{
audioSavePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + wordsArrayList.get(position) + ".3gp";
getMediaRecorderReady();
}
}
}
Now your second point, saving its name by any name. Will cover by above code.
Thanks and happy coding
I want to detect onLongPress event with in particular layout.In my xml LinearLayout with in the RelativeLayout.I want to get the onLongTouch with in the LinearLayout only.I try to using onTouchEvent this works well, but onLongPress working on hole layout including RelativeLayout. If I put return mDetector.onTouchEvent(event); with in the myLinear.setOnTouchListener.It turn into single Touch.Please help me to solve this problem.Any reply I am very appreciate.
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:text="Show Text"
android:textStyle="bold"
android:textSize="#dimen/abc_text_size_title_material"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/myLinear"
android:layout_below="#+id/txtView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/floorimg">
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout myLinear;
private GestureDetectorCompat mDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLinear = (LinearLayout)findViewById(R.id.myLinear);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
myLinear.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
});
}
/* #Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}*/
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public void onLongPress(MotionEvent e) {
Toast.makeText(getBaseContext(),"Long press called",Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
}
}
You may want to use the OnLongClickListener with you LinearLayout:
myLinear.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long press called", Toast.LENGTH_SHORT).show();
return true;
}
});
I want to cancel shortClick when LongClicked an item,
I'm using ripple effect material-ripple library in list view but when i long press list item it also call onClick-event
list.setOnItemClickListner not work with MaterialRippleLayout on row item
i have also return true in OnLongClicked but does not work..
also tried and added method in MaterialRippleLayout.java
#Override
public void setOnLongClickListener(OnLongClickListener l) {
super.setOnLongClickListener(l);
childView.setOnLongClickListener(l);
}
and some changes in gesturedetectore
here is my code list_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center" >
<com.balysv.materialripple.MaterialRippleLayout
android:id="#+id/list_item_ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
app:rippleAlpha="0.2"
app:rippleColor="#585858"
app:rippleDelayClick="true"
app:rippleHover="true"
app:rippleOverlay="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:text="Name/Number"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2_dur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="Duration"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp" >
<TextView
android:id="#+id/textView3_in_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Incoming/Outgoing"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</LinearLayout>
</com.balysv.materialripple.MaterialRippleLayout>
& java code
holder.riple.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "item riple clicked "+position, 0).show();
Intent intent = new Intent(getActivity(), PlayerActivity.class);
intent.putExtra("songs", list_path.get(position).toString());
intent.putExtra("name", ""+parts[0]);
intent.putExtra("dur", ""+convertMillis(parts[2]));
intent.putExtra("time", getDate(parts[1].toString()));
startActivity(intent);
}
});
holder.riple.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
list.showContextMenu();
//Toast.makeText(getActivity(), "LongClick", 0).show();
//v.setClickable(false);//v.isClickable();
return true;
}
});
sory for bad english
It looks like you are using a ListView and list_item is the xml for each row. If I'm wrong, stop reading now! But if I'm right, we should consider using the methods setItemClickListener() and setOnItemLongClickListener().
So in your Activity:
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return true; // return true to prevent the `onClick` from being triggered as well.
}
});
Notice the position parameter gets passed in these two methods, so as long as you have a reference to the data set that is being displayed, you can access the exact item that was clicked or long clicked.
It's hard for me to tell why your code wasn't working, so some might not consider this an answer, but I hope this can help. All I know is that things can get really messy when setting click listeners on individual views inside a list view through the adapter.
As a side note, if the only action we want to execute in the on long click action is showing the context menu, then we should consider using registerForContextMenu(listView) and then using the context menu life cycle. One can find more documentation on that practice here.
Actually there is a bug in material-ripple library , already reported there,https://github.com/balysv/material-ripple/issues/15
i have solved my issue,
boolean isLongClick = false;
holder.riple.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "item riple clicked "+position, 0).show();
Intent intent = new Intent(getActivity(), PlayerActivity.class);
intent.putExtra("songs", list_path.get(position).toString());
intent.putExtra("name", ""+parts[0]);
intent.putExtra("dur", ""+convertMillis(parts[2]));
intent.putExtra("time", getDate(parts[1].toString()));
if(isLongClick== false)
{
startActivity(intent);
}
// for click agin on item
isLongClick = false;
}
});
holder.riple.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
isLongClick = true;
list.showContextMenu();
//Toast.makeText(getActivity(), "LongClick", 0).show();
//v.setClickable(false);//v.isClickable();
return true;
}
});
I am currently developing an android application in which purpose is to play a large variety of every-day sounds on click.
I have 3 activities : home, animaux and transports.
Home is fine, animaux is fine (it plays all the sounds without any problems).
But I want to integrate admob on Transports acivity. Ads display well, but when I cluck on the sounds buttons, nothing happens. If you could help me understand my mistake and how to fix it, it would be very nice !
Here is transports.java
public class Transports extends Activity {
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transports);
// Create the adView
adView = new AdView(this, AdSize.BANNER, "xxxxxx"
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
private MediaPlayer mPlayer = null;
/** Called when the activity is first created. */
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transports);
Button btn_sound_sncf2 = (Button) findViewById(R.id.sncfnew);
btn_sound_sncf2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.sncf2);
}
});
Button btn_sound_sncf1 = (Button) findViewById(R.id.sncf1);
btn_sound_sncf1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.sncf1);
}
});
}
#Override
public void onPause() {
super.onPause();
if(mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
}
private void playSound(int resId) {
if(mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = MediaPlayer.create(this, resId);
mPlayer.start();
}
}
And here is the transports.xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_vertical"
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=".Transports" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
android:gravity="top" >
</com.google.ads.AdView>
<Button android:id="#+id/sncfnew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/sncf1"
android:layout_below="#+id/sncf1"
android:layout_marginTop="38dp"
android:text="#string/sncfnew" />
<Button android:id="#+id/sncf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1"
android:text="#string/sncf1" />
</LinearLayout>
</RelativeLayout>
I wonder if the problem isn't in my xml file : I have a lot of difficulties to maker a clear interface with the linearlayout required by admob.
You aren't actually setting the listeners. You have that code in a method:
public void onCreate1(Bundle savedInstanceState) {
that is never called. Move that code to the actual onCreate() method and it should work fine.