Code Library

Powered by Modelexam | Version 1.0

Layouts

Vertical LinearLayout - #back to top

A vertical LinearLayout arranges its children in a column.


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>

</LinearLayout>

Vertical LinearLayout - #back to top

A horizontal LinearLayout arranges its children in a row.


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3"/>

</LinearLayout>

Vertical LinearLayout - #back to top

A vertical LinearLayout can give all of its children equal height.


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="3"/>

</LinearLayout>

Vertical LinearLayout - leftover height #back to top

A vertical LinearLayout can give one of its children all the leftover height.


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="3"/>

</LinearLayout>

Vertical LinearLayout - #back to top

A horizontal LinearLayout can give all of its children equal width.


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>

</LinearLayout>

Horizontal LinearLayout: leftover width - #back to top

A horizontal LinearLayout can give one of its children all the leftover width.


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="1"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="3"/>

</LinearLayout>

RelativeLayout - #back to top

A RelativeLayout can position a child relative to the RelativeLayout.


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="3"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text="4"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="5"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="6"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="7"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="8"/>

</RelativeLayout>

RelativeLayout - #back to top

A RelativeLayout can position a child relative to another child that has an android:id attribute.


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/center"
android:layout_alignBottom="@id/center"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/center"
android:layout_alignBottom="@id/center"
android:text="3"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"
android:text="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"
android:text="4"/>

</RelativeLayout>

Basic Widgets

TextView - #back to top


<TextView
android:id="@+id/sample_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My photos"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#4689C8"
android:textStyle="bold"
/>

Implementation


TextView textview = (TextView) findViewById(R.id.sample_text_view);
String text = textview.getText().toString();
textview.setText("new Text");
              

Button - #back to top


<Button
android:id="@+id/sample_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Button" />

Java Implementation


Button samplebutton = (Button) findViewById(R.id.sample_button);
samplebutton.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
         Toast.makeText(this,"button clicked",Toast.LENGTH_LONG).show();
    }
});

ToggleButton - #back to top


<ToggleButton
android:text="ToggleButton"
android:textOff="Off state"
android:textOn="On state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/togglebuttonsample" />

Implementation


ToggleButton sampleToggle = (ToggleButton) findViewById(R.id.togglebuttonsample);
sampleToggle.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
         Toast.makeText(this,"toggle button clicked",Toast.LENGTH_LONG).show();
    }
});

CheckBox - #back to top


<CheckBox
android:id="@+id/sample_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify Me"
android:textAppearance="?android:textAppearanceMedium" />

Java Implementation


CheckBox sampleCheckBox = (CheckBox) findViewById(R.id.sample_checkbox);
sampleCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
      Toast.makeText(this,"checkbox checked",Toast.LENGTH_LONG).show();
  }
});

RadioGroup and RadioButton - #back to top


<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/yes_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:textAppearance="?android:textAppearanceMedium" />

<RadioButton
android:id="@+id/no_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:textAppearance="?android:textAppearanceMedium" />

</RadioGroup>

Implementation


  RadioButton yesButton = (RadioButton) findViewById(R.id.yes_radio_button);
  RadioButton noButton = (RadioButton) findViewById(R.id.no_radio_button);

  yesButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
      Toast.makeText(this,"Yes Radiobutton checked",Toast.LENGTH_LONG).show();
    }
  });

  noButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
      Toast.makeText(this,"No Radiobutton checked",Toast.LENGTH_LONG).show();
    }
  });

Spinner - #back to top


<Spinner
android:id="@+id/sample_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Implementation


String[] country = { "India", "USA", "China", "Japan", "Other" };
Spinner sampleSpinner = (Spinner) findViewById(R.id.sample_spinner);

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sampleSpinner.setAdapter(aa);

sampleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  Toast.makeText(this,"Spinner Item Selected",Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

ProgressBar - #back to top


<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="40"/>

<ProgressBar
android:id="@+id/loading_spinner"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Java Implementation


  ProgressBar horizontalBar = (ProgressBar) findViewById(R.id.progress_bar);
  ProgressBar spinnerBar = (ProgressBar) findViewById(R.id.loading_spinner);
  horizontalBar.setProgress(80);

Ratingbar - #back to top


<RatingBar
android:id="@+id/rating_bar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.5"
android:stepSize="0.5" />

Java Implementation


RatingBar sampleRatingbar = (RatingBar) findViewById(R.id.rating_bar);
sampleRatingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
  @Override
  public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
    Toast.makeText(this,"Rated :"+v,Toast.LENGTH_LONG).show();
  }
});
sampleRatingbar.setNumStars(5);

Switch - #back to top

XML code


<Switch
android:id="@+id/switch_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Backup Photos"
android:textAppearance="?android:textAppearanceSmall"
/>

Java Implementation


Switch sampleSwitch = (Switch) findViewById(R.id.switch_sample);
sampleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
      Toast.makeText(this,"Switch checked",Toast.LENGTH_LONG).show();
  }
});

ImageView - #back to top

XML code


<ImageView
android:id="@+id/image_sample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/sample_image"
/>

Java Implementation


ImageView sampleImage = (ImageView) findViewById(R.id.sample_image);
sampleImage.setImageResource(R.drawable.sample_image);

ImageButton - #back to top

XML code


<ImageButton
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageButton"
android:layout_height="wrap_content"
/>

Java Implementation


ImageButton sampleImageButton = (ImageButton) findViewById(R.id.imageButton);

WebView - #back to top

XML code


<WebView
android:layout_width="match_parent"
android:id="@+id/webviewsample"
android:layout_height="match_parent">
</WebView>

Java Implementation


WebView sampleWebView = (WebView) findViewById(R.id.webviewsample);
sampleWebView.loadUrl("http://www.i-visionblog.com");
sampleWebView.getSettings().setJavaScriptEnabled(true);

if (Build.VERSION.SDK_INT >= 19) {
  sampleWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}

VideoView - #back to top

XML code


<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/videoview"/>

Java Implementation


VideoView videoView =(VideoView)findViewById(R.id.videoview);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);

//specify the location of media file
String path = "android.resource://" + getPackageName() + "/" + R.raw.small;

//Setting MediaController and URI, then starting the videoView
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(path));
videoView.requestFocus();
videoView.start();

EditText Widget #back to top

Choose a EditText type and code will auto generate

Person Name
Password
Numeric Password
E-Mail
Phone
Address
MultiLine
Time
Date
Number (Signed)
Number (Decimal)

XML code


      

      
Before seeking support, please...