I'm currently working on letting a user tap an image that will then display an AlertDialog with a zoomed in version of that image. I'm not too sure if the AlertDialog is the best way to go about this but it's my solution for now.
I'm finding myself struggling getting the Resource ID for an ImageView before I show the alert dialog. Here is the exact piece of code where the error is occurring (The comments are failed fix attempts):
mPhotoView.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ResourceType")
#Override
public void onClick(View view) {
if(mPhotoView != null){
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
dialog.setView(inflater.inflate(R.layout.zoomed_image, null)).create();
//medImg.setImageResource(dialog.getContext().getResources().getLayout(R.layout.zoomed_image).getIdAttributeResourceValue(R.id.ivZoomedImg));
//ivZoomedImg = getActivity().findViewById(R.id.ivZoomedImg);
//ivZoomedImg = view.findViewById(R.id.ivZoomedImg);
Bitmap bm = PictureUtils.getScaledBitmap(mPhotoFile.getPath(), getActivity());
ivZoomedImg.setImageBitmap(bm); // <- null reference
dialog.show();
}
}
});
Specifically, I can't seem to figure out a way to access the R.id.ivZoomedImg that should have been instantiated through the LayoutInflator? I'm not even sure if that's possible to be honest, though I don't see why it wouldn't. Does anyone know what would be the best way to go around this?
Some things to note:
R.layout.zoomed_image is just an XML layout containing a single ImageView with the id of R.id.ivZoomedImg
mPhotoView is the image being clicked on to have it displayed bigger
The fix involved simply instantiating an independent View where you can pull the ImageView resource Id from!
Lines of coded added:
View v = inflater.inflate(R.layout.zoomed_image, null);
ivZoomedImg = v.findViewById(R.id.ivZoomedImg);
dialog.setView(v).create();
Related
I have an alertDialogue popup when a user wants to create a game, and it asks the user how many points they would like to gamble in the game, but it keeps throwing a null reference error and I am not too sure why.
This is my alertDialogue positive button click listener
alertDialog.setPositiveButton("Confirm Wager", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
createLobbyGame();
double wagerD;
String wager;
TextView wagerRV = findViewById(R.id.wagerRV);
wagerD = Double.parseDouble(edittext.getText().toString());
wager = Double.toString(wagerD);
boolean wage = wager.endsWith("0");
if(wage) {
wagerRV.setText(wager+"0");
} else {
wagerRV.setText(wager);
}
}
});
It throws an error when it tries to setText. This is the error it throws
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I know I had this working in the past, but I must have changed something to make it not work properly anymore but I have no idea what I would have changed.
I know this is a very common and simple problem, but I have looked at many other answers and have not found a solution that works for me.
Any help?
TextView declaration:
TextView wagerRV = (TextView) ((AlertDialog.Builder) alertDialog).findViewById(R.id.wagerRV);
How I am defining alertDialog:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(FlipCoinLobby.this);
final EditText edittext = new EditText(FlipCoinLobby.this);
alertDialog.setView(edittext);
Your wagerRV is null because it can't find R.id.wagerRV.
You need to retrieve views from within onClick() using the dialog reference.
Change
TextView wagerRV = findViewById(R.id.wagerRV);
to
TextView wagerRV = (TextView) ((AlertDialog) alertDialog).findViewById(R.id.wagerRV);
Remove any unnecessary casting (I don't have IDE at the moment).
Update based on comments and question edit:-
alertDialog.setView(edittext) --> your alertDialog does not have any TextView with id R.id.wagerRV. Please check out some examples online on setting the content view with XML and that XML should have that TextView. If your wagerRV is in the activity and not inside the dialog, then declare it at the activity level, not inside onClick of alertDialog.
Update 2
You need to change your builder to the actual AlertDialog using AlertDialog alertDialog = alertDialogBuilder.create();. And then the casting will work too.
I have one filter button.I need to open expandable/dropdown filter same as given below image.I tried popup menu and Listpopup window but,I did not get any success.
Thanks in advance
you can make use of fragment which will be below the filter button,layout will include the ui you expect i.e upper arrow and expandable view or any view you are using..
After clicking on filter just visible the fragment and with fragment transition,for the effect you can use animation too...
This i already done,you can refer carwale app used car section..click on filter button one same dilog will come..
for any thing let me know further..
I did code by the help of above comments. but the code looks as below.
final View popUpView = getLayoutInflater().inflate(R.layout.exp, null);
listPopupWindow = new PopupWindow(popUpView, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
expandableListView= (ExpandableListView) popUpView.findViewById(R.id.expandableListView);
btnticker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(MainActivity.this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
listPopupWindow.showAsDropDown(v);
}
});
I have written an app which has a screen view containing a thumbnail that I want to expand to full screen view (with pan and zoom) when I click it.
The large view with pan an zoom works fine, but I want to return to the original view when I click the large image.
final TouchImageView imgBig = new TouchImageView(Dashboard.this);
final ImageView img = (ImageView) findViewById(R.id.graph);
final Bitmap bitmap = result.getImage();
img.setImageBitmap(bitmap);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgBig.setImageBitmap(bitmap);
imgBig.setMaxZoom(4f);
setContentView(imgBig);
imgBig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// What do I need to do here to return to original thumbnail screen view?
}
});
}
});
Have tried a number of things without success!
Just the original setContentView(R.layout.main) will set it back.
It might be better to have an ImageView in the layout xml and set its image and show and hide it. Otherwise you are stuck with just one view which may be limiting.
When you do a setContentView its initializing your activity with that content. So if you want to just put something on top of it you could just have a hidden view. In your layout xml code make an image view that sits on top of all the other views. Then set its visibility="gone" so its hidden.
Then in your onClick instead of calling setContentView just set the image bitmap like you do and call imgBig.setVisibility(View.Gone or View.Visible) to show or hide your big image.
Another possibility is to have 2 activities. And call startActivity to show your big image and then finish to go back to the other activity like it was.
Another possibility is to use fragments, but that probably more involved.
I know there are lots of other answers on stackoverflow on the same thing but I can't seem to get it to work.
What I'm trying to do is find the ID of a view from an inflated layout. I want WV1 to load google.com when the button is clicked, you can see I'm using onClick from XML to do this.
public void ButtonClicked(View view)
{
View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, null);
WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
else{
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
This is in the MainActivity, the webview (WV1) is in the other, inflated class.
Problem is, nothing happens at all...
I've been stuck on this for quite some time now, I appreciate all help given to me.. If there's any other information you require then just ask, thanks in advance!
--Edit--
In the MainActivity, theres tabhost and a button that creates new tabs. When new tabs are created the MainActivity inflates the second class file containing the webview, I can't get the mainactivity to find the webview from the inflated class. I dont know if this helps any more or not...
Check out this link:
Android - Add textview to layout when button is pressed
Where one of the answers does this:
mLayout.addView(createNewTextView(mEditText.getText().toString()));
where mLayout is a linear layout in the activity.
You'd have to add a view to one of your current layouts or start up an activity that opens up with a web view in it.
Alternative way to create webview :
Webview WV1 = new WebView(view.getContext);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
else{
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
Now add this to a view group
viewgroup.addchild(WV1);
You're not attaching it to anything. You need to either supply the parent when you inflate it, or call addView on the ViewGroup that should contain it.
public void ButtonClicked(View view){
View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, (ViewGroup) findViewById(android.R.id.content));
WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
Or:
((ViewGroup)findViewById(android.R.id.content)).addChild(WV1);
Both of these will add your view at the end. You may need to add some layout attributes to get it to look like you want.
I have an activity that has a TabHost containing a set of TabSpecs each with a listview containing the items to be displayed by the tab. When each TabSpec is created, I set an icon to be displayed in the tab header.
The TabSpecs are created in this way within a setupTabs() method which loops to create the appropriate number of tabs:
TabSpec ts = mTabs.newTabSpec("tab");
ts.setIndicator("TabTitle", iconResource);
ts.setContent(new TabHost.TabContentFactory(
{
public View createTabContent(String tag)
{
...
}
});
mTabs.addTab(ts);
There are a couple of instances where I want to be able to change the icon which is displayed in each tab during the execution of my program. Currently, I am deleting all the tabs, and calling the above code again to re-create them.
mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();
Is there a way to replace the icon that is being displayed without deleting and re-creating all of the tabs?
The short answer is, you're not missing anything. The Android SDK doesn't provide a direct method to change the indicator of a TabHost after it's been created. The TabSpec is only used to build the tab, so changing the TabSpec after the fact will have no effect.
I think there's a workaround, though. Call mTabs.getTabWidget() to get a TabWidget object. This is just a subclass of ViewGroup, so you can call getChildCount() and getChildAt() to access individual tabs within the TabWidget. Each of these tabs is also a View, and in the case of a tab with a graphical indicator and a text label, it's almost certainly some other ViewGroup (maybe a LinearLayout, but it doesn't matter) that contains an ImageView and a TextView. So with a little fiddling with the debugger or Log.i, you should be able to figure out a recipe to get the ImageView and change it directly.
The downside is that if you're not careful, the exact layout of the controls within a tab could change and your app could break. Your initial solution is perhaps more robust, but then again it might lead to other unwanted side effects like flicker or focus problems.
Just to confirm dominics answer, here's his solution in code (that actually works):
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if (TAB_MAP.equals(tabId)) {
ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
} else if (TAB_LIST.equals(tabId)) {
ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
}
}
});
Of course it's not polished at all and using those direct indices in getChildAt() is not nice at all...
See my post with code example regarding Customized Android Tabs.
Thanks
Spct
This is what I did and it works for me. I created this function in the activity that extends from TabBarActivity
public void updateTab(int stringID) {
ViewGroup identifyView = (ViewGroup)getTabWidget().getChildAt(0);
TextView v = (TextView)identifyView.getChildAt(identifyView.getChildCount() - 1);
v.setText(stringID);
}
You can modify this function to change the image instead of text or you can change both, also you can modify this to get any tab child. I was particularly interested in modifying the text of the first tab at runtime.
I called this function from the relevant activity using this call
getParent().updateTab(R.string.tab_bar_analyze);
Try This:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if (TAB_MAP.equals(tabId)) {
ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));
iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));
} else if (TAB_LIST.equals(tabId)) {
ImageView iv = (ImageView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));
iv = (ImageView) tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));
}
}
});