This repo contains implementation of all widgets in Android with clean and modular source code.
To understand the code of each widget or just simply Ctrl+c and Ctrl+v to use it in your project.
- Toggle Button
- Check Box
- Radio Button
- Rating Bar
- Floating Action Button
- Alert Dialog
- Spinner
- SeekBar
- Date Picker
- Time Picker
Only required gradle dependency is -
implementation 'com.google.android.material:material:1.0.0'
Add it in app level gradle file.
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ONN"
android:layout_marginTop="20dp"
/>
private void toggleButton_OnCLickEvent() {
mToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mToggleButton.isChecked())
mStatusToggleButton.setText(R.string.statusToggleOn);
else
mStatusToggleButton.setText(R.string.statusToggleOff);
}
});
}
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Check Box 1"
/>
Use below function to get checked item.
isChecked() : To check if CheckBox is ticked or not
Radio Group ensures that only one radio butoon get selected at a time.
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2" />
</RadioGroup>>
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radioButton1:
mStatusRadioGroup.setText(R.string.RadioButton1Selected);
break;
case R.id.radioButton2:
mStatusRadioGroup.setText(R.string.RadioButton2Selected);
break;
}
}
});
}
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
To get the rating in String format below code can be used.
String rating = String.valueOf(ratingBar.getRating());
com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="480dp"
android:layout_marginStart="280dp"
android:onClick="onClickFloatButton1"
android:clickable="true"
android:src="@drawable/ic_stat_name"
android:focusable="true" />
Just Use onClick attribute of XML to handle click event.
public void onClickFloatButton1(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
No code needed .
AlertDialog.Builder is used to create the the dialog while AlertDialog is used to show it.
private void creatingAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("EXIT");
builder.setMessage("Do you want to close this activity?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Replace the entries with your desired array.
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:entries="@array/cars"
/>
To get the selected item in spinner we have to use the below code.
String item = (String) spinner.getSelectedItem();
<androidx.appcompat.widget.AppCompatSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginEnd="80dp"
android:id="@+id/seekBar"
android:layout_marginTop="20dp"
/>
private void seekBarProgressDisplay() {
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarStatus.setText(String.format(getString(R.string.statusProgress), progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
<DatePicker
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
/>
private String getCurrentDate() {
StringBuilder stringBuilder = new StringBuilder();
int month = datePicker.getMonth() + 1; //As it counts month from 0
int day = datePicker.getDayOfMonth() ;
int year = datePicker.getYear();
stringBuilder.append(day).append("/").append(month).append("/").append(year);
return stringBuilder.toString();
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp" />
private String getCurrentTime() {
int hour=0,min=0;
StringBuilder stringBuilder = new StringBuilder();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hour = timePicker.getHour();
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
min = timePicker.getMinute();
}
stringBuilder.append(hour).append(" : ").append(min);
return stringBuilder.toString();
}
No bugs.