Problems with buttons in a Dialog - android

I have a custom Dialog that shows a few buttons. The idea is to have a scrollable Dialog where one can choose a number or letter from 0 to 9, or 0 to F or 0 to Z ...
So my first problem is how do I add these buttons through code and not xml since there is a variable number of buttons each time. Even the simplest code crashes on me so I'm probably not doing anything right.
Also the code I have with a few xml buttons chrashes when I click on the buttons saying it can't find the onClick function. As you can see I have android:onClick="onClickDialogbutton" in my button xml and the function does exist in my java code but still it crashes.
Hope someone can take look at the code and help me with adding buttons programatically and get the onClick to work.
Here is my code:
DialogTestActivity.java:
package com.test.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
public class DialogTestActivity extends Activity {
Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickStartDialog( View view ) {
dialog = new Dialog( this );
dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
dialog.setContentView( R.layout.dialog );
dialog.setCancelable( true );
dialog.show();
// I here wish to add buttons through code and not xml.
// This gives an error as it is now.
Button button = new Button( this );
( ( LinearLayout )findViewById( R.id.Buttons ) ).addView( button );
}
public void onClickDialogButton( View view ) {
dialog.dismiss();
}
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="onClickStartDialog"
android:text="Start Dialog" />
</LinearLayout>
Dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="54dip"
android:layout_height="150dip"
android:gravity="center" >
<ScrollView
android:layout_width="48dip"
android:layout_height="144dip" >
<LinearLayout
android:id="#+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/ButtonId0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="0"
android:textSize="32dip" />
<Button
android:id="#+id/ButtonId1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="1"
android:textSize="32dip" />
<Button
android:id="#+id/ButtonId2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="2"
android:textSize="32dip" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

You probably need to call findViewById on the dialog. Simply calling findViewById inside of your Activity will try to find a view or child view currently being displayed in your activity with the specified id. In this case that is probably returning null, hence the crash. Try it again with dialog.findViewById...

Related

Android TextView doesn't show affected text

Ok so... I've been trying to affect text to TextView (because it's meant to show text, right?)
But for some unknown to me reason it refuses to print whatever I put into it.
My application has two activities, the MainAcivity, or a welcoming screen, per se, and a GameActivity (the project is a Domino game).
I create my previously written java objects at the beginning of the GameActivity class, trying then to show player's name within a TextView.
Player player1 = new Player("Hulk");
creates a player with name Hulk, as you can imagine. I have a method within Player.java to return player's name:
player1.getName()
returns string "Hulk"
I then try to set my TextView's text to hulk, first off by creating a handle to it with:
TextView p1v = (TextView) findViewById(R.textviews.p1view);
then doing the following:
p1v.setText("Player 1 : " + player1.toString());
the p1view is defined as follows within game.xml layout:
<LinearLayout
...
<LinearLayout
...
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I tried p1view.textAppend, tried using android:editable="true". Nothing seems to work.
Funnily enough though, when I try doing the same thing, within MainActivity, it works, as so:
public class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
with the TextView defined within main.xml as follows:
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Following are the full codes of the files, just to avoid answers like: Give us the full code D:
GameActivity.java:
package domino.asd;
import LI260.*; import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class GameActivity extends Activity {
private Player player1 = new Player("HULK");
private Player player2 = new Player("CPU");
private Pioche bag = new Pioche();
private Plateau board = new Plateau();
private Game game1 = new Game(bag, board, player1, player2);
TextView p1name = null;
TextView p2name = null;
/**
* Called when the activity is first created.
*
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
TextView tv = (TextView) findViewById(R.textviews.p1view);
tv.setText(player1.getName());
} }
game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="10"
>
<!-- Played layout -->
<LinearLayout
android:id="#+drawable/gameview_Scores"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="top"
android:layout_weight="2"
android:layout_marginBottom="10px"
android:orientation="horizontal"
android:background="#ff0000"
android:gravity="center"
>
<Button
android:id="#+buttons/exitButton"
android:text="Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="#+buttons/drawButton"
android:text="Draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/scoresButton"
android:text="Show scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Board layout -->
<LinearLayout
android:id="#+textviews/gameview_Table"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="center"
android:layout_weight="6"
android:layout_marginBottom="10px"
android:background="#ffff00"
>
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- At hand layout -->
<LinearLayout
android:id="#+drawable/gameview_Playable"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_gravity="bottom"
android:background="#ff00ff"
>
<TextView
android:id="#+textviews/gameview_AtHandText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package domino.asd;
import LI260.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Player p1 = new Player("Hulk");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
}
public void btnClick_Name(View view) {
if (view.getId() == R.buttons.B_EnterName) {
EditText playersname = (EditText) findViewById(R.string.playerNameInput);
Toast.makeText(this, "Your name: " + playersname.getText().toString(), Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.game);
}
}
and lastly main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<EditText
android:id="#+string/playerNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="1"
/>
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/B_EnterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save name and start game."
android:onClick="btnClick_Name"
/>
</LinearLayout>
When you are first loading the activity, you are calling this code:
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
Yet when the user click's the button you are only calling this code:
setContentView(R.layout.game);
Because you have reset the main content view the child view mainview_player has been destroyed and replaced with a new one when inflated from R.layout.game.
You would need to call this code again to find the new view in the game layout and populate it with your player name:
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
You would be better to look into how activities relate to one another and start a new activity for R.layout.game

RelativeLayout Gravity not applied?

I have an activity with the following layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="#+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnblock" />
<TextView
android:id="#+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/testbtnX"
android:layout_toRightOf="#+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="#string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="#+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="#dimen/padding_medium"
android:src="#drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="#+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="#string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/testlayoutErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="1px"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and the following code :
package minmaxdev.android.picrossanywhere;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Capture our button from layout
ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
// Register the onClick listener with the implementation above
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
rv.setGravity(Gravity.CENTER);
Button btnRetry = new Button(TestActivity.this);
btnRetry.setId(R.id.btnRetry);
btnRetry.setBackgroundResource(R.drawable.btnselector);
RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
prmBtn.setMargins(0, 120, 0, 0);
btnRetry.setLayoutParams(prmBtn);
// btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
btnRetry.setText("Retry");
btnRetry.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
rv.addView(btnRetry);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
}
I would like to know :
Why is my dynamically created Button (named btnRetry) not appearing in the center of the screen, since I set the parent RelativeLayout gravity to center with rv.setGravity(Gravity.CENTER) ?
Thank you very much for your time
The answer is easy. Gravity works for the content, layout_gravity for the view that uses that.
Source basically: https://stackoverflow.com/a/3482757/180538
Try to use LayoutParams with addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
To answer your gravity understanding: You are right but I guess that this information in the documentation is important:
Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.
I can't test your layout at the moment but my guess is that the time where gravity is applied doesn't create the expected result.
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.

Could not click on the buttons in SlidingDrawer

I have a problem with SlidingDrawer. All buttons in it could not be click.
Here is my xml file for SlidingDrawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:content="#+id/content"
android:handle="#+id/handle">
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
>
<Button
android:id="#+id/btn_video"
style="#style/button_menu_bottom"
android:drawableTop="#drawable/mnu_video"
android:text="#string/video" />
...
...
</LinearLayout>
</SlidingDrawer>
Here is my java file extended from SlidingDrawer
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import com.example.R;
public class BottomMenuBar extends SlidingDrawer {
private SlidingDrawer mSlidingDrawer;
private Button mButtonSlidingDrawer;
private LinearLayout mLayoutContent;
private Button mButtonVideo;
private Button mButtonPhoto;
private Button mButtonChat;
private Button mButtonSetting;
public BottomMenuBar2(Context context, AttributeSet attrs) {
super(context, attrs);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.bar_bottom_menu, this, true);
mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu);
mButtonSlidingDrawer=(Button)findViewById(R.id.handle);
mLayoutContent=(LinearLayout)findViewById(R.id.content);
mButtonVideo=(Button)findViewById(R.id.btn_video);
mButtonSetting=(Button)findViewById(R.id.btn_setting);
}
public Button getmButtonSlidingDrawer() {
return mButtonSlidingDrawer;
}
public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) {
this.mButtonSlidingDrawer = mButtonSlidingDrawer;
}
...
Setter & Getter methods
...
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
...
Any content
...
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.BottomMenuBar
android:id="#+id/sld_bottom_menu"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:handle="#+id/handle"
android:content="#+id/content"
/>
</LinearLayout>
</LinearLayout>
For my buttons in sliding drawers, I tell each button what method to use in the xml with android:onClick.
<Button
android:id="#+id/sortbutton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:onClick="SortClickHandler"
android:text="#string/sd_sortmethod_button"
android:textSize="12dp" />
And then define the method in whatever activity is appropriate:
public void SortClickHandler(View v) {
// Do stuff here
}
EDIT
I think I found your issue. From the developer docs here it says, and I quote:
SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer
should only be used inside of a FrameLayout or a RelativeLayout for instance.
So, I think if you change your layout to a RelativeLayout you will find that your buttons will work. I just double-checked all of my uses of a sliding drawer and in all cases the root element of the view containing it is a RelativeLayout.
I don't understand why you're extending SlidingDrawer.
You should look up the documentation here.
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<FrameView
android:id="#id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
[INSERT VIEWS HERE]
<FrameView>
</SlidingDrawer>
In your Activity you don't even technically need to gain access to the SlidingDrawer object (The sliding functionality is already in place as standard, it simply contains the the view's in content, you don't need to interfere).
From your Activity gain reference to the view's you add in [INSERT VIEWS HERE]. You can then do setOnClickListener() on each of these buttons.

Android auto focus change

i want to have a set of buttons and the focus on buttons should change periodically first i want to try it with 2 buttons.this is my main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/number_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.72"
android:focusable="true"
android:gravity="center"
android:text="#string/number"
android:textSize="10pt" android:clickable="true"
android:focusableInTouchMode="true"/>
<Button
android:text="#string/contact"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contact_button"
android:layout_weight="1" android:layout_gravity="center"
android:focusable="true" android:gravity="center"
android:focusableInTouchMode="true"/>
</LinearLayout>
in the source code i have used FOCUS_LEFT,RIGHT and all other options ,but i am getting a run time exception. the orientation is set as vertical in linear layout.Please help me to sort out this problem, if anybody know how to set the timer option as well post it here
here is the code
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HelloandroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Object o=null;
// o.toString();
// TextView tv = new TextView(this);
// tv.setText("Hello Andriod");
setContentView(R.layout.main);
Button mybtn= (Button)findViewById(R.id.number_button);
mybtn.requestFocus();
try
{
Button mybtn2= (Button)mybtn.focusSearch(View.FOCUS_DOWN);
mybtn2.requestFocus();
}
catch(Exception e)
{
Log.e("focus change","focus failed",e);
}
}
}
The problem is that you is finding a control in mybtn. Inside your activity you should call focusSearch in Activity context. Try this:
Button mybtn2 = (Button) focusSearch(View.FOCUS_DOWN);
if (mybtn2 != null)
mybtn2.requestFocus();

Button visibility problems in Android

I'm creating a toolbar that toggles the visibility of buttons when you click a button from a toolbar. So, if the user clicks on the "Draw" button, invisible buttons "Pencil" and Pen" will become visible above the "Draw" button. If you click the "Draw" button again, the buttons "Pencil" and "Pen" will become invisible again.
Within my xml file I have set the visibilty of some buttons to be "invisible" so when I launch the activity they won't be seen. This part is straight forward.
.xml file of btnDrawLine - (Update # 12:21)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<com.odhranlynch.testSection.UserInterface
android:id="#+id/UserInterface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/btnDraw"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Draw" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Line" />
<Button
android:id="#+id/btnDrawCurve"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDrawLine"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Curve" />
<Button
android:id="#+id/btnCutout"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/btnDraw"
android:text="Cutout" />
<Button
android:id="#+id/btnCutInner"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_toRightOf="#+id/btnDraw"
android:visibility="visible"
android:text="Inner" />
<Button
android:id="#+id/btnCutOutter"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnDrawCurve"
android:layout_alignBottom="#+id/btnDrawCurve"
android:layout_toLeftOf="#+id/btnCancel"
android:visibility="visible"
android:text="Outter" />
<Button
android:id="#+id/btnCancel"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btnFinish"
android:text="Cancel" />
<Button
android:id="#+id/btnFinish"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Finish" />
</RelativeLayout>
Next, when the user clicks a button that is visible, I'd like the invisible buttons to appear.
Here's the thing, they won't reappear!lol I'm confused by it.
I would be very grateful if someone would be kind enough to shed so light onto this for me :)
testActivity.java
package com.odhranlynch.testSection;
import com.odhranlynch.testSection.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_product);
// Find buttons and give them a name.
final View btnDraw = findViewById(R.id.btnDraw);
final View btnCutOut = findViewById(R.id.btnCutout);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
final View btnDrawCurve = findViewById(R.id.btnDrawCurve);
final View btnCutInner = findViewById(R.id.btnCutInner);
final View btnCutOutter = findViewById(R.id.btnCutOutter);
//Draw Button clicked (UI Toolbar).
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Treat button as a toggle button
//So if a sub-button (e.g. Draw Line) is visible, then we know the button has
//been toggled to visible so lets now make it invisible.
if (btnDrawLine.getVisibility()== View.VISIBLE) {
//Its visible.
btnDrawLine.setVisibility(View.INVISIBLE);
btnDrawCurve.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
// Either gone or invisible
btnDrawLine.setVisibility(View.VISIBLE);
btnDrawCurve.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
As a further point to mention, if I set the visibilty of the buttons to be visible within the .xml file I can toggle the visibilty perfectly fine during runtime!
Again, I would be grateful of some help :)
Try replacing View.INVISIBLE to View.GONE.
Your code is working fine..
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Line" />
<Button
android:id="#+id/draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="draw" />
</LinearLayout>
The activity
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class DrawCanvasActivity extends Activity {
private static final String Number1 = "9686801147";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
Button btnDraw = (Button) findViewById(R.id.draw);
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (btnDrawLine.getVisibility()== View.VISIBLE) {
btnDrawLine.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
btnDrawLine.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
The button line appears when the draw button is clicked.Guess there may be problem in the view formatting in your code.
If you want ot use gone/visible, just add a LinearLayout around your buttons, which you want to hide. LinearLayout will have a layout_width=wrap_content; and you referer position of other element to this Layout.
After you free to change visibility to gone/visible of your buttons.

Categories

Resources