Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android-View Elements

TextView: It is used to display text on the screen, and it is a very important UI element for Android.

EditText: Allows the user to enter and edit text.

Button: It is used to perform some action on click.

ImageView: It displays any kind of image from Bitmap or Drawable, add your image in the drawable or mipmap and use it.

ProgressBar: Shows the progress of an ongoing task.

  • CheckBox: It is mostly used where the user can select one or more than one option from the given list option.

Here is the .xml code for the checkbox,

Now the how to call checkbox class,

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CheckBox check1, check2, check3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        check1=(CheckBox)findViewById(R.id.check_one);
        check2=(CheckBox)findViewById(R.id.check_two);
        check3=(CheckBox)findViewById(R.id.check_three);
    }
    
    //call on the click on button
    public void Check(View v){
        if(check1.isChecked())
            Toast.makeText(this, "one", Toast.LENGTH_SHORT).show();
        if(check2.isChecked())
            Toast.makeText(this, "two", Toast.LENGTH_SHORT).show();
        if(check3.isChecked())
            Toast.makeText(this, "three", Toast.LENGTH_SHORT).show();
    }

}
  • RadioButton: It is used when the user can choose only one option from a group and compulsory needs to choose one option.

Here the .xml code, in the xml file under a layout user has write the following code,

After that call the RadioGroup and RadioButton in the MainActivity.java and perform the action on the button.

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                if (radioButton != null) {
                    String selectedOption = radioButton.getText().toString();
                    Toast.makeText(MainActivity.this, "Selected: " + selectedOption, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}

ToggleButton: It is an on/off button it displays the current state of ToggleButton.

Here is How to perform on click on ToggleButton,

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    ToggleButton togglebutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        togglebutton = (ToggleButton)findViewById(R.id.toggleButton);
    }
    public void onToggleClick(View view)
    {
        if (togglebutton.isChecked()) {
            Toast.makeText(this, "Toggle is ON", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Toggle is OFF", Toast.LENGTH_SHORT).show();
        }
    }
}

Switch: Represents a two-state operation switch (on or off).

In this here check the status of the switch and add a check change listener, in the MainActivity.java,

public class MainActivity extends AppCompatActivity {

    private Switch switchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        switchView = findViewById(R.id.idSwitch);

        //here the we check is the status
        if (switchView.isChecked()) {
            Toast.makeText(MainActivity.this, "Switch is Checked", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Switch is UnChecked", Toast.LENGTH_SHORT).show();
        }

        //here we are add check change listener
        switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(MainActivity.this, "Switch is Checked", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Switch is UnChecked", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Spinner: Displays a dropdown list of options, this is the easy way of show of the list.

Here in the Main Activity implements Adapter view, Array adapter used for the bind data to the spinner.

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    String[] numbers = { "one", "two", "three", "four", "five", "Six" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spin = findViewById(R.id.spinner);
        spin.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

        //Create the instance of ArrayAdapter
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,numbers);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //set adapter
        spin.setAdapter(adapter);

    }

    @Override
    public void onItemClick(AdapterView> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), numbers[position], Toast.LENGTH_LONG).show();
    }
}
  • CardView: It is a flexible and adjustable container for showing data or material in a card-like style offered by the UI component CardView. It is a component of the AndroidX framework and is frequently used to design cards that look uniformly good on all devices.

Firstly user has added to add card dependency in the build.gradle file,

implementation 'androidx.cardview:cardview:1.0.0'

Some common attributes include:

  1. cardElevation
  2. cardCornerRadius
  3. cardBackgroundColor
  4. cardUseCompatPadding

Add desired content in the CardView like textviews, imageviews, etc.


    
  • DatePicker: DatePicker is used to select dates by day, month, and year in the Android app. The Date picker has two modes, first to show the complete calendar and second to show the date in spinner mode.

This is used when the user wants a calendar,

This is used when the user wants date in spinner mode,

  • TimePicker: Allows the user to select a time by hour and minute.

Here is the .xml code:

In the .java file code for the time picker:

public class MainActivity extends AppCompatActivity  {
    TimePicker timepicker;
    Button changetime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timepicker=(TimePicker)findViewById(R.id.timePicker);

        //code for 24 hour view
        timepicker.setIs24HourView(true);
        changetime=(Button)findViewById(R.id.button1);
        changetime.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, getCurrentTime(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    public String getCurrentTime(){
        String currentTime="Current Time: "+timepicker.getCurrentHour()+":"+timepicker.getCurrentMinute();
        return currentTime;
    }
}
  • SeekBar: It is a kind of ProgressBar represents a slider that allows selecting a value from a range.

Define these line codes under the .xml file,

Now here how to perform seekbar event,

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity  {

    SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar=(SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Toast.makeText(MainActivity.this, "progress", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "stop", Toast.LENGTH_SHORT).show();
            }
        });

    }

}
  • RatingBar: Shows a rating indicator, normally represented as a star.

Code for .xml,

Code for rating the user:

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity  {

    RatingBar ratingbar;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnButtonClick();
    }
    public void OnButtonClick() {
        ratingbar = (RatingBar) findViewById(R.id.ratingBar);
        button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Getting the rating and displaying it on the toast
                String rating = String.valueOf(ratingbar.getRating());
                Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
            }

        });
    }
}
  • WebView: A view inside the application called WebView shows web pages.

Need to add permission under the AndroidManifest.xml for the access of internet.

In the .xml file add these lines,

In the .java file:

package com.example.loginretrofit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = findViewById(R.id.web);

        // loading url in the WebView.
        webView.loadUrl("https://www.google.org");
        webView.getSettings().setJavaScriptEnabled(true);

        // onPageFinished and override Url loading.

        webView.setWebViewClient(new WebViewClient());
    }
}

The post Android-View Elements appeared first on PHPGurukul.



This post first appeared on PHP Gurukul, please read the originial post: here

Share the post

Android-View Elements

×

Subscribe to Php Gurukul

Get updates delivered right to your inbox!

Thank you for your subscription

×