Pages

Showing posts with label Scroll View. Show all posts
Showing posts with label Scroll View. Show all posts

Tuesday, September 13, 2016

Android Example-Set the Wallpaper of Your Device using Bitmap Class


activity_main.xml
<?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="in.kantapp.example110.MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:paddingTop="10dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/aa" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SetWallpaper" />

    <HorizontalScrollView
        android:layout_width="match_parent"

        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@drawable/a" />

            <ImageView
                android:id="@+id/img2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/b" />

            <ImageView
                android:id="@+id/img3"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"

                android:scaleType="fitXY"
                android:src="@drawable/c" />

            <ImageView
                android:id="@+id/img4"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/d" />

            <ImageView
                android:id="@+id/img5"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                
                android:src="@drawable/e" />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

MainActivity.java
package in.kantapp.example110;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
    ImageView display;
    int tophone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // to our activity to cover the whole screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        Button but = (Button) findViewById(R.id.button);
        tophone = R.drawable.aa;
        but.setOnClickListener(this);
        display = (ImageView) findViewById(R.id.img);
        ImageView image1 = (ImageView) findViewById(R.id.img1);
        ImageView image2 = (ImageView) findViewById(R.id.img2);
        ImageView image3 = (ImageView) findViewById(R.id.img3);
        ImageView image4 = (ImageView) findViewById(R.id.img4);
        ImageView image5 = (ImageView) findViewById(R.id.img5);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast var;
        switch (v.getId()) {
            case R.id.img1:
                display.setImageResource(R.drawable.aa);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.aa;
                break;
            case R.id.img2:
                display.setImageResource(R.drawable.bb);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.bb;
                break;
            case R.id.img3:
                display.setImageResource(R.drawable.cc);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.cc;
                break;
            case R.id.img4:
                display.setImageResource(R.drawable.dd);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.dd;
                break;
            case R.id.img5:
                display.setImageResource(R.drawable.ee);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.ee;
                break;
            case R.id.button:
                // to set a background we need to use bitmap
                InputStream is = getResources().openRawResource(tophone);
                // to phone is a variable that is updated everytime we click on an
                // ImageView to that imageview resource and by clicking the button
                // we set the phone background to that image.
                Bitmap bm = BitmapFactory.decodeStream(is);
                // decode inputstream is
                try {
                    getApplicationContext().setWallpaper(bm);
                    // to set the wallpaper of the phone background we need to ask
                    // permission from the user so add permission of background from
                    // manifest file

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                var = Toast.makeText(MainActivity.this, "Wallpaper image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                break;
        }
    }

}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example110">
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>













Thursday, September 8, 2016

Android Example -Simple Scroll View with Button and TextView

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="in.kantapp.example8.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/clear" />
        <TextView
            android:layout_width="match_parent"
            android:text="@string/scroll"
            android:textSize="20dp"
            android:textColor="#000000"
            android:layout_height="wrap_content"
            android:id="@+id/txt" />
    </LinearLayout>
</ScrollView>

MainActivity.java
package in.kantapp.example8;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button= (Button) findViewById(R.id.clear);
        final TextView textView= (TextView) findViewById(R.id.txt);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                textView.setText("");
            }
        });
    }
}

string.xml
<resources>
    <string name="app_name">"Example 8 "</string>
    <string name="scroll">Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.</string>
</resources>