2016-06-25 4 views
0

私は、画像プレビューファイルを含むリストビューでアプリケーションを作成しようとしています。リストビュー内の画像の1つがクリックされたときにはがデバイスの壁紙として設定されます。私のリストビューのXMLファイルには、次のようになります。壁紙をListviewで選択した画像に変更します - Android

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TableRow> 

     <ImageView 
      android:id="@+id/img" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:contentDescription="@string/wallpaper_preview" /> 

    </TableRow> 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_margin="10dp" 
     android:gravity="center"/> 
</TableLayout> 

リストビュー用のJavaファイルは次のようになります。

package biz.bigtooth.naturewallpapers; 

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomList extends ArrayAdapter<String>{ 

    private final Activity context; 
    private final String[] web; 
    private final Integer[] imageId; 
    public CustomList(Activity context, 
         String[] web, Integer[] imageId) { 
     super(context, R.layout.list_single, web); 
     this.context = context; 
     this.web = web; 
     this.imageId = imageId; 

    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView= inflater.inflate(R.layout.list_single, null, true); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 

     ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
     txtTitle.setText(web[position]); 

     imageView.setImageResource(imageId[position]); 
     return rowView; 
    } 
} 

そして、私の主な活動のJavaファイルは次のようになります。

package biz.bigtooth.naturewallpapers; 

import android.app.WallpaperManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.InterstitialAd; 
import com.google.android.gms.ads.MobileAds; 

import java.io.IOException; 

public class MainActivity extends AppCompatActivity { 

    InterstitialAd mInterstitialAd; 

    ListView list; 
    String[] web = { 
      "Leaves", 
      "Lilacs", 
      "Dandy Lions", 
      "Twigs", 
      "Holly", 
      "White Flowers", 
      "Cave Pool" 
    } ; 
    Integer[] imageId = { 
      R.drawable.leaves_preview, 
      R.drawable.lilac_preview, 
      R.drawable.dandy_lions_preview, 
      R.drawable.twigs_preview, 
      R.drawable.holly_preview, 
      R.drawable.white_flowers_preview, 
      R.drawable.cave_pool_preview 

    }; 

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

     MobileAds.initialize(getApplicationContext(), "ca-app-pub-5164171001589422/5381023392"); 

     AdView mAdView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 

     mInterstitialAd = new InterstitialAd(this); 
     mInterstitialAd.setAdUnitId("ca-app-pub-5164171001589422/5381023392"); 

     mInterstitialAd.setAdListener(new AdListener() { 
      @Override 
      public void onAdClosed() { 
       requestNewInterstitial(); 

      } 
     }); 

     requestNewInterstitial(); 

     CustomList adapter = new 
       CustomList(MainActivity.this, web, imageId); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            final int position, long id) { 
      } 
      String main = list.getSelectedItem().toString(); 
      //where i need help 
     }); 
    } 

    private void requestNewInterstitial() { 
     AdRequest adRequest = new AdRequest.Builder() 
       .addTestDevice("5C6759521D465119182182A234FF5CE3") 
       .build(); 

     mInterstitialAd.loadAd(adRequest); 
    } 
} 

の場合誰もが正しいトラックで私を得ることができた私は非常に感謝するだろう。

私は特定の壁紙を適用したくないので、クリックした画像を認識してそのアプリケーションを適用したいと考えています。

+0

[プログラムによって設定は、Androidの携帯電話の背景]が重複する可能性( http://stackoverflow.com/questions/20053919/programmatically-set-android-phones-background) –

答えて

0

このコードを使用できます。

:あなたは

myWallpaperManager.setResource(R.drawable.leaves_preview)このコードはpublic void onItemClick(AdapterView<?> parent, View view,final int position, long id)

WallpaperManager myWallpaperManager 
    = WallpaperManager.getInstance(getApplicationContext()); 
    try { 
     myWallpaperManager.setResource(R.drawable.leaves_preview); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

にマニフェストにパーミッションを設定する必要が

使用中の画像IDを使用する必要があります

<uses-permission android:name="android.permission.SET_WALLPAPER"/> 
+0

あなたのコードの唯一の問題は、他の画像に対して調整しないことです。どの画像がクリックされたのかを把握し、壁紙として設定された画像を動的に変更できるようにしたい。 –

0

あなたの許可をに設定する必要がありますファイル。

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

あなたは以下のものの背景を設定することができます。

@RequiresPermission(Manifest.permission.SET_WALLPAPER) 
    private void setWallpaper(int resId) { 
     WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 
     try { 
      myWallpaperManager.setResource(resId); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              @NonNull String[] permissions, 
              @NonNull int[] grantResults) { 
     if (requestCode == PERMISSION_SET_WALLPAPER) { 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       btn.performClick(); 
      } else { 
       Toast.makeText(getApplicationContext(), "Require permission to set Wallpaper. " + 
         "Please Allow.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

以下を追加コードをListView.setOnItemClickListener() into次のように:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            final int position, long id) { 
       if (ContextCompat.checkSelfPermission(MainActivity.this, 
         Manifest.permission.SET_WALLPAPER) != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(MainActivity.this, 
          new String[]{Manifest.permission.SET_WALLPAPER}, 
          PERMISSION_SET_WALLPAPER); 
       } else { 
        setWallpaper(R.drawable.splash_bg); 
       } 
      } 
     }); 
+0

これは、壁紙をユーザーが選択したものに設定しません。これは常に同じものに設定します。 –

関連する問題