UI component: ImageButton, Togglebutton, ProgressBar

Hello guys, welcome back to another android studio project tutorial. in this tutorial, I am going to showing you about “How to create UI components in the android studio” easily.

Let’s get started…

Here I’m gonna teach you to create an application having the following UI component:

  1. ImageButton
  2. Toggle button
  3. ProgressBar

Check This –> Display toast (Message) on the specific interval of Time

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml
4 create src/Main2Activity.java
5 create res/layout/activity_main2.xml
6  Modify src/MainActivity2.java and add the code
7  Modify layout XML file res/layout/activity_main2.xml

Content of src/MainActivity.java 

package com.example.kenilvavaliya.navojay;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
ProgressBar pbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton)findViewById(R.id.imageButton);
pbar = (ProgressBar)findViewById(R.id.progressBar2);
}

public void onK(View v)
{
Thread t1 = new Thread()
{
@Override
public void run(){
super.run();
for (int i=0;i<=100;){
try{
sleep(700);
}
catch (InterruptedException e){
e.printStackTrace();
}
pbar.setProgress(i);
i=i+15;
}
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
};
t1.start();

}
}

Content of res/layout/activity_main.xml

<?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="match_parent"
tools:context=".MainActivity">

<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="157dp"
app:srcCompat="@android:drawable/ic_menu_send"
android:onClick="onK"/>

<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="108dp"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="184dp"
android:onClick="onK" />
</RelativeLayout>

After that left click on java folder go New > Activity > Empty Activity

It will create a new java and XML file.

Content of src/Main2Activity.java 

package com.example.kenilvavaliya.navojay;

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

public class Main2Activity extends AppCompatActivity {
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
}
public void onChaman(View v){
if(toggleButton.isChecked())
{
Toast.makeText(this,"ON",Toast.LENGTH_SHORT).show();;

}
else {
Toast.makeText(this, "OFF", Toast.LENGTH_SHORT).show();
}
}
}

Content of res/layout/activity_main2.xml

<?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="match_parent"
tools:context=".Main2Activity">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onChaman"
android:text="ToggleButton"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="193dp" />
</RelativeLayout>

Output:

Stay Connect with our app: – 

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

 

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*