I write gridView, its working well. Now i want to open each of single image from gridView and zoom/fullscrened it. what i have to use in onClick method?
Related
Hi,
in my simple app i have an activity with some textviews and a single imageview.
I'm trying to understand how to dynamically change the content of the imageview in my linear layout. I have a linearlayout and i only need to change the image in it (based on the logic of the underlying application) and similarly the text of the textviews. The pictures are .png files that i have put in res/drawable.
It seems to me that the only way is to use an adapter (simpleadapter) but i struggle to find an example of source code as all the ones i found so far refer to more complex adapters (e.g. simplecursoradapter). Anyway i'm very new to these concepts (adapters, helpers, etc.).
Do i really need to define and use AdapterView (or a more specific subclass?) or if if i keep the image files in res/drawable there is a more straightforward way to dynamically change the ImageView?
Thanks in advance,
x70
ImageView iv = (ImageView)findViewById(R.id.myimageview);
iv.setImageResource(resId);
where resId = R.drawable.mypng
With the textviews is similar, just call setText.
Thanks a lot janfsd. That was really easy...
I did actually try before in that way but i thought i needed also to explicitly set the view (eg via setContentView) and it didnt work; i didn't realise that there is no need for that.
still its not clear to me when do i need to use setContentView, but i made good progress!
Cheers,
x70
setContentView is normally used to set the layout(View) of the activity. I have only used it once per activity. Unless you want to change all the Layout of the activity then don't use it more than once, only on the onCreate method after the super call.
So you should only need to use setSomething method which each view should have it.
whenever I use
Code:
tv.setText("Game Over!"); setContentView(tv);
my program crashes (note: tv is an object of the TextView class)
Before this call is made the program is set to a default view that contains a text view, buttons, etc. But the crash only happens when I call the method to change the view. Any ideas?
You probably wouldnt wanna do setContentView on a textview, unless its the only thing you want on the screen. Are you creating the textview dynamically? (In the java code) Or is this a textview you create in the xml. If you are doing it dynamically, are you giving it LayoutParams first?
Yes its dynamic. What kind of LayoutParams do you mean?
Sent from my GT-I9000M using XDA App
If you just create a new textview...
TextView text = new TextView(context);
its just floating around not attached to anything. Just create a quick layout with a textview in it, and do
setContentView(R.layout.layout2);
or you could try
tv.setLayoutParams(new LayoutParams(LayoutParam.WRAP_CONTENT,LayoutParam.WRAP_CONTENT));
It still crashes, no matter what I do as soon as I try to do anything with the TextView tv, it crashes
setting it works: final TextView tv = (TextView) findViewById(R.id.stats);
But doing ANYTHING with tv crashes.
Examples:
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams. WRAP_CONTENT));
tv.setText("testing");
Anything I'm overlooking?
Well for starters you cant change anything about a variable that you declare as final..
Could someone help me add a m player and a setonclicklistener in a fragment. I have a couple of sound bite apps and I am trying to put them together in a Viewpager. I have the layout all set up, but im not sure on how to have the m player to the fragment. Thanks.
Sent from my SCH-I535 using Tapatalk 2
Set an onClickListener by using the setOnClickListener() method in the onCreateView() method.
If you set the onClick attribute in the xml, it will look for the method in your Activity.
The MediaPlayer requires a Context, right? Pass your Activity which you can get by calling getActivity().
u can use fragmentpagerapteradapter
to assign fragment as viewpager child...
My app uses the ActionBarCompat and NavigationDrawer, which begs the question of how is one supposed to structure the navigation? I see two options:
1) Use a single Activity and swap out Fragments.
2) Use multiple Activities, and transition between them.
The latter seems to be the correct way, however this means that each Activity must get and build the ActionBar and NavigationDrawer when called from another Activity.
How will this affect my transition animations? If I have a left-to-right transition effect when a navigation item is clicked, will it transition the entire Activity or everything below the ActionBar?
The former seems fraught with issues. A single .java class file could get really heavy and difficult to manage.
But while we're on the subject of Fragments, please consider this situation and school me on proper Fragment management:
A blank Activity with just a LinearLayout attaches a new Fragment to it that contains a Button. Inside the Fragment class, this button gets an onClickListener and when the button is clicked, another of the same kind of Fragment should be attached to the LinearLayout inside the Activity.
A) Should the Fragment class communicate to the Activity that a new Fragment should be attached? This could be done with a custom public interface and the callback being executed within the onClickListener in the Fragment class.
B) Or, should the Fragment handle attaching the new instance inside the onClickListener, making the Activity work less? By simply bridging the clicked Button and calling .getParent() until the LinearLayout is reached, then attaching it there.
The correct way is the former. You should use fragments with the navigation drawer. If you use activities you are going to setup the navigation drawer every new activity. You should open other fragments from the activity and not from the fragment itself. I believe their is a tutorial on the android developer site that explains this in depth. A quick Google search for "android fragment to fragment communication" or something like that should get you to it. I'll try and find a link later when I am on my computer if you don't find anything
Sent from my Nexus 7 using XDA Premium HD app
There are two ways to define Button onclick, one of them is to create instance for BUTTON which defined in XML, then use setOnClickListener() to define private method to deal with What BUTTON was pressed. Another is declare OnClickListener method in XML.
I personal consider it is much easier way to declare onClickLstener method in XML. But I read first way in most of Android coding books.
what is the difference between them?
I thank you in advanced.
sincerely yours Tom