User Interface with XML
TableLayout
TableLayout allow user Interface to arrange item in rows and Columns in Table format, just like in HTML, <tr> and <td>. A TableLayout consists of a number of TableRow objects, each defining a row (Each TableRow can hold many child Views as columns).
Example: Create a new project. add RattingBar, ToggleButton, ProgressBar, TextView and Button from UI of You IDE. OR copy The Code Below.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:gravity="center"
android:paddingRight="16dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginRight="10dp"
android:text="Notification"
android:textColor="#000" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewRate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="Rate"
android:textColor="#000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:gravity="center"
android:paddingRight="16dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginRight="10dp"
android:text="Notification"
android:textColor="#000" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewRate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="Rate"
android:textColor="#000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow> <TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
Result: Run Application
package com.example.plush.helloworld;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends ActionBarActivity {
ToggleButton toggleButton;
RatingBar rating;
TextView textView;
Button button;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButtonListener();
ratingBarListenr();
buttonAndProgressbar();
}
public void toggleButtonListener() {
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
Toast.makeText(getApplicationContext(), "Notification is turned ON", Toast.LENGTH_LONG).show();
} else {
// The toggle is disabled
Toast.makeText(getApplicationContext(), "Notification is turned OFF", Toast.LENGTH_LONG).show();
}
}
});
}
public void ratingBarListenr() {
rating = (RatingBar) findViewById(R.id.ratingBar);
textView = (TextView) findViewById(R.id.textViewRate);
//display rating value in textview if value changed
rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
textView.setText(String.valueOf(rating));
}
});
}
private void buttonAndProgressbar() {
button = (Button) findViewById(R.id.submit);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
//Make progressBar invisiable at the beggining
progressBar.setVisibility(View.INVISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Make progressBar Visiable at the beggining
progressBar.setVisibility(View.VISIBLE);
//Run a thread to wait for 3 seconds.
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//After 3 seconds make progresbar invisiable again.
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Result: Run Application
No comments:
Post a Comment