Wednesday 18 October 2017

15. Using RadioButton & RadioGroup in Android

15. Using RadioButton & RadioGroup

in Android


In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added in it will automatically unchecked all the others. It means at one time we can checked only one radio button from a group of radio buttons which belong to same radio group.

RadioButon is a two state button that can be checked or unchecked. If a radio button is unchecked then a user can check it by simply clicking on it. Once a RadiaButton is checked by user it can’t be unchecked by simply pressing on the same button. It will automatically unchecked when you press any other RadioButton within same RadioGroup.

RadioActivity.java

package tutorial.android.sachin4droid.androidtutorials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class RadioActivity extends AppCompatActivity {
  RadioButton radio1,radio2,radio3;
  Button radioSubmit;

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

      radioSubmit = (Button) findViewById(R.id.radioSubmit);
      radio1 = (RadioButton) findViewById(R.id.radio1);
      radio2 = (RadioButton) findViewById(R.id.radio2);
      radio3 = (RadioButton) findViewById(R.id.radio3);
      radioSubmit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(radio1.isChecked()){
                  Toast.makeText(RadioActivity.this, radio1.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }
              if(radio2.isChecked()){
                  Toast.makeText(RadioActivity.this, radio2.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }
              if(radio3.isChecked()){
                  Toast.makeText(RadioActivity.this, radio3.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }

          }
      });



  }
}


Activity_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="tutorial.android.sachin4droid.androidtutorials.RadioActivity">

  <RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:layout_editor_absoluteX="103dp"
      tools:layout_editor_absoluteY="94dp" >

      <RadioButton
          android:id="@+id/radio1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio1"/>
      <RadioButton
          android:id="@+id/radio2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio2"/>
      <RadioButton
          android:id="@+id/radio3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio3"/>

      </RadioGroup>

  <Button
      android:id="@+id/radioSubmit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Submit"/>
</LinearLayout>


14. Using Spinner in Android

14. Using Spinner in Android


Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

For adding listener when any item selected from spinner:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);


SpinnerActivity.java

package tutorial.android.sachin4droid.androidtutorials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerActivity extends AppCompatActivity {

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

      Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
              R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
      spinner.setAdapter(adapter);
  }
}


Activity_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
  tools:context="tutorial.android.sachin4droid.androidtutorials.SpinnerActivity">


  <Spinner
      android:id="@+id/spinner"
      android:layout_width="368dp"
      android:layout_height="wrap_content"
      tools:layout_editor_absoluteX="8dp"
      tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>

Strings.xml

<resources>
  <string name="app_name">Android Tutorials</string>

  <!-- TODO: Remove or change this placeholder text -->
  <string name="hello_blank_fragment">Hello blank fragment</string>
  <string-array name="planets_array">
      <item>Mercury</item>
      <item>Venus</item>
      <item>Earth</item>
      <item>Mars</item>
      <item>Jupiter</item>
      <item>Saturn</item>
      <item>Uranus</item>
      <item>Neptune</item>
  </string-array>

</resources>


13. Using Scrollview in Android

13. Using Scrollview in Android


A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.
Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead.
Never add a RecyclerView or ListView to a scroll view. Doing so results in poor user interface performance and a poor user experience.

Some points for Scroll View:


  1. ScrollView can hold only one direct child. This means that, if you have complex layout with more view controls, you must enclose them inside another standard layout like LinearLayout, TableLayout or RelativeLayout.
  2. You can specify layout_height and layout_width to adjust height and width of screen.
  3. Scrollview is ideal for screens where scrolling is required, but it is an overhead when scroll view is used to render a larger list of data. Android provides specialized adapter views like ListView, GridView and Recycler View (Introduced in Android Lollipop) are recommended for long lists.
  4. You should never use a ScrollView with a ListView or GridView, because they both takes care of their own vertical scrolling.
  5. ScrollView only supports vertical scrolling. Use HorizontalScrollView for horizontal scrolling.
  6. The android:fillViewport property defines whether the scrollview should stretch its content to fill the viewport. You can set the same property by calling setFillViewport(boolean) method.

ScrollViewActivity.java

package tutorial.android.sachin4droid.androidtutorials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ScrollViewActivity extends AppCompatActivity {

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

Activity_scroll_view.xml


<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:layout_height="match_parent" android:layout_width="match_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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"
  tools:context="tutorial.android.sachin4droid.androidtutorials.ScrollViewActivity">
  <TextView
      android:id="@+id/scrollViewTestTextView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:textSize="30dp"
      android:text="ScrollView can hold only one direct child. This means that, if you have complex layout with more view controls, you must enclose them inside another standard layout like LinearLayout, TableLayout or RelativeLayout.
You can specify layout_height and layout_width to adjust height and width of screen.
Scrollview is ideal for screens where scrolling is required, but it is an overhead when scroll view is used to render a larger list of data. Android provides specialized adapter views like ListView, GridView and Recycler View (Introduced in Android Lollipop) are recommended for long lists.
You should never use a ScrollView with a ListView or GridView, because they both takes care of their own vertical scrolling.
ScrollView only supports vertical scrolling. Use HorizontalScrollView for horizontal scrolling.
The android:fillViewport property defines whether the scrollview should stretch its content to fill the viewport. You can set the same property by calling setFillViewport(boolean) method."
      />
</LinearLayout>
</HorizontalScrollView>





Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...