0%
Loading ...

Android UI

This category contains all the tutorials related to Android UI.

RecyclerView in Android

In this tutorial, we will learn about RecyclerView in Android. Recycler view is introduced from android 6.0(Marshmallow). RecyclerView is used to create a list or grids in android. You can say it RecyclerView is the next version of ListView. RecyclerView provides much better performance than ListView. Here we will create simple RecyclerView. Info! Please follow each step carefully. Implement Dependencies: Our first step would be to add two dependencies in our project. so add these two given dependencies in your Gradle (Module: app). implementation ‘com.android.support:recyclerview-v7:26.1.0′ Now write the following code in main_activity.xml to add RecyclerView in activity. <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” xmlns:app=”http://schemas.android.com/apk/res-auto” android:layout_width=”match_parent” android:layout_height=”match_parent” app:layout_behavior=”@string/appbar_scrolling_view_behavior” tools:showIn=”@layout/activity_main” tools:context=”.MainActivity”> <android.support.v7.widget.RecyclerView android:id=”@+id/recycler_view” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:scrollbars=”vertical” /> </RelativeLayout> Our next step is to create model. Name this class as DataModel.java.(Model totally depends upon the requirements of project. Here we are just taking two variables title and description in model). public class DataModel { private String post_title; private String post_description; public TopicTitleModel(String post_title, String post_description) { this.post_title = post_title; this.post_description = post_description; } public String getPost_title() { return post_title; } public String getPost_description(){ return post_description; } } Our next step is to create layout for recyclerView item. Design is totally depends upon the project requirements. Here we are just creating simple layout to show title and description. we named this layout as single_item_layout.xml. <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:clickable=”true” android:focusable=”true” android:orientation=”vertical” android:padding=”16dp” > <TextView android:id=”@+id/title” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_alignParentTop=”true” android:textSize=”16dp” android:textStyle=”bold” /> <TextView android:id=”@+id/description” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_below=”@id/title” /> </RelativeLayout> After creating single_item_layout.xml our next step is to create a Adapter for RecyclerView. We named this adapter as PostAdapter.java. public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> { List<DataModel> list=new ArrayList<>(); Context context; public TitlesAdapter(List<DataModel> list, Context context){ this.list=list; this.context=context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_item_layout,viewGroup,false); return new ViewHolder(v); } public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) { viewHolder.text.setText(list.get(i).getPost_title()); viewHolder.description.setText(list.get(i).getPost_description()); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ TextView text; TextView description; public ViewHolder(@NonNull View itemView) { super(itemView); text=itemView.findViewById(R.id.title); description=itemView.findViewById(R.id.description); } } } No we reached at our final step. In this step we will get Recycler we in MainActivity.java then we will set adapter and LayoutManager to RecyclerView. public class MainActivity extends AppCompatActivity { private List<DataModel> list = new ArrayList<>(); private RecyclerView recyclerView; private PostAdapter postAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); postAdapter = new PostAdapter(movieList,getApplication()); prepareData(); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(postAdapter); } private void prepareData() { list.add(new DataModel(“Color Red”,”Apple is Red”)); list.add(new DataModel(“Color Blue”,”Sky is Blue”)); list.add(new DataModel(“Color Green”,”Grass is Green”)); } }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

RecyclerView in Android Read More »

ListView in Android

In this tutorial, we will learn about ListView in Android. ListView is one of the most commonly used widgets. We normally see ListView in different Android Apps, for example, contact list. Here, we will learn how we can create a simple list in Android and how we can create a custom ListView in Android. First of all we will add ListView Widget in XML layout file. Check this code. XML Code: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” tools:context=”.MainActivity”> <ListView android:layout_height=”match_parent” android:layout_width=”match_parent” android:id=”@+id/listView” /> </LinearLayout> To add listView in Activity we need an adapter which will help us to merge data and view together in ListView. We can provide data to list in the format of array or list. Here we will add simple ArrayAdapter. Check this Java code. Java Code: public class MainActivity extends AppCompatActivity { String colors[]={“Red”,”Green”,”Blue”}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView=findViewById(R.id.listView); ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,colors); listView.setAdapter(arrayAdapter); } } Here is output after writing all this code. Custom ListView in Android: To make a custom ListView in android first of all we have to create a layout for single item of list and then we have to create a custom adapter to create listview. Here you have to crate new layout file. Here I just create new layout file and named it as list_item_layout Check this code to create a custom list item. List Item XML Code: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”match_parent” android:layout_height=”wrap_content”> <TextView android:layout_width=”match_parent” android:layout_height=”wrap_content” android:textSize=”18dp” android:textColor=”@color/colorPrimaryDark” android:id=”@+id/text1″/> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:textSize=”14dp” android:id=”@+id/text2″/> </LinearLayout> After creating this layout file we will create custom adpater to add data in this custom list view. To create a custom adapter just add a new java class in your android project and inherit BaseAdapter class in this class. BaseAdapter is an abstract class it mean you have to override all the abstract methods of this class into user class. Here i will create new class called MyListAdapter to create a custom adapter check this code Custom Adapter Java Code: public class MyListAdpater extends BaseAdapter { String array1[]={“Red”,”Blue”,”Green”}; String array2[]={“Rose is Red”,”Sky is Blue”,”Grass is Green”}; LayoutInflater layoutInflater; public MyListAdpater(Context context){ layoutInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return array1.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v=layoutInflater.inflate(R.layout.list_item_layout,null,false); TextView text1=v.findViewById(R.id.text1); TextView text2=v.findViewById(R.id.text2); text1.setText(array1[i]); text2.setText(array2[i]); return v; } } Here our custom list item design is ready and our adapter is also ready. Now we will change the adapter of our listview from ArrayAdapter to MyListAdapter in MainActivity. Main Activity Java Code: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView=findViewById(R.id.listView); MyListAdpater myListAdpater=new MyListAdpater(getApplicationContext()); listView.setAdapter(myListAdpater); } } check this Output after adding CustomAdapter to our ListView. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

