I am trying to develop my first game but i am having trouble, it is something really simple which i am confused about. Basically I need to load 2 images i created in photoshop but if they are clicked, a different image of the same shape will be drawn on top, making it look like its been selected, then if someone clicks on the other image, image one goes back to the initial state and another image for the second one is drawn on top... How can I achieve this?
I am just having trouble on how to load the initial images to the screen and thenbe able to swap them with the others...
Thank you
If I understand your question right, it sounds like using tabs would be a solution.
Smth like this might be a first step to your game
public class MyTab extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The acWednesdaytivity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, YourWithImage1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab1").setIndicator("tab1",
res.getDrawable(R.drawable.ic_tab))
.setContent(intent);
tabHost.addTab(spec);
}}
and your ic_tab will look like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey img-->
<item android:drawable="#drawable/ic_tab_grey"
android:state_selected="true" />
<!-- When not selected, use white img-->
<item android:drawable="#drawable/ic_tab_white" />
</selector>
I am new myself to andorid. Hope this will help somehow.
I'm new to Android too, so not sure if I have the correct answer. But here is what I might try:
Have your activity implement onclicklistener
In the onCreate method, find your ImageView using findViewById
call the ImageView's setOnclickListener method
Handle the onclick event
In the event handler, call the setImageResource of the ImageView to switch the image.
Again, I'm new to Android too so I'm not talking from experience. I hope this helps.
See the following link which I happened to come across:
http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/
Related
I got this image in a tab but it's too small. how can I scale it up or make it fill the tab
final TabHost tabHost = getTabHost(); //edit: added this line
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Discussion",
getResources().getDrawable(R.drawable.forum))
.setContent(new Intent(this, MyActivity.class)));
Set a LinearLayout as a child of the TabHost and add your image to that LinearLayout
You won't be able to with the method you're using. The drawable is mean't to be a small icon. If you want something custom, you'll have to use setIndicator(View) instead. I'd image you'd create an ImageView set to use the drawable you want and pass that in. If you wanted the text "Discussion" to still be there, you'd had to create a layout with an ImageView and TextView and use that.
This is how I want the tabs to look:
http://img14.imageshack.us/img14/5696/tabort.png
This is how they look using tabHost:
http://img684.imageshack.us/img684/1030/tabort2.png
So I want to remove the border around the images. Instead, I want to have the greyish background image behind the tabs. Can anyone please help me with this (I'm new to Android)?
Here is some relevant code:
// Create an Intent to launch an Activity for the tab
intent = new Intent().setClass(this, WashActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("washer").setIndicator("",
res.getDrawable(R.drawable.wash_tab))
.setContent(intent);
tabHost.addTab(spec);
You can use Buttons positioned side-by-side in Relative Layout with custom background images instead of TabView.
set custom view ( imageview or image with text in ur case ) using setView() instead setIndicator() will work for you .
TabHost is deprecated now . so better to use fragment with compatibility package .
Anyone know how facebook did this:
From what I know we cannot change the height of tabhost. I'm guessing that they laid the "Frank Cho" view over the tabhost to give it the appearance of being shorter but I may be wrong. Anyone know what's going on?
You actually can have custom looking tab widgets. You need to set the tab indicator to some custom layout (with your drawables) and you should be good to go.
Here's an semi-example:
final TabHost host = getTabHost();
final TextView indicator = (TextView) getLayoutInflater().inflate(
R.layout.tab_indicator,
getTabWidget(), false);
indicator.setText("Tab title");
host.addTab(host.newTabSpec("The tab tag")
.setIndicator(indicator)
.setContent([put your content here]));
}
Where the tab_indicator layout can look like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab_label"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="38dp"
android:background="#drawable/minitab" />
The minitab drawable is a drawable item selector (so you need to have an image for selected, default, pressed, and unselected). The facebook app used some white drawable image for the default tab and the blue gradient drawable for the unselected tabs.
Check out the Google IO Schedule app for a full working example: http://code.google.com/p/iosched/ (and specifically the TrackDetailActivity.java)
Somebody else had what I think is the correct answer, but deleted it for some reason...
The height in question is not that of the TabHost, but of the TabWidget.
Try using the version of setIndicator() that takes a View instead of just a String or String plus drawable resource. While I have not played with this yet, my understanding is that it solves this problem nicely.
Be careful, though, that you don't wind up with tabs that are too tough to tap.
I am trying to change the content of the tab header but am struggling. I can see that it is possible to pass a View to the setIndicator() method. I tried this and got a removeParent() warning which I dont understand. Has anyone managed to do this successfully. If so can you explain how you did it please?
Clive
This problem occurs when you use the same view in different tabs. You must use a different view. This is what I did:
view.setImageResource(R.drawable.videotab);
view1.setImageResource(R.drawable.musictab);
intent = new Intent().setClass(this, CustomList.class);
spec = tabHost.newTabSpec("Video").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CustomListOne.class);
spec = tabHost.newTabSpec("Music").setIndicator(view1)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
You don't have to use different Views. You can simply inflate the same layout again when you need it. That means you inflate your layout, set everything you need on your different Views and then you set it as your indicator. If you need another tab just do it again.
I have a tabhost,
e.g:
final TabHost tabs = getTabHost();
tabs.setup();
TabHost.TabSpec spec = null;
spec = tabs.newTabSpec("search");
spec.setContent(new Intent(this, Search.class));
spec.setIndicator("search");
tabs.addTab(spec);
in this tabhost is a Intent,and in the activity must change to other activity,
question is I hope the other actitivy at same tabhost switch?
Can do this?
I had this problem almost a 3 months ago. you can not replace the activity. Because activities are open in new screens. so you have to use the views and layout to achieve what you are want to do. use the setVisiblity and isShown methods you can hide and release yours view. Hope it make some sense on it.