Open top menu


Example: How to implement AdapterViewFlipper In android.And why we are used?


Simple ViewAnimator that will Aniamte between two or moreViews View that have been added to it.Only one Child is shown at a time.


activity_main.xml
....................................................................... 

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"    
tools:context="com.project.example.androidbeginnerpoint.MainActivity"



  
 >      
android:layout_width="match_parent"       
android:layout_height="match_parent"       
android:id="@+id/simpleAdapterViewFlipper" 

 </LinearLayout>
............................................ 


row_item.xml

............................................ 

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:background="#fff"    
android:gravity="center"    
android:orientation="vertical"

<ImageView        

android:id="@+id/fruitImage"        
android:layout_width="wrap_content"        
android:layout_height="150dp"       
 android:layout_gravity="center" />

 <TextView        
android:id="@+id/fruitName"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_gravity="center"        
android:layout_marginTop="2dp"        
android:textColor="#000" />

</LinearLayout>
.................................... 




MainActivity.Java

..................................... 

package com.project.example.androidbeginnerpoint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterViewFlipper;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private AdapterViewFlipper simpleAdapterViewFlipper;
    int[] fruitImages = {R.drawable.a, R.drawable.ga, R.drawable.s};     
    String fruitNames[] = {"Apple", "Green Apple", "Litchi"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleAdapterViewFlipper = (AdapterViewFlipper) 
findViewById(R.id.simpleAdapterViewFlipper);  
                 AdapterViewFlip customAdapter = new AdapterViewFlip
(getApplicationContext(), fruitNames, fruitImages);
        simpleAdapterViewFlipper.setAdapter(customAdapter);  
                simpleAdapterViewFlipper.setFlipInterval(3000);
        
        simpleAdapterViewFlipper.setAutoStart(true);
    }
}
............................................... 



AdapterViewFlip.Java
................................. 


package com.project.example.androidbeginnerpoint;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class AdapterViewFlip extends BaseAdapter {
    Context context;
    int[] fruitImages;
    String[] fruitNames;
    LayoutInflater inflter;

    public AdapterViewFlip(Context applicationContext, String[] fruitNames,
 int[] fruitImages) {
        this.context = applicationContext;
        this.fruitImages = fruitImages;
        this.fruitNames = fruitNames;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override    public int getCount() {
        return fruitNames.length;
    }

    @Override    public Object getItem(int position) {
        return null;
    }

    @Override    public long getItemId(int position) {
        return 0;
    }

    @Override    public View getView(int position, View view, ViewGroup parent) {
        view = inflter.inflate(R.layout.row_item, null);
        TextView fruitName = (TextView) view.findViewById(R.id.fruitName);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruitImage);
        fruitName.setText(fruitNames[position]);
        fruitImage.setImageResource(fruitImages[position]);
        return view;
    }
}

.................................... 






Read more






Exaplain:- How to prevent Activity going to sleep mode Android ?


Stop the activity in pause Mode .



@Overridepublic void onResume() {
    super.onResume();
    getWindow().addFlags(WindowManager.LayoutParams.
               FLAG_KEEP_SCREEN_ON);
}

@Overridepublic void onPause() {
    super.onPause();
    getWindow().clearFlags(WindowManager.LayoutParams.
              FLAG_KEEP_SCREEN_ON);
}





Read more








Example:-Prevent the android device Screen going to sleep (foreground) ?

First you can given Manifest Permission

<uses-permission android:name="android.permission.WAKE_LOCK" />


DeviceStayWake .java


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;

public class DeviceStayWake extends Activity {

    protected PowerManager.WakeLock mWakeLock;

    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");
        this.mWakeLock.acquire();
    }

    @Override    public void onDestroy() {
        this.mWakeLock.release();
        super.onDestroy();
    }
}






Read more


Explain- How to used Decimal Integer Value in android?

The NumberFormatException occurred when we are  add Decimal value in EditText and Decimal type String Value Convert  in integer.


java.lang.NumberFormatException: Invalid int: "171."

String d=9.90;
int value=new Double(d).intValue();


try....

String amount=171.234;

txt_toatalcost.setText(amount);

amountadult= new Double(txt_toatalcost.getText().toString()).intValue();


//Avoid this...specialy when you used decimal type value.
// amountadult= Integer.parseInt(textTotalAmount.getText().toString());


Thank you for watching... 
Read more


Example:-

Restrict edittext single line, means edittext have a single line.The User Press " enter key" the cursor should not get down second line.




android:maxLines="1"
android:singleLine="true"



<EditText    
android:id="@+id/openorder_searchViewtext"    
android:layout_width="match_parent"    
android:layout_height="48dp"    
android:layout_weight="1"   
android:background="@drawable/edit_text_gradient"    
android:drawableEnd="@drawable/search_icon"   
android:drawablePadding="10dp"    
android:drawableRight="@drawable/search_icon"    
android:ellipsize="end"    
android:hint="@string/Search"    
android:maxLines="1"    
android:paddingLeft="15dp"    
android:scrollHorizontally="true" />
Read more