ListView in Android Read More »

WebView in Android

In this tutorial, we will learn how to use a WebView in Android. We use WebView to show webpages in Android. Using WebView you can load a web page from the same application or you can load a web page from any URL. To use web view in android you must have internet permission in your android manifest file. In this tutorial, we will learn how to add WebView in Android XML file and how to load URL in this WebView using our Java file. Before doing anything we will add internet permission in our AndroidManifest.xml file. Because without Internet permission our web view will not work. Add in Manifest file: <uses-permission android:name=”android.permission.INTERNET”/> Now we will move to our next step. So start with our XML file. In this step, we will add a Web View widget in our activity. XML Code: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” tools:context=”.MainActivity”> <WebView android:id=”@+id/webView” android:layout_width=”match_parent” android:layout_height=”match_parent” /> </LinearLayout> After adding this XML code in XML layout file add this code to Java file where we will write code to load a web page in our WebView. Java Code: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final WebView webView=findViewById(R.id.webView); webView.loadUrl(“https://www.google.com”); } } After adding this code in Java file run project and you will get Google Home page on your activity because we have written URL of google in loadURL method of WebView class. Here you can try to load different URLs. Check Output of this project. Output: Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

WebView in Android Read More »

RatingBar in Android

In this tutorial, we will learn how to use RatingBar in Android. RatingBar is used to get the rating from users. User can give rating by clicking on starts. When the user clicks on starts of Ratingbar it returns float value like 2.5, 1.0, 4.5 etc. In this tutorial, we will learn how to add Ratingbar in the XML layout file. How to get Rating in Java file. First of all, we will write our layout file code. In the layout file, we will add a RatingBar widget and a button. After that, we will write code in Java file to get rating given by the user on button click. Check this XML code. XML Code: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” android:padding=”16dp” tools:context=”.MainActivity”> <RatingBar android:id=”@+id/ratingbar” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:saveEnabled=”true” android:numStars=”5″ android:layout_marginBottom=”10dp” /> <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Submit” android:id=”@+id/submit”/> </LinearLayout> After writing this code we will write code in our java file to fetch rating given by the user. Java Code: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RatingBar ratingBar=findViewById(R.id.ratingbar); final Button button=findViewById(R.id.submit); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { float rating=ratingBar.getRating(); Toast.makeText(getApplicationContext(),rating+””,Toast.LENGTH_SHORT).show(); } }); } } Output: Here is the output after writing this code. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

RatingBar in Android Read More »

SeekBar in Android

In this tutorial, we will learn how to use SeekBar in Android. The Seekbar is a child class of AbsSeekBar class. Seekbar is kind of progress bar and Seekbar allow users to move thumb of Seekbar from left or right. For example, in media players, you can move the song forward or backwards by moving a Seekbar. Here we will learn how to add SeekBar in the XML layout file and how to get change listener of SeekBar in android Java file. XML Code: <?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:padding=”16dp” android:orientation=”vertical” android:gravity=”center” tools:context=”.MainActivity”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”0″ android:layout_marginBottom=”15dp” android:id=”@+id/textView” android:textSize=”18sp”/> <SeekBar android:layout_width=”match_parent” android:layout_height=”wrap_content” android:id=”@+id/seekBar”/> </LinearLayout> Output: After creating this XML layout file we will move to our Java file In this file we will use seekbar change listener to track the change in seekbar. Java Code: public class MainActivity extends AppCompatActivity { SeekBar seekBar; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar=(SeekBar)findViewById(R.id.seekBar); textView=(TextView)findViewById(R.id.textView); textView.setTextColor(Color.BLACK); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textView.setText(progress+””); } @Override public void onStartTrackingTouch(SeekBar seekBar) { textView.setTextColor(Color.GREEN); } @Override public void onStopTrackingTouch(SeekBar seekBar) { textView.setTextColor(Color.BLACK); } }); } }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

