Menus

Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Tuesday 4 June 2019

Android Drop Down List Tutorial



Spinner in Android application is equivalent of ComboBox

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


You can add a spinner to your layout with the spinner.

For example

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



List of Items in Spinner

Open “res/values/strings.xml” file, define the list of items that will display in Spinner 

File : res/values/strings.xml
<resources>
    <string name="app_name">Layouts</string>

    <string name="prompt_country">Select a Country</string>

    <string-array name="country_list">
        <item>India</item>
        <item>United States</item>
        <item>Singapore</item>
        <item>Malaysia</item>>
        <item>France</item>
        <item>Italy</item>
        <item>New Zealand</item>
    </string-array>

</resources>


Spinner  selection items will be defined in xml file


<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_list"
         >
</Spinner>

Other method :

<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/prompt_country"
        android:entries="@array/country_list"
        android:spinnerMode="dialog"
         >

 </Spinner>

Spinner selection items will be defined in code

add code in onCreate method

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.country_list));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnCountry.setAdapter(myAdapter);


    }
}


Video





How to set selected item of Spinner programmatically in Android

Get spinner selected items text?

This example enplane value display on button click 

Java code
       Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                         Toast.makeText(TestActivity.this,String.valueOf(spnCountry.getSelectedItem()),Toast.LENGTH_LONG).show();
            }

        });

Example 2


Spinner spnCountry = (Spinner)findViewById(R.id.spnCountry);
String text = spnCountry.getSelectedItem().toString();


Handle spinner click event 


Spinner setOnItemSelectedListener


           spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(TestActivity.this,parent.getItemAtPosition(position) + " Selected",Toast.LENGTH_LONG).show();
            }

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

            }
        });


Set Key and Value in spinner

SpinnerCountry.java

package com.example.root.spinner;

/**
 * Created by root on 8/6/19.
 */
public class SpinnerCountry {

    public String id;
    public String country;

    public SpinnerCountry(String id,String country) {
        this.country = country;
        this.id = id;
    }

    @Override
    public String toString() {
        return country;
    }

}


MainActivity.java


package com.example.root.spinner;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayList<SpinnerCountry> countryList = new ArrayList<SpinnerCountry>();
        //add coutries        countryList.add(new SpinnerCountry("1","India"));
        countryList.add(new SpinnerCountry("2","France"));
        countryList.add(new SpinnerCountry("3","United States"));

        //fill data in spinner
        ArrayAdapter<SpinnerCountry> spinnerAdapter = new ArrayAdapter<SpinnerCountry>(MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item,countryList);
        spnCountry.setAdapter(spinnerAdapter);

// And this is how we can retrieve it upon item selection

        spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SpinnerCountry spn = (SpinnerCountry) parent.getItemAtPosition(position);

                Toast.makeText(MainActivity.this,spn.id,Toast.LENGTH_LONG).show();
        
            }

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

            }
        });


    }
}


Video





Thursday 9 May 2019

Android Activity



Calling one activity from another in android

Intent intent = new Intent(context, YourActivityClass.class);
startActivity(intent);
intent = new Intent(getApplicationContext(),CoursesActivity.class);
startActivity(intent);


How to start Activity in adapter?


holder.linearLayout.setOnClickListener(new View.OnClickListener(){
    @Override    public void onClick(View view) {

        Intent intent= new Intent(context, CoursedetailsActivity.class);
        
        context.startActivity(intent);

        
    }
});



How to Pass a Data from One Activity to Another in Android

* Passing data from one activity to other in android

Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);

holder.linearLayout.setOnClickListener(new View.OnClickListener(){
    @Override    public void onClick(View view) {

        Intent intent= new Intent(context, CoursedetailsActivity.class);
        intent.putExtra("courseid", listCourse.getCourseid());
        intent.putExtra("course",listCourse.getCourse());

        context.startActivity(intent);

    }
});


* Retrieving bundle data from android activity

Intent intent = getIntent();
if (null != intent) {
    String courseid= intent.getStringExtra("courseid");
    String course= intent.getStringExtra("course");
    Toast.makeText(this,courseid+"-"+course, Toast.LENGTH_LONG).show();
}



Friday 26 April 2019

Android user registration interface create


Android Layout Design




Layout design script below here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.layouts.UserregistrationActivity">
   
    <LinearLayout

        android:id="@+id/Linearlayout"
        android:layout_centerVertical="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



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

        <EditText
            android:id="@+id/Txtusername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Username"/>

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

        <EditText
            android:id="@+id/Txtpassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Enter Password"/>

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

        <EditText
            android:id="@+id/Txtemail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Email"/>

        <Button
            android:id="@+id/Btnregister"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Register User"/>

    </LinearLayout>


    <TextView
        android:layout_below="@+id/Linearlayout"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:text="Alreay Registerd ? \n Click Here to Login"
        />

</RelativeLayout>






Android - SQLite Database Tutorial




Wednesday 20 February 2019

How To Make Splash Screen in Android Studio



How to make Splash Screen in your android project.



Steps

Create new project to give application name as SplashScreen


Keeping minimum SDK 


Add on Activity and then finish to open new project.


Then add to EmptyActivity activity in your project.

Select EmptyActivity and give name SplashScreen




For display image copy to drawable folder.

Add to image view in activity_splash.xml file

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"    tools:context="com.example.root.splashscreen.SplashActivity">

    <ImageView        
          android:layout_width="match_parent"        
          android:layout_height="match_parent"        
          android:src="@drawable/logo"        />

</RelativeLayout>


Then change theme to select NoTitlebar.


Then go to our main job as three minutes display  splash screen.

Open SplashActivity.java to add this codes

package com.example.root.splashscreen;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        Thread myThread = new Thread(){
            @Override
            public void run() {
                try {
                    sleep(3000);
                    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        myThread.start();


        
    }
}


Then set Splash Screen to start activity.


Go to open manifest file

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.root.splashscreen">

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">

        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>



Code that hides title bar of activity

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title   
  2. getSupportActionBar().hide(); //hide the title bar  

The setFlags() method of Window class is used to display content in full screen mode. You need to pass theWindowManager.LayoutParams.FLAG_FULLSCREEN constant in the setFlags method.







Tuesday 19 June 2018

How to play audio and video files in the background on Android


How to set play mx player in the background  on android mobile



Open your mobile


Just launch MX Player app and then press the menu icon at the top-right side of the screen.


Go to choose "Play".    or  (Go to “Settings” and then choose “Player”.)

Check the box next to “Background Play"  


How to enable background play. 






Thursday 14 December 2017

How to completely uninstall android studio in ubuntu


Open Terminal

Ctrl + Alt + T

sudo su  //login to superuser

Run the following commands in the terminal:
rm -Rf /Applications/Android\ Studio.app  
rm -Rf ~/Library/Preferences/AndroidStudio*  
rm -Rf ~/Library/Preferences/com.google.android.*  
rm -Rf ~/Library/Preferences/com.android.*  
rm -Rf ~/Library/Application\ Support/AndroidStudio*  
rm -Rf ~/Library/Logs/AndroidStudio*  
rm -Rf ~/Library/Caches/AndroidStudio*  
rm -Rf ~/.AndroidStudio*  
rm -Rf ~/.gradle  
rm -Rf ~/.android  
rm -Rf ~/Library/Android*  
rm -Rf /usr/local/var/lib/android-sdk/  
rm -Rf ~/.AndroidStudio*  
To delete all projects:
rm -Rf ~/AndroidStudioProjects  

Uninstall Android Studio