I'm making a quiz game. If the correct button if pressed (4 options) i want to show another picture. First picture is a questionmark, next picture is a picture of a person.
I have these two ImageViews
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:src="#drawable/et"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView2"
android:src="#drawable/questionmark"
android:layout_alignTop="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false" />
Both in my RelativeLayout. ImageView2 is overlapping ImageView.
What is the best way to either switch between the picture or remove ImageView2?
If private button knapEt is pressed, ImageView2 should be removed or switched in layering, so ImageView is shown.
Using a simple logic, at first only one of the view is shown the other is not visible using the android:visibility="gone" property.
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:src="#drawable/et"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:visibility="gone"/>
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView2"
android:src="#drawable/questionmark"
android:layout_alignTop="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false" />
then use the codes to toggle the image. each time it is clicked, the first one becomes invisible and the second one becomes visible and vice versa.
boolean flag = true;
public void toggleImage()
{
if (flag == true)
{
image.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}
else
{
//make the background visible
image.setVisibility(View.VISIBLE);
imageView2.setVisibility(View.GONE);
}
}
Related
I am currently building a popup window which should show 2 rows of labels and buttons allowing the user to view two videos.
I am having difficulties with the layout. Here is the popup layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/layout_background_start">
<TextView
android:id="#+id/spacer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/general_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spacer1"
android:textSize="20dp"
android:textStyle="bold"
android:text="Single-Player Tutorial"/>
<Button
android:id="#+id/general_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/general_video_title"
android:text="See Video"/>
<TextView
android:id="#+id/spacer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#id/general_video_button"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/multiplayer_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/spacer2"
android:text="Multiplayer Tutorial"/>
<Button
android:id="#+id/multiplayer_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"
android:layout_below="#+id/general_video_button"
android:text="See Video"/>
</RelativeLayout>
This is the code displaying the popup in the main activity:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.videos_popup, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(m_PlanesLayout, Gravity.CENTER, 0, 0);
I am using the emulator to test the display of the popup with different phones or tablets. The problem is that on tablets the "See Video" buttons become very big and do not respect the "wrap_content" setting in the layout. It seems that the popup window somehow respects a minimum size relative to the width of the screen and that the button size is adjusted in order to obtain this minimum size.
Can anyone please help ?
Yes, it's not respected wrap_content because it's stretched between TextView and right edge of the screen:
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"
My activity consist of 10 buttons each given a unique background color. When a button is pressed the background of activity should change according to the color of pressed button. And the color of clicked button should change to white and it's text color should change to black. I have accomplished the task till change of background color of activity. I can't figure out how to change button color and its text color using View v. As I don't know which button will be pressed so I can't use the button id directly. Here is the code.
Java File:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_red=(Button)findViewById(R.id.red_btn);
btn_pink=(Button)findViewById(R.id.pink_btn);
btn_blue=(Button)findViewById(R.id.blue_btn);
btn_sblue=(Button)findViewById(R.id.sblue_btn);
btn_orange=(Button)findViewById(R.id.orng_btn);
btn_purple=(Button)findViewById(R.id.purple_btn);
btn_green=(Button)findViewById(R.id.grn_btn);
btn_yellow=(Button)findViewById(R.id.yellow_btn);
btn_brown=(Button)findViewById(R.id.brown_btn);
btn_black=(Button)findViewById(R.id.black_btn);
rl=(RelativeLayout)findViewById(R.id.rel_layout);
btn_red.setOnClickListener(this);
btn_pink.setOnClickListener(this);
btn_blue.setOnClickListener(this);
btn_sblue.setOnClickListener(this);
btn_orange.setOnClickListener(this);
btn_purple.setOnClickListener(this);
btn_green.setOnClickListener(this);
btn_yellow.setOnClickListener(this);
btn_brown.setOnClickListener(this);
btn_black.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Change Background Color
btn_clr=(ColorDrawable)v.getBackground();
rl.setBackground(btn_clr);
// Change Button Color
v.setBackgroundColor(Color.WHITE);
//Change Text Color
//Text Color should change to black
}
}
XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.bsse.asiya.colorchanger.MainActivity"
android:id="#+id/rel_layout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:id="#+id/red_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp"
android:layout_marginTop="40dp"
android:textColor="#color/white"
android:background="#color/red"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pink"
android:id="#+id/pink_btn"
android:layout_alignBottom="#+id/red_btn"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp"
android:textColor="#color/white"
android:background="#color/pink"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"
android:id="#+id/blue_btn"
android:layout_below="#+id/red_btn"
android:layout_alignRight="#+id/red_btn"
android:layout_alignEnd="#+id/red_btn"
android:layout_marginTop="40dp"
android:textColor="#color/white"
android:background="#color/blue"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sky Blue"
android:id="#+id/sblue_btn"
android:layout_above="#+id/orng_btn"
android:layout_alignLeft="#+id/pink_btn"
android:layout_alignStart="#+id/pink_btn"
android:textColor="#color/white"
android:background="#color/sky_blue"
android:layout_alignRight="#+id/pink_btn"
android:layout_alignEnd="#+id/pink_btn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"
android:id="#+id/orng_btn"
android:layout_below="#+id/blue_btn"
android:layout_alignLeft="#+id/blue_btn"
android:layout_alignStart="#+id/blue_btn"
android:layout_marginTop="40dp"
android:textColor="#color/white"
android:background="#color/orange"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Purple"
android:id="#+id/purple_btn"
android:layout_alignBottom="#+id/orng_btn"
android:layout_alignRight="#+id/pink_btn"
android:layout_alignEnd="#+id/pink_btn"
android:textColor="#color/white"
android:background="#color/purple"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
android:id="#+id/grn_btn"
android:layout_below="#+id/orng_btn"
android:layout_alignLeft="#+id/orng_btn"
android:layout_alignStart="#+id/orng_btn"
android:layout_marginTop="41dp"
android:textColor="#color/white"
android:background="#color/green"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow"
android:id="#+id/yellow_btn"
android:layout_alignTop="#+id/grn_btn"
android:layout_alignLeft="#+id/purple_btn"
android:layout_alignStart="#+id/purple_btn"
android:textColor="#color/white"
android:background="#color/yellow"
android:outlineProvider="bounds"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brown"
android:id="#+id/brown_btn"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/grn_btn"
android:layout_alignStart="#+id/grn_btn"
android:layout_marginBottom="41dp"
android:textColor="#color/white"
android:background="#color/brown"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black"
android:id="#+id/black_btn"
android:layout_alignTop="#+id/brown_btn"
android:layout_alignLeft="#+id/yellow_btn"
android:layout_alignStart="#+id/yellow_btn"
android:textColor="#color/white"
android:background="#color/black"/>
</RelativeLayout>
What about this?
#Override
public void onClick(View v) {
...
if (v instanceof Button) {
((Button) v).setBackgroundColor(Color.WHITE);
((Button) v).setTextColor(Color.BLACK);
}
}
If changing the background color, button text color and background is all you need, there is no need to keep so many (10!) references in your code to every button. Simply add onClick attribute to all your buttons in xml android:onClick="changeColors". And declare changeColors method in your activity.
public void changeColors(View view) {
// this method is invoked with the parameter "view" which is the button that was clicked
// change activity background to view(button)'s current color etc.
((Button)view).setBackgroundColor(Color.WHITE);
((Button)view).setTextColor(Color.BLACK);
}
The correct view(button) is passed as the parameter so you don't have to worry about updating the wrong button.
I'm creating a game and in it I use a lot of dialogs. The dialogs make up the main menu in a more simple setup then making up an entire activity for it. These dialogs though, they have a grey outline which is really annoying, and in addition when going from one dialog to another, it shrinks down and blows up as one dissapears and another one reappears.
How can I remove the outline and make the transition more smooth? If it is not possible with dialogs, what can an alternative be? I am using custom layouts connected to Java classes that extend Dialog
EDIT
Java:
public class MenuDialog extends Dialog implements View.OnClickListener {
Context con;
Clicker c;
public MenuDialog(Context c, Clicker game) {
super(c);
this.con = c;
this.c = game;
Window window = this.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
show();
}
Button stat, gem, cash, shop, powerup, settings;
#Override
protected void onCreate(Bundle sis){
super.onCreate(sis);
setContentView(R.layout.menu);
setButtons();
}
private void setButtons(){
stat = (Button) findViewById(R.id.bStats);
gem = (Button) findViewById(R.id.bGems);
gem.setOnClickListener(this);
stat.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bStats:
StatDialog sd = new StatDialog(con,c);
sd.show();
break;
case R.id.bGems:
IAPDialog iapd = new IAPDialog(con, c);
iapd.show();
break;
//other buttons
}
dismiss();
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/relativeLayout"
android:gravity="center_horizontal"
android:background="#drawable/phone_like_bc"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout5"
android:layout_marginTop="51dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bStats"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/bGems"
android:background="#drawable/stat_button"
android:layout_toStartOf="#+id/bGems" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/stats"
android:id="#+id/textView17"
android:layout_below="#+id/bStats"
android:layout_alignLeft="#+id/bStats"
android:layout_alignStart="#+id/bStats" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bShop"
android:background="#drawable/shop"
android:layout_below="#+id/textView17" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/shop"
android:textAlignment="center"
android:id="#+id/textView18"
android:layout_alignTop="#+id/textView16"
android:layout_alignLeft="#+id/bShop"
android:layout_alignStart="#+id/bShop" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout"
android:layout_marginTop="51dp"
>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/gem"
android:id="#+id/bGems"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/linearLayout5"
android:layout_toEndOf="#+id/linearLayout5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/gems"
android:id="#+id/textView15"
android:layout_alignBaseline="#+id/textView20"
android:layout_alignBottom="#+id/textView20"
android:layout_toLeftOf="#+id/bPowerUp"
android:layout_toStartOf="#+id/bPowerUp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bPowerUp"
android:background="#drawable/lightning"
android:layout_alignTop="#+id/linearLayout"
android:layout_toLeftOf="#+id/bSettings"
android:layout_toStartOf="#+id/bSettings" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/powerups"
android:id="#+id/textView16"
android:layout_alignBaseline="#+id/bSettings"
android:layout_alignBottom="#+id/bSettings"
android:layout_toLeftOf="#+id/bSettings"
android:layout_toStartOf="#+id/bSettings" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:gravity="center">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bCash"
android:background="#drawable/cash"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text=" " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/cash"
android:id="#+id/textView19"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/bSettings"
android:background="#drawable/settings"
android:layout_below="#+id/bCash"
android:layout_alignLeft="#+id/bCash"
android:layout_alignStart="#+id/bCash" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/settings"
android:id="#+id/textView20"
android:layout_alignBottom="#+id/bCash"
android:layout_alignLeft="#+id/textView19"
android:layout_alignStart="#+id/textView19" />
</LinearLayout>
</LinearLayout>
There are two more dialog classes both XML and java. I added those two to show that I already know how to add the classes, but I need to know how to remove the outline of the dialog and I need to know how I can smoothen the transition between two dialogs. They have the same background too.
You can use DialogFragments instead of Dialogs, they works as Fragments and you can customize them. Read more about the use of Dialog fragments at https://developer.android.com/reference/android/app/DialogFragment.html
You have a lot of Tutorials to create your own DialogFragments in Google if you don't like the Android Documentation.
Currently I have a UI designed with imageviews, the problem is they cannot overlap or be layered. (Or if they can, I haven't figure out how)
I'd like to do 3 layers, the bottom layer is the board,
The next layer is pieces,
The next layer is a selected piece with valid moves.
My idea is that I will have listeners on the top layer only.
I am just looking for the best data structure to use for this, the app is a chesslike game I am working on developing.
If there is another approach that might be better I am all ears as well.
Thanks!
As requested code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView22"
android:src="#drawable/ni_whitesquare"
android:layout_below="#+id/textView5"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView32"
android:src="#drawable/ni_blacksquare"
android:layout_alignTop="#+id/imageView22"
android:layout_alignLeft="#+id/imageView22"
android:layout_alignStart="#+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView22"
android:layout_toLeftOf="#+id/imageView22"
android:layout_toStartOf="#+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView21"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="#+id/imageView22"
android:layout_toRightOf="#+id/imageView12"
android:layout_toEndOf="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView23"
android:src="#drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="#+id/imageView12"
android:layout_toLeftOf="#+id/imageView33"
android:layout_toStartOf="#+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView31"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="#+id/imageView21"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView21"
android:layout_toLeftOf="#+id/imageView21"
android:layout_toStartOf="#+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView13"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView12"
android:layout_alignStart="#+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView33"
android:src="#drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="#+id/imageView23"
android:layout_alignLeft="#+id/imageView32"
android:layout_alignStart="#+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/directions"
android:id="#+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView21"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Startingpoint"
android:src="#drawable/ni_pawn"
android:layout_above="#+id/imageView11"
android:layout_toLeftOf="#+id/imageView12"
android:layout_toStartOf="#+id/imageView12" />
</RelativeLayout>
Activity :
public class GameActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}
For layering views, you might want to use a FrameLayout see this answer: Layout Layers? Z-Axis?
Also, if you're trying to set up a layout in which you have several consecutive views that need to be lined up one next to the other, the most appropriate element is usually a LinearLayout. LinearLayout will automatically set up your views in consecutive order in the order you declared them, according to the android:orientation attribute you set for it.
Overall, I'd suggest looking at the docs for each of these. RelativeLayout is probably not the way to go since it involves a lot of managing of view positioning which just seems frivolous for what I think you are trying to do. I hope this helps
I have a FrameLayout with multiple ImageViews as shown in the image.
I have registered listeners to all these Imageviews.
I have positioned the ImageViews using android:layout_gravity, android:paddingLeft and android:paddingRight due to which the images are overlapping.
My Activity class is as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ivbglogin = (ImageView) findViewById(R.id.bglogin);
ivlogin = (ImageView) findViewById(R.id.login);
tvlogin = (TextView) findViewById(R.id.tvlogin);
ivbgsignup = (ImageView) findViewById(R.id.bgsignup);
ivsignup = (ImageView) findViewById(R.id.signup);
tvsignup = (TextView) findViewById(R.id.tvsignup);
ivbglogin.setOnClickListener(this);
ivlogin.setOnClickListener(this);
tvlogin.setOnClickListener(this);
ivbgsignup.setOnClickListener(this);
ivsignup.setOnClickListener(this);
tvsignup.setOnClickListener(this);
}
and
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bglogin:
Toast.makeText(AstralPadActivity.this, "bglogin", Toast.LENGTH_SHORT).show();
break;
case R.id.login:
Toast.makeText(AstralPadActivity.this, "login", Toast.LENGTH_SHORT).show();
break;
case R.id.tvlogin:
Toast.makeText(AstralPadActivity.this, "tvlogin", Toast.LENGTH_SHORT).show();
openLoginDialog();
break;
case R.id.bgsignup:
Toast.makeText(AstralPadActivity.this, "bgsignup", Toast.LENGTH_SHORT).show();
break;
case R.id.signup:
Toast.makeText(AstralPadActivity.this, "signup", Toast.LENGTH_SHORT).show();
break;
case R.id.tvsignup:
Toast.makeText(AstralPadActivity.this, "tvsignup", Toast.LENGTH_SHORT).show();
break;
}
}
The Framwlayout is
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="40"
android:background="#drawable/start_bottombg" >
<ImageView
android:id="#+id/bglogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="194dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:src="#drawable/start_loginbg" />
<ImageView
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="15dp"
android:paddingRight="200dp"
android:paddingTop="15dp"
android:src="#drawable/icon_login" />
<TextView
android:id="#+id/tvlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="120dp"
android:text="Log In"
android:textSize="15dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/bgor"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:src="#drawable/start_or" />
<ImageView
android:id="#+id/bgsignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="190dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:src="#drawable/start_signupbg" />
<ImageView
android:id="#+id/signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="15dp"
android:paddingLeft="100dp"
android:paddingTop="15dp"
android:src="#drawable/icon_signup" />
<TextView
android:id="#+id/tvsignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="200dp"
android:text="Sign Up"
android:textSize="15dp"
android:textStyle="bold" />
</FrameLayout>
So when I click on the brown ImageView, it is calling the green one.
I want to know how to register listeners to each one of these elements and invoke those specific listeners on click of respective widgets please.
Thanks in advance.
Haven't run into this before, but it sounds like you're encountering (for lack of a better term) undefined behaviour. Is using a RelativeLayout an option? If not, then adjusting dimensions to prevent the overlap? Barring that--try assigning separate anonymous listeners instead of one that inspects the widget's ID.
And remember to, every step of the way, crawl through in a debugger/trace file to verify that what you think is happening, is really happening.
The issue was because I was using android:paddingLeft="200dp" instead of android:layout_marginLeft="100dp". Because of the padding the images were overlapping. Now it works fine.