SeekBar in Android Read More »

Spinner in Android

In this tutorial, we will learn about Spinner in Android. If you ever worked in Java AWT or Swing then you might be familiar with ComboBox. Spinner is same as those ComboBoxes. Spinner allows a user to choose from multiple choices and the user can select only one option at a single time. For example, you are making an app for an educational institute and you have an activity in your application where a user can apply for a course. In this kind of situation, you can use Spinner which would allow the user to select one course from many. In this tutorial firstly we will learn about Array Adapters then we will move to Spinners. Array Adapter : – An ArrayAdapter is a class in Android which work like a bridge between UI components and source of data. The data can be in any form like in the form of a list, in the form of Cursor object or in the form of Array. Array adapter mainly works with simple text data. In this tutorial, we will use an Array as our data source. To understand more about ArrayAdapter we will follow an illustration. In this illustration, you can see Adapter is working in the centre of our Data source and Adapter View. The adapter will take data from the data source and will send it to Adapter View and after merging data and adapter view. We will set this adapter to our spinner. It might be confusing at present for many students but after seeing examples you will find it easy. So to start with spinner firstly we will add spinner in our main XML layout file. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Spinner in Android Read More »

RadioButton in Android

In this tutorial, we will learn about RadioButton in Android. Radio buttons are kind of buttons which has only two states checked or unchecked the same as CheckBox expects with little differences. RadioButton in Android is generally used with another widget which in RadioGroup. Suppose a RadioGroup contain two Radio Buttons it means we can select only one RadioButton at a single time. We can not select both Radio Buttons once. For Example, we have Radio Group of two RadioButtons with Male or Female option in our signup form in this case user can only select one radio button to mention their gender. Throughout this tutorial learn how to add RadioButtons in RadioGroup widget in the XML layout file and how to get selected RadioButton in Android in particular RadioGroup using Java file. XML Code: <?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:padding=”16dp” android:orientation=”vertical” tools:context=”.MainActivity”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginBottom=”15dp” android:text=”Gender” android:textSize=”18sp”/> <RadioGroup android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_marginBottom=”15dp” android:id=”@+id/gender”> <RadioButton android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Male” android:id=”@+id/male”/> <RadioButton android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Female” android:id=”@+id/female”/> </RadioGroup> <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Submit” android:id=”@+id/submit”/> </LinearLayout> Output: We got this output after writing this code in out XML file. Here we will write java code to make things working. Java Code: public class MainActivity extends AppCompatActivity { RadioGroup gender; Button submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gender=(RadioGroup)findViewById(R.id.gender); submit=(Button)findViewById(R.id.submit); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (gender.getCheckedRadioButtonId()){ case R.id.male: Toast.makeText(getApplication(),”You Selected Male”,Toast.LENGTH_SHORT).show(); break; case R.id.female: Toast.makeText(getApplication(),”You Selected Female”,Toast.LENGTH_SHORT).show(); break; } } }); } }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

RadioButton in Android Read More »

CheckBox in Android

