What I'm trying to do is when an Imageview item being preesed, then a Dialog will open up and show the inage in a large scale.
Now first of all this is the XML layout of the dialog -
<?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" >
<ImageView
android:id="#+id/imageView_large_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bkgnd_pressed" />
</LinearLayout>
Now the code to the Dialog is as follows -
Dialog settingsDialog = new Dialog(MainChat.this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_large
, null));
ImageView ivLarge = (ImageView) findViewById(R.id.imageView_large_pic);
String pathpic = v.getTag().toString();
ivLarge.setImageURI(Uri.parse(pathpic));
settingsDialog.show();
Now I tried to dubuging this code, and it get stuck at this line - ivLarge.setImageURI(Uri.parse(pathpic));
The logcat gives me that this is java.lang.NullPointerException error.
Thanks for any kind of help
You are looking in the wrong place for the ImageView
ImageView ivLarge = (ImageView) findViewById(R.id.imageView_large_pic);
is looking in the layout you inflate in setContentView(). Instead, you need to look in the Dialog's layout. Try instead
ImageView ivLarge = (ImageView) settingsDialog .findViewById(R.id.imageView_large_pic);
Related
I am trying to show icons in BottomSheeet but only text shows. I read lot of threads on SO but none of them seem to help or are old.
Seeking help on latest android version. Appreciate any help.
List<MenuItem> bottomSheetMenuItems = new ArrayList<>(optionsList.size());
Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_email,
getContext().getTheme());
MenuItem bottomSheetMenuItem = new BottomSheetMenuItem(
getContext(),
someId,
"Test",
drawable);
bottomSheetMenuItem.setChecked(true).setChecked(true);
bottomSheetMenuItems.add(bottomSheetMenuItem);
BottomSheet bottomSheet = new BottomSheet
.Builder(getContext())
.setTitle("Test Title")
.setMenuItems(bottomSheetMenuItems)
.create();
bottomSheet.show();
For icons, you would have to create an xml for bottomsheet.
XML example
<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:id="#+id/intervalBottomSheet"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical"
android:background="?attr/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:src="#drawable/ic_remove_red_eye_black_24dp" />
<TextView
android:text="MenuItem" />
Java code
final BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(getActivity());
View sheetView = getActivity().getLayoutInflater().inflate(R.layout.bottomsheet_interval, null);
mBottomSheetDialog.setContentView(sheetView);
mBottomSheetDialog.show();
If your menu items are dynamic and not constant, you could use a recyclerview in the bottomsheetdialog.
With that in mind, you'll need to do this to load the icons
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
I trying to add image into LinearLayout (linearForImage) dynamically, but it doesn't work.
val image = ImageView(activity)
image.setLayoutParams(LinearLayout.LayoutParams(80, 60))
for (i in 1..5){
linearForImage.removeView(image)
image.setImageResource(R.drawable.ic_add)
linearForImage.addView(image)
}
I expect I will get 4 or 5 images in the LinearLayout, but there are no image is set.
You can modify the layout , as follows ...
Example : ( In Java )
LinearLayout linearForImage = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=1;i<6;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
linearForImage.addView(image);
}
set in view of image
ivImage.setImageResource(your drawable image)
Please try below code, it is working for me.
for (i in 1..5){
val imageView = ImageView(this)
imageView.layoutParams = LinearLayout.LayoutParams(80, 60) // value is in pixels
imageView.setImageResource(R.mipmap.ic_launcher)
linearForImage.addView(imageView)
}
Here is the xml file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearForImage"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</RelativeLayout>
set In Image view
imageView.setImageResource(R.drawable.image)
I'm trying to load an existing layout from XML and then create dynamically a button and a Circle, the reason which I cannot include them onto the XML is because the purpose is to create Circles dynamically, actually the following code is a snippet of what I want to create.
I know the way I am doing this (adding the layout) is wrong, but after reading a lot of Internet content I failed to did it by myself, because that I request help.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_etest);
LayoutInflater inflater;
inflater = this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)
inflater.inflate(R.layout.activity_etest ,
null);
LinearLayout viewGroup = layout;
Button b1 = new Button(this);
b1.setText("test");
viewGroup.addView(b1);
viewGroup.addView(new Circle(this));
}
}
And my class Circle, which extends from View and its method onDraw() consists of:
... onDraw(){
canvas.drawCircle(x, y, radius, paint);
}
The parameters of drawCircle are not rellevant to this question. I have defined them elsewhere.
I also add the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button12"
android:layout_gravity="center_horizontal" />
</LinearLayout>
create one view group like linearlayout in your xml and using its instance add your dynamic views in that.
activity_etest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/viewg" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
activity
setContentView(R.layout.activity_etest);
Linearlayout viewgroup = (LinearLayout)findViewById(R.id.viewg);
Button b1 = new Button(this);
b1.setText("test");
viewGroup.addView(b1);
viewGroup.addView(new Circle(this));
I want to create some sort of logging procedure in my Android app.
I have managed to detect where user is pressing on the screen (three times) and to create logging sequence from that.
On the end, I want to show 3 images which represent selected logging sequence (from my drawable folder)
I have dynamically set drawable ID and it works when I place image on main layout.
But if I place image inside custom dialog, I get force close.
From LogCat I see following: java.lang.NullPointerException at image1.setImageResource(iIdSlike);
If I show drawable ID as a text it's OK and ID is the same on main and custom dialog layout.
I get force close even if I set image like this (not dynamically):
image1.setImageResource(R.drawable.s11);
Why I can't show image on custom dialog?
This is my dijalog.xml (custom dialog layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgDrugi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:layout_marginRight="27dp"
android:layout_marginTop="51dp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="#+id/TextView01" />
</RelativeLayout>
And this is my code:
public class MyWorkLogiranje extends Activity implements OnClickListener
{
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
.
.
.
//calculations to get string sPrvi
sPrvi="s31"; //For testing purposes sPrvi set manually
//Image and text on main layout (it works)
iIdSlike = getResources().getIdentifier(sPrvi, "drawable", getPackageName());
ImageView image = (ImageView) findViewById(R.id.imgPrvi);
image.setImageResource(iIdSlike);
TextView text1 = (TextView)findViewById(R.id.txtPrvi);
text1.setText("iIdSlike: " + iIdSlike);
//Prikaz dijaloga
final Dialog dialog = new Dialog(MyWorkLogiranje.this);
dialog.setContentView(R.layout.dijalog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
dialog.getContext();
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("iIdSlike: " + iIdSlike);
ImageView image1 = (ImageView) findViewById(R.id.imgDrugi);
image1.setImageResource(R.drawable.iIdSlike);
//image1.setImageResource(R.drawable.s11);
//Podešavanje dugmeta
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
Can anyone help me with this please?
I'm scratching my head for two days about this.
Thank you.
Your findViewById is failing. So you are getting NullPointerException.
ImageView image1 = (ImageView) findViewById(R.id.imgDrugi);
should be
ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi);
Try this:
image1.setImageDrawable(getResources().getDrawable(iIdSlike));
Before this add this in your dialog code:
ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi);
You can use this way:
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
or
setImageDrawable(getResources().getDrawable(R.drawable.icon));
or
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
imageview.setImageResource(id);
I have a thumbnail image and when user clicks on that image,
it will popup (a window or dialog??) to show the big image.
The background should be transparent for the (window or dialog??).
Is there any tutorial??
yes you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(backgroundsActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
where preview_image is xml contains only ImageView and close button.
Here the xml used for showing the image full screen
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
use a dialog with background color transparent, no-title theme and custom layout having just one ImageView