My app has 5 "tabs" (image views) in a row, I've made onClick-methods for all of them since they are changing the URL to my web view when you click them. (just explaining so you get the idea of the app) I want to change background on the "tab"(image view) I'm on (lets say tab1), and when you click another tab (e.g. tab4), the background on tab1 disappears and appears on tab4.
Code:
public void onWelcome(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL1");
ImageView imgV = (ImageView) findViewById(R.id.tab1);
imgV.setImageDrawable(Drawable drawable); /*i do not understand this line, someone said this is how it's done*/
}
public void onProgram(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL2");
}
public void onNews(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL3");
}
public void onContact(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL4");
}
public void onPictures(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL5");
}
As I said I do not understand how to use this method .setImageDrawable(Drawable drawable);
I have a image called img_selected
in drawable, where I would in the XML-file write "android:background="@drawable/img_selected"
How do I do the same thing In the onclick-method? and how do I get rid of it when I tap on another tab?
The convenience of these methods are that the view clicked on are already passed through. so anything you need done to the view can me done right through the parameter. (here it is View V
)
public void onWelcome(View V)
{
WebView htmlContent = (WebView) findViewById(R.id.webViewer);
htmlContent.loadUrl("URL1");
V.setImageResource(R.drawable.img_selected);
}
It's simple. much like findViewById
, the method would set the image Resource of the view to whatever id
is passed in. to refer to that specific drawable
in code, you can use the above.