質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

772閲覧

Android studioにてボタンを画像表示させる方法

2r4h8k3m

総合スコア22

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2022/07/31 22:27

前提

AndroidstudioでExcel出力、画像選択をする際のボタンを画像で表示させたいのですが、それがうまく行きません。良い方法あれば教えて頂けたらと思います。

#試したこと
Buttun属性をImageButtonに書き換えたり、srcをbackground に書き換えたり、ImageViewで表示させようとしたりもしてみたのですが、こちらも他のソースとの関係上変えるのが難しくどうしたらいいか分かりませんでした。

https://github.com/araara3842/TextDetection27.git

該当のソースコード

activity_main

1 2<?xml version="1.0" encoding="UTF-8"?> 3 4-<RelativeLayout tools:context=".MainActivity" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> 5 6<!--image view to display our image--> 7 8 9 10-<ScrollView android:layout_height="match_parent" android:layout_width="match_parent" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="8dp" android:layout_marginStart="8dp" android:layout_above="@+id/linear_holder"> 11 12<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="22sp" android:textColor="@color/black" android:text="Hello, Capture or Choose an Image to Extract Text." android:layout_below="@+id/image" android:id="@+id/text_data"/> 13 14</ScrollView> 15 16 17-<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linear_holder" android:layout_alignParentBottom="true"> 18 19<!--button to capture our image--> 20 21 22<Button android:layout_height="wrap_content" android:layout_width="0pt" android:id="@+id/button_capture" android:src="@drawable/capture" android:layout_margin="8dp" android:layout_weight="1"/> 23 24<Button android:layout_height="wrap_content" android:layout_width="0pt" android:id="@+id/button_excel" android:src="@drawable/excel2" android:layout_margin="8dp" android:layout_weight="1" android:onClick="onExcelClick"/> 25 26<!--button to detect text from our image--> 27 28 29<Button android:layout_height="wrap_content" android:layout_width="0dp" android:text="Copy Text" android:id="@+id/button_copy" android:layout_margin="8dp" android:layout_weight="1" android:visibility="gone"/> 30 31</LinearLayout> 32 33</RelativeLayout> 34

MainActivity

