How to show another button by clicking a button? - android

I don't want to create a new activity. Just like this gentleman's example (http://www.youtube.com/watch?v=V6AdmCIe4Ik),but I want to implement this on LinearLayout using Button instead of main.
Say on the video at 00:44 user clicks a button specified on res/layout/activity1.xml and sub button shows up at 00:47.
He implemented it using menu and creating a sub folder (menu) under res instead of using layout.
What I would like to do is that once user clicks a button declared on LinearLayout it will show another button just like 00:47 on the video.

With a very simple XML file:
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:animateLayoutChanges="true">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2"
android:visibility="gone" />
</LinearLayout>
Then in code, you set the listener for button1 as follows:
private Button button1;
private Button button2;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity1);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button2.setVisibility(View.VISIBLE);
}
});
}
This code sample sets up the event listener for the first button and, when clicked, changes the visibility of the button from gone (which means it takes up no space in the layout and is invisible) to Visible, which is the normal state.

Related

Alert Dialog embedded buttton is not clicking in Android Java

Am trying to click on a button i just added to an AlertDialog but its not responding. The button is on a layout file in the resources folder and then i added this layout to the AlertDialog via the Builder method setView()
I accessed the same Button via my mainactivity after inflating a random view with the layout and set an OnClickListener but its still not working
Here is detail code of what i have tried so far
Layout containing the button
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:background="#drawable/round2"
android:textSize="20sp"
android:textAlignment="center"
android:padding="5dp"
android:text="Submit"
android:textAllCaps="false"
android:drawablePadding="10sp"
android:id="#+id/submit"
/>
</Linearlayout>
Then in my MainActivity.class, i access the button this way
class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//inflate layout containing button
datView=getLayoutInflater().inflate(R.layout.data_entry,null);
//access button
submit=datView.findViewById(R.id.submit);
//set event listener
submit.setOnClickListener(this);
}
//the interface
#Override
public void onClick(View v) {
check();
}
//custom method check
private void check(){
Toast.makeText(this,"Registering data into database",Toast.LENGTH_LONG).show();
}
}
Despite all that the toast does not show on clicking the button, please help
Please try using android:onClick = "check" in the button in your xml, and also create a method in your MainActivity called public void check(View view){...} with the toast inside of it

Android my App crash after set button disabled

My app crashes after I pressed a button.
My code:
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_print_trans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:onClick="OnClickPrintSimpleApiTest"
android:text="PRINT"
android:textColor="#FFFFFF" />
and:
public void OnClickPrintSimpleApiTest(View view) {
final Button BTN_print = (Button) findViewById(R.id.btn_print_trans);
BTN_print.setBackgroundColor(Color.GREEN);
BTN_print.setEnabled(false);
}
Because the OP defined the onClick method OnClickPrintSimpleApiTest as an attribute in their xml layout file as:
android:onClick="OnClickPrintSimpleApiTest"
They do not need to get a reference to the Button using findViewById().
The Button view is passed to the OnClickPrintSimpleApiTest() method as the parameter "view". Therefore, simply do this:
public void OnClickPrintSimpleApiTest(View view) {
Button BTN_print = (Button) view
BTN_print.setBackgroundColor(Color.GREEN);
BTN_print.setEnabled(false);
}

How to disable area of Gridview before pressed a Button?

I'm trying to make a simple game that select a value from item of Gridview. But i want that value just can clickable after press Play Button.We can't click in anywhere if did not press Play Button. How can i do that?
What you can do is to change the clickable property of your grid view
and then in the button click listener just set the clickable property to true
so your code should look like the following:
let's assume that this is your view:
<?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:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="50dp" />
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" />
</LinearLayout>
your activity should contain the following code:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private GridView mGridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.grid);
mButton = (Button) findViewById(R.id.button);
// if you want to do it with setEnable
mGridView.setEnabled(false);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGridView.setClickable(true);
// if you want to toggle then you can use the following code:
// mGridView.setClickable(!mGridView.isClickable());
// you can also change the setEnable property and not the clickable
mGridView.setEnabled(true);
}
});
}
}

How to disable other button until a button is clicked

I am new in android programming. I have made an app which is fill in the blanks type app. Until the answer-confirm button is clicked, the next and the previous Button should be disabled. If it is clicked and answer is checked, then next and previous Button to get enabled. please help!!!!!!!!
If you want to disable a button in xml use this code
<Button
android:text="Next"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
for enabling the button when we click on previous then in onClick function(previous) add this code
next.setVisibility(View.VISIBLE);
next is the button
something like that (maybe its not the best way, you can play with it and make it better)
protected void onCreate(Bundle savedInstanceState) {
...
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enableNextButton();
}
});
...
}
private void enableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_active);
nextButton.setClickable(true);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goNext();
}
});
}
private void disableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_inactive);
nextButton.setClickable(false);
}
and in your xml the buttons should be something like
<Button
android:id="#+id/first_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_active"
android:layout_margin="#dimen/generic_margin"
android:text="#string/first_button_text"
android:textColor="#color/white"/>
<Button
android:id="#+id/next_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_inactive"
android:clickable="false"
android:layout_margin="#dimen/generic_margin"
android:text="#string/next_button_text"
android:textColor="#color/white"/>
this way you start with an active button and an inactive button, when the first is pressed you can "activate" the next button
Please use punctuations and uppercases but anyway.
You can check here for more infos about buttons : http://developer.android.com/reference/android/widget/Button.html
Otherwise, the method setVisible() allow you to make visibile or not a button of your layout. Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
EDIT
Oh sorry ! So you can use setEnabled().

Display an edit text box on click of a button

I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?
If you just want to "display an edit text box on click of a button" why don't you just..
Keep the EditText in your XML layout file for that activity below the Button where you want it..
XML set it's
android:visibility = "gone"
and making instance of that
EditText et=(EditText)findViewById(R.id.thatEditText);
in activity...in your button click event set
et.setVisibility(View.VISIBLE);
Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).
//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:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<EditText
android:id="#+id/edtbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Activity
//oncreate
editText.setVisibility(View.GONE);
btn..setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setVisibility(View.VISIBLE);
}
});
If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.
e.g.
<RelativeLayout android:id="#+id/templayout">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
and you want to add edit text control after text View then on button click :
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.title);
EditText yourEditText = new EditText(this);
relativeLayout.addView(yourEditText, params);
Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.
YourEditText.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
YourEditText.setVisibility(View.VISIBLE);
}
});

Categories

Resources