....................................................................................................................................................................................................................................
Linear Layout
Linear Layout
. Arrange view linearly - Horizontally or vertically
. android : orientations attribute to define the direction
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="button 2"
/>
android:background='@color/colorAccent'
Example #
<Button
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" // remain space
android:text="Button 1"
/>
<Button
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>
Example #
<Button
android:layout_width ="wrap_content"
or below one performance purpose
//android:layout_width ="0dp"
android:layout_weight="70" // remain space
android:text="Button 1"
/>
<Button
android:layout_width ="wrap_content"
or below one performance purpose
//android:layout_width ="0dp"
android:layout_weight="30" // remain space
android:text="Button 1"
/>
Example # Relative Layout
Example #
Example #
Step 1
Java class
package com.share;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SaharedataActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void shareData(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "This is my shared text");
startActivity(Intent.createChooser(intent, "Share this text via"));
}
}
........................................................................................................................
Step -2
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C0C0C0"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share Data"
android:onClick="shareData"
/>
</LinearLayout>
out put
ConstraintLayout
ConstraintLayout is a layout on Android that gives you adaptable and flexible ways to create views for your apps.
<ImageView
android:id="@+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="@drawable/space_station_icon"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="@drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="@+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="@+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="@+id/roverIcon"
app:layout_constraintStart_toEndOf="@+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="@+id/spaceStationIcon" />
<ImageView
android:id="@+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="@+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/flightsIcon"
app:layout_constraintTop_toTopOf="@+id/flightsIcon" />
<TextView
android:id="@+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/rovers"
app:layout_constraintEnd_toEndOf="@+id/roverIcon"
app:layout_constraintStart_toStartOf="@+id/roverIcon"
app:layout_constraintTop_toBottomOf="@+id/roverIcon" />
<TextView
android:id="@+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/flights"
app:layout_constraintEnd_toEndOf="@+id/flightsIcon"
app:layout_constraintStart_toStartOf="@+id/flightsIcon"
app:layout_constraintTop_toBottomOf="@+id/flightsIcon" />
<TextView
android:id="@+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/space_stations"
app:layout_constraintEnd_toEndOf="@+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="@+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="@+id/spaceStationIcon" />
Circle images
<ImageView
android:id="@+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/rocket_icon"
app:layout_constraintCircle="@id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
by default is Horizontal
ListView[ kotlin]
...........................................................................................................................................................................................................................
val countries = arrayOf<String>("USA", "INDIA", "DUBAI")
val mylistview = findViewById<ListView>(R.id.mylist)
mylistview.adapter= ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries)
mylistview.setOnItemClickListener { parent, view, position, id ->
val toast = Toast.makeText(this, "HEllo\n " + countries[position], Toast.LENGTH_SHORT)
toast.setGravity(Gravity.CENTER, 0 , 0)
toast.show()
}
...........................................................................................................................................................................................................................
RelativeLayout [ Login UI ]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="username"
android:layout_marginTop="120dp"
android:inputType="textPersonName"
android:ems="10"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="password"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="10dp"
android:layout_below="@+id/edit1"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@+id/edit2"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="100dp"
/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_below="@+id/edit2"
android:layout_alignParentRight="true"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
/>
</RelativeLayout>
..................................................................................................................................................................................................................................
Display userName
val myUser = findViewById<EditText>(R.id.username) val btn = findViewById<Button>(R.id.login) btn.setOnClickListener { val strMessage:String strMessage= myUser.getText().toString() Toast.makeText(this, "Hello $strMessage" , Toast.LENGTH_SHORT).show() }
No comments:
Post a Comment