worked fine by adding android:orientation="vertical"
in the xml
How to add imageview programmatically
-
14-07-2023 - |
문제
I am working on programmatically add imageview to layout
image_layout.addView(imSex,imParams);
in this there is a image declared above oncreate
ImageView imSex ;
LinearLayout image_layout;
LinearLayout.LayoutParams imParams;
after oncreate
image_layout=(LinearLayout)findViewById(R.id.image_layout);
imParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
imSex= new ImageView(Detail_mul.this);
imSex.setBackgroundResource(R.drawable.camera);
image_layout.setGravity(Gravity.CENTER);
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/Top_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/title_bar_bg" >
<ImageView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/logo" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_sub_middle"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="#D4D5D5" >
<TextView
android:id="@+id/textView_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Fyll ut skjemaet"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000"
android:gravity="bottom"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/addpic_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Velg hoved bilde" />
<LinearLayout
android:id="@+id/image_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<ImageView
android:id="@+id/im1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/camera" />
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_btn"
android:text="@string/submit"
android:textColor="#FFF"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
Here is the method where I'm adding the imageview
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 300, 200, true);
im1.setImageBitmap(bm);
String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("my_image", f.toString()).commit();
//f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE)
{
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, Detail_mul.this);
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("my_image", tempPath).commit();
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath,btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 300, 200, true);
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
im1.setImageBitmap(bm);
}
}
image_layout.addView(imSex,imParams); //this line works fine
}
adding imageview debugs fine but the visually the imageview is not add on the view.
해결책
제휴하지 않습니다 StackOverflow