1 2package com.example.textdetection; 3 4import androidx.annotation.Nullable; 5import androidx.appcompat.app.AppCompatActivity; 6import androidx.core.app.ActivityCompat; 7import androidx.core.content.ContextCompat; 8 9import android.Manifest; 10import android.content.ClipData; 11import android.content.ClipboardManager; 12import android.content.Context; 13import android.content.Intent; 14import android.content.SharedPreferences; 15import android.content.pm.PackageManager; 16import android.graphics.Bitmap; 17import android.net.Uri; 18import android.os.Bundle; 19import android.provider.MediaStore; 20import android.service.voice.VoiceInteractionSession; 21import android.util.SparseArray; 22import android.view.View; 23import android.widget.Button; 24import android.widget.Space; 25import android.widget.TextView; 26import android.widget.Toast; 27 28import com.google.android.gms.vision.Frame; 29import com.google.android.gms.vision.text.TextBlock; 30import com.google.android.gms.vision.text.TextRecognizer; 31import com.theartofdev.edmodo.cropper.CropImage; 32import com.theartofdev.edmodo.cropper.CropImageView; 33 34import java.io.IOException; 35 36 37 38 39 40 41 42import androidx.appcompat.app.AppCompatActivity; 43import android.os.Bundle; 44import android.view.View; 45import android.widget.Button; 46import android.widget.Toast; 47import org.apache.poi.hssf.usermodel.HSSFCellStyle; 48import org.apache.poi.hssf.usermodel.HSSFWorkbook; 49import org.apache.poi.hssf.util.HSSFColor; 50import org.apache.poi.ss.usermodel.Cell; 51import org.apache.poi.ss.usermodel.CellStyle; 52import org.apache.poi.ss.usermodel.Row; 53import org.apache.poi.ss.usermodel.Sheet; 54import org.apache.poi.ss.usermodel.Workbook; 55import java.io.File; 56import java.io.FileNotFoundException; 57import java.io.FileOutputStream; 58import java.io.IOException; 59 60 61 62 63 64 65 66 67public class MainActivity extends AppCompatActivity { 68 Button button_capture,button_copy,button_excel; 69 TextView textview_data; 70 Bitmap bitmap; 71 private static final int REQUEST_CAMERA_CODE = 100; 72 int count; 73 private SharedPreferences preference; 74 private SharedPreferences.Editor editor; 75 76 77 78 @Override 79 protected void onCreate(Bundle savedInstanceState) { 80 super.onCreate(savedInstanceState); 81 setContentView(R.layout.activity_main); 82 button_capture = findViewById(R.id.button_capture); 83 button_copy = findViewById(R.id.button_copy); 84 button_excel = findViewById(R.id.button_excel); 85 textview_data = findViewById(R.id.text_data); 86 87 88 //プリファレンスの準備 89 preference = getSharedPreferences("Preference Name", MODE_PRIVATE); 90 editor = preference.edit(); 91 92 if (preference.getBoolean("Launched", false)==false) { 93 //初回起動時の処理 94 count=1; 95 96 //プリファレンスの書き変え 97 editor.putBoolean("Launched", true); 98 editor.commit(); 99 } 100 101 102 103 104 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 105 ActivityCompat.requestPermissions(MainActivity.this, new String[]{ 106 Manifest.permission.CAMERA 107 }, REQUEST_CAMERA_CODE); 108 } 109 button_capture.setOnClickListener(new View.OnClickListener() { 110 @Override 111 public void onClick(View view) { 112 CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).start(MainActivity.this); 113 } 114 }); 115 button_copy.setOnClickListener(new View.OnClickListener() { 116 @Override 117 public void onClick(View view) { 118 String scanned_text= textview_data.getText().toString(); 119 copyToClipBoard(scanned_text); 120 } 121 }); 122 } 123 124 125 @Override 126 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 127 super.onActivityResult(requestCode, resultCode, data); 128 if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { 129 CropImage.ActivityResult result = CropImage.getActivityResult(data); 130 if (resultCode == RESULT_OK) { 131 Uri resultUri = result.getUri(); 132 try { 133 bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),resultUri); 134 getTextFromImage(bitmap); 135 } 136 catch (IOException e) 137 138 { 139 e.printStackTrace(); 140 } 141 } 142 } 143 } 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158public void onExcelClick(View v) { 159 File file; 160 161 String scanned_text= textview_data.getText().toString(); 162 163 Workbook wb = new HSSFWorkbook(); 164 Cell cell = null; 165 CellStyle cellStyle = wb.createCellStyle(); 166 // cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); 167 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 168 169 170 //Now we are creating sheet 171 Sheet sheet = null; 172 sheet = wb.createSheet("Name of sheet"); 173 //Now column and row 174 175 Row row = sheet.createRow(0); 176 cell = row.createCell(0); 177 cell.setCellValue(scanned_text); 178 cell.setCellStyle(cellStyle); 179 180 sheet.setColumnWidth(0, (10 * 200)); 181 182 SharedPreferences data = getSharedPreferences("Data", MODE_PRIVATE); 183 SharedPreferences.Editor editor = data.edit(); 184 185 186 187if(count==1){ 188 189 190 file = new File(getExternalFilesDir(null), "plik1.xls"); 191 192 count++; 193 editor.putInt("DataInt", count); 194 editor.commit(); 195 196}else{ 197 count = data.getInt("DataInt", count); 198 199 200 file = new File(getExternalFilesDir(null), "plik"+count+".xls"); 201 count++; 202 editor.putInt("DataInt", count); 203 editor.commit(); 204 205 206} 207 208 209 210 211 212 213 214 215 216 217 FileOutputStream outputStream = null; 218 219 220 try { 221 outputStream = new FileOutputStream(file); 222 wb.write(outputStream); 223 Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show(); 224 } catch (java.io.IOException e) { 225 226 e.printStackTrace(); 227 228 Toast.makeText(getApplicationContext(), "NO OK", Toast.LENGTH_LONG).show(); 229 try { 230 outputStream.close(); 231 } catch (IOException ex) { 232 ex.printStackTrace(); 233 } 234 } 235} 236 237 238 239 240 241 private void getTextFromImage(Bitmap bitmap) 242 { 243 TextRecognizer recognizer = new TextRecognizer.Builder(this).build(); 244 if (!recognizer.isOperational()) 245 { 246 Toast.makeText(MainActivity.this,"Error Occurred!!",Toast.LENGTH_SHORT).show(); 247 } 248 else 249 { 250 Frame frame = new Frame.Builder().setBitmap(bitmap).build(); 251 SparseArray<TextBlock> textBlockSparseArray= recognizer.detect(frame); 252 StringBuilder stringBuilder = new StringBuilder(); 253 for (int i=0; i<textBlockSparseArray.size();i++) { 254 TextBlock textBlock = textBlockSparseArray.valueAt(i); 255 stringBuilder.append(textBlock.getValue()); 256 stringBuilder.append("\n"); 257 } 258 textview_data.setText(stringBuilder.toString()); 259 button_capture.setText("Retake"); 260 button_copy.setVisibility(View.VISIBLE); 261 } 262 } 263 private void copyToClipBoard(String text) 264 { 265 ClipboardManager clipBoard= (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 266 ClipData clip = ClipData.newPlainText("Copied data",text); 267 clipBoard.setPrimaryClip(clip); 268 Toast.makeText(MainActivity.this,"Copied to Clipboard!",Toast.LENGTH_SHORT).show(); 269 } 270} 271

color.xml

1 2<?xml version="1.0" encoding="UTF-8"?> 3 4-<resources> 5 6<color name="purple_200">#B7B7BBBB</color> 7 8<color name="purple_500">#B7B7BBBB</color> 9 10<color name="purple_700">#B7B7BBBB</color> 11 12<color name="teal_200">#B7B7BBBB</color> 13 14<color name="teal_700">#FF000000</color> 15 16<color name="black">#FF000000</color> 17 18<color name="white">#FFFFFFFF</color> 19 20</resources> 21

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2022/08/01 00:04 編集

>Android studioにて AndroidStudio は開発ツールですが、その AndroidStudio にボタンを表示したいのではなく、AndroidStudio を使って作っているアプリの話と思います。 > うまく行きません どのボタンが、どうしたいのが、どうなるのでしょうか。 > 他のソースとの関係上変えるのが難しく どのソースとのどのような関係で難しいのでしょう。
dodox86

2022/08/01 01:42

Excelの操作とButtonへの画像表示は本来は別のトピックの気がしますがそんなことは無いのでしょうか。切り離して考えられませんか。
2r4h8k3m

2022/08/01 23:41 編集

ジンベさん ご質問ありがとうございます、ButtonをImageButtonに変えてしまうと、MainActivityファイルでButtonとして実装していたものを自分の方で変更するのが難しいという意味でそのように書きました。画像選択のボタンの方でsetOnClickListener関数を使っていて、その辺りの変換がよく分からなかったです。日本語が不親切ですみません。
2r4h8k3m

2022/08/01 23:49

dodox86さん ボタンが2つあるアプリでExcel出力のボタンにはおそらくButton属性での定義は関係ないのですが、画像選択のためのsetOnClickListener関数がImageButtonに変更すると難しいという意味で書きました。言葉足らずですみません。
dodox86

2022/08/02 01:10

画像ファイルは正しく選択できているのですか? で、選択後にその画像をボタン上に表示させたいということなのでしょうか。
jimbe

2022/08/02 12:05

> Buttonとして実装していたものを自分の方で変更するのが難しい Button を ImageButton と書き換えるだけですけど…。もちろん setText が出来なくなったりはしますが、最初から「難しい」と言ってしまっては進歩できません。 Button を ImageButton に変えたくらいではスマホが壊れたりはしないのですから、ドキュメントをしっかり読んでどんどんチャレンジしたほうが勉強になると思います。
2r4h8k3m

2022/08/04 11:24 編集

返信大変遅くなってしまいすみません。 dodoxさん 変える中で画像は表示されるがcolorで指定したグレーに塗りつぶされてしまったり、textが表示されなかったりなどしました。こちらのファイルは確かtextが表示されないものだったと思います。 jimbeさん ベストアンサーもありがとうございました、、アイコン指定も助かりました。あとはMainActivityでbuttonのIDを用いて使っている関数をどう変更するかですね...確かに自分の考察も足りないのですが、ImageButton自体が特殊な属性で変更になるので、ネットでsetOnClickListenerの代わりを見つけるのが難しいと思いました。
guest

回答1

0

ベストアンサー

とりあえず… xml の Button でアイコンの指定は drawableLeft 等になります。 (src で指定するのは ImageButton です。)
android:drawableLeft="@drawable/capture" とかですね。
変に試行錯誤するのではなくドキュメントを確認してください。

投稿2022/08/02 12:01

jimbe

総合スコア12646

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問