Skip to content

Commit

Permalink
Adding ClickListener to the SegmentedButtonGroup. Try 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasilis committed Oct 24, 2020
1 parent 6b29579 commit 2ae52a6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/alimaddi/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
//import butterknife.BindView;
Expand Down Expand Up @@ -208,6 +210,14 @@ protected void onCreate(Bundle savedInstanceState)

binding.buttonGroupGradient.setPosition(position, true);
});

binding.buttonGroupGradient.setOnClickListener(new SegmentedButtonGroup.OnClickListener() {
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this, "Click Listener", Toast.LENGTH_SHORT).show();
}
});
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
app:rippleColor="@color/blue_300"
app:selectedBackground="@drawable/gradient_drawable_selector"
app:selectionAnimationDuration="1000"
app:selectionAnimationInterpolator="fastOutSlowIn">
app:selectionAnimationInterpolator="fastOutSlowIn" >

<com.alimaddi.segmentedbutton.SegmentedButton
android:layout_width="0dp"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.android.tools.build:gradle:4.0.2'


// NOTE: Do not place your application dependencies here; they belong
Expand Down
16 changes: 8 additions & 8 deletions segmentedbutton/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.0"

defaultConfig {
minSdkVersion 23
targetSdkVersion 29
versionCode 4
versionName "4.0.4"
targetSdkVersion 30
versionCode 5
versionName "4.0.5"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -54,8 +54,8 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public class SegmentedButtonGroup extends LinearLayout
// Note: Position change will be notified after animations complete because that is when the position actually
// changes
private OnPositionChangedListener onPositionChangedListener;

private OnClickListener clickListener ;
// endregion

// region Constructor
Expand Down Expand Up @@ -598,6 +598,8 @@ else if (!isLTR && position != 0)
button.setSelectedButtonBorder(selectedBorderWidth, selectedBorderColor, selectedBorderDashWidth,
selectedBorderDashGap);

button.setOnClickListener(SegmentedButtonGroup.this::onClick);

// Add the button to the main group instead and store the button in our buttons list
buttonLayout.addView(button, params);
buttons.add(button);
Expand Down Expand Up @@ -1850,6 +1852,20 @@ public void setOnPositionChangedListener(final OnPositionChangedListener onPosit
this.onPositionChangedListener = onPositionChangedListener;
}

/**
* Sets the listener used for notifying view clicked
*/
public void setOnClickListener(OnClickListener clickListener)
{
this.clickListener = clickListener;
}

public void onClick(View v)
{
if (clickListener != null)
clickListener.onClick(v);
}

// endregion

// region Listeners
Expand All @@ -1865,6 +1881,16 @@ public interface OnPositionChangedListener
void onPositionChanged(int position);
}

/**
* Interface definition for a callback that will be invoked when a child of the segmented button gtoup clicked
*
* This callback will be called before the animation is start since invoke onClick of view.
*/
public interface OnClickListener
{
void onClick(View view);
}

// endregion

// region Classes
Expand Down

0 comments on commit 2ae52a6

Please sign in to comment.