In this tutorial, we will learn about CheckBox in Android. The CheckBox is a kind of button which only have two states checked or unchecked. The CheckBox can be used in my different ways, for example, to show different options in the app that user can select multiple from those options, In this kind of situation you can use CheckBox. We will learn how to add CheckBox in Android and how to use different methods related to CheckBox. Firstly we will learn how to add CheckBox widget in XML. XML Code: <?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:padding=”16dp” android:orientation=”vertical” tools:context=”.MainActivity”> <CheckBox android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:src=”@drawable/owlbuddy” android:textSize=”20dp” android:id=”@+id/notifications” android:layout_marginBottom=”10dp” android:text=”Notifications”/> <View android:layout_width=”match_parent” android:layout_height=”1dp” android:layout_marginBottom=”10dp” android:background=”@color/colorPrimary”/> <CheckBox android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:src=”@drawable/owlbuddy” android:id=”@+id/updates” android:layout_marginBottom=”10dp” android:textSize=”20dp” android:text=”Auto Updates”/> <View android:layout_width=”match_parent” android:layout_height=”1dp” android:layout_marginBottom=”10dp” android:background=”@color/colorPrimary”/> <CheckBox android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:layout_marginBottom=”10dp” android:id=”@+id/history” android:src=”@drawable/owlbuddy” android:textSize=”20dp” android:text=”Clear History”/> <View android:layout_width=”match_parent” android:layout_height=”1dp” android:layout_marginBottom=”10dp” android:background=”@color/colorPrimary”/> </LinearLayout> Output: Now we will learn how to perform some action when someone will check these CheckBoxes. Firstly will initialize all these CheckBoxes in our java file with findViewById() method and then we will call to setOnCheckedChangeListener() to these CheckBoxes. Java Code: public class MainActivity extends AppCompatActivity { CheckBox notifications,updates,history; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notifications=(CheckBox)findViewById(R.id.notifications); updates=(CheckBox)findViewById(R.id.updates); history=(CheckBox)findViewById(R.id.history); notifications.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonifView, boolean isChecked) { if(isChecked) Toast.makeText(getApplication(),”Notifications On”,Toast.LENGTH_SHORT).show(); } }); updates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) Toast.makeText(getApplication(),”Auto Updates On”,Toast.LENGTH_SHORT).show(); } }); history.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) Toast.makeText(getApplication(),”Clear History On”,Toast.LENGTH_SHORT).show(); } }); } } Output: Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

CheckBox in Android Read More »

ImageView in Android

In this tutorial, we will learn how to use ImageView in Android. ImageView is really helpful widget and we use this widget to add Images in our Android App. Here we will learn how to use drawable images in ImageView. We will also learn how we can add images in ImageView using the XML layout file. So without wasting time let’s directly jump to our Examples. Before writing any code firstly we will add an image in our project. To do this, you can copy an image from anywhere in PC and then you have to add the image in res/drawable folder of our project. While adding an image always keep it in your mind image size should be less than 1MB and image name should be in small letter. <?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:gravity=”center” android:padding=”16dp” android:orientation=”vertical” tools:context=”.MainActivity”> <ImageView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:src=”@drawable/owlbuddy”/> </LinearLayout> ImageView in Android Output: Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

ImageView in Android Read More »

Toast in Android

In this tutorial, we would learn about Toast in Android. We use Toast in Android to show small piece of information for small time. For example, we want to show a message like “Signup Successfully” or “User Name Already Taken” after a user will press the signup button. In that kind of situations toast is a best option to give a small message to user. In this tutorial, we will learn how can we show a Toast in our Android App. Toast is class already available in Android. we can use that class to show Toast in our App and We can also create a custom Toast in Android. In this tutorial we will also learn how we can create a custom Toast in Android. XML Code: <?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:gravity=”center” android:padding=”16dp” android:orientation=”vertical” tools:context=”.MainActivity”> <Button android:layout_width=”250dp” android:layout_height=”wrap_content” android:layout_marginBottom=”10dp” android:textColor=”@color/colorWhite” android:background=”@color/colorPrimary” android:id=”@+id/button” android:text=”Show Toast”/> </LinearLayout> In this layout file, I added a button and I will show a Toast whenever someone presses this Button in this Activity. Java Code: public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Toast Example Toast.makeText(MainActivity.this,”Learn Android with Owlbuddy”,Toast.LENGTH_SHORT).show(); } }); } } To create Toast in Android we have a static method “makeText” in “Toast” class. so that’s why I called that static method from “Toast” class. This makeText method gets three parameters. First Parameter: The first parameter of makeText method is context. So to give context I added here MainActivity.this. Second Parameter: The second parameter is your message. The message which you want to show in your Toast. I added here “Learn Android with Owlbuddy”. Thirst Parameter: The third Parameter in makeText is duration mean for how much time you want to show this message. To set duration we have two static variables in Toast class Toast.LENGTH_SHORT and Toast.LENGTH_LONG. I hope so now you would have a clear idea of how to add a simple Toast in our Android App. Now we will learn how to create a “Custom Toast” in Android. For this rest of the tutorial, I would keep “main_activity.xml” file the same. I will just add a new layout file to give layout to my custom toast and I will also change some code in MainActivity java file to add custom toast. Custom Toast XML code: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:orientation=”vertical” android:gravity=”center” android:padding=”15dp” android:id=”@+id/toast_layout” android:background=”@color/colorPrimary” android:layout_gravity=”center”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”This is Toast” android:textColor=”@color/colorWhite”/> </LinearLayout> MainActivity Java Code: public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); LayoutInflater layoutInflater=getLayoutInflater(); final View view=layoutInflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Toast Example Toast toast=new Toast(MainActivity.this); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); } }); } }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Toast in Android Read More »

Scroll to Top
×