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
Related
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);
}
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);
}
});
}
}
I want to create an activity such as mentioned in photo...
as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, and again when I press the restore button I want it to become in a first state: to be able to see part 1 and part 2 ...
I think if we put two layouts it is possible? Isn't it? Please refer me to a resource to help me about this, or show me the code to achieve a solution.
Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.
Ok, here I go. Below you have a complete example of how to hide/show grouped views.
main.xml
<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" >
<LinearLayout
android:id="#+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
I hope this helps ;)
There is a bit simplified solution, than Diego Palomar produced, without using additional variable. I'll take his code to show:
public class MyActivity extends Activity implements OnClickListener {
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (mViewGroup.getVisibility() == View.VISIBLE) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
}
I am trying to open a new activity. When I do I get a null pointer exception. Everything works untill the following code gets executed.
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
Here is a better look at the code.
public class DisplayTests extends ListActivity {
private DbManagement mdbManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
private OnClickListener AnsRmTest = new OnClickListener(){
#Override
public void onClick(View v) {
try{
System.out.println("fart");
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
I am not sure why the activity would bomb out when it reaches the creation of a button.
Here is the XML as well
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="194dp" >
</ListView>
</FrameLayout>
<Button
android:id="#+id/btnNewExam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/strNewExam" />
<Button
android:id="#+id/btnRmExams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnRmTests"
android:layout_alignBottom="#+id/btnRmTests"
android:layout_alignParentRight="true"
android:text="#string/strRmExams" />
<Button
android:id="#+id/btnRmTests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnNewExam"
android:layout_alignBottom="#+id/btnNewExam"
android:layout_toLeftOf="#+id/btnRmExams"
android:text="#string/strRmTest" />
</RelativeLayout>
So with that being said I am the following code
setContentView(R.layout.moretime);
When I did that I get the following error:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
I do have a list view as you can see about but it is called listView1 and I am not sure why I am getting this error message. Thanks in advance.
You need to set the content of your layout to the activity first then initialize views.
privae Button RmTests;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // missing
RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
... // rest of the code
}
You can findViewById of the current view hierarchy set to the activity. Since you have not set the content to the activity, when you initialize your button you get NullPointerException.
ALso
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
In your xml replace
<ListView
android:id="#+id/listView1"
By
<ListView android:id="#android:id/list"
Also do you require a FrameLayout? Why not have just the listview and the buttons in the relative layout.
http://developer.android.com/reference/android/app/ListActivity.html
Edit:
From the comment below
You can do as below
public class MainActivity extends ListActivity {
String s[] = {"A","B","C","D","E"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, s);
setListAdapter(adapter);
}
}
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate().
If you need to have other views then you can define them in xml extend ListActivity in which case you need to have listview with id "#android:id/list". Like i can have a list and a button at the button. So i need to have a listview with id as "#android:id/list".
Where do you tell your activity which xml it is supposed to load?
Try:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
replace "R.layout.main" with your xml file title. E.g. if it's Main_Layout.xml, replace it with "R.layout.Main_Layout"
Write
setContentView(R.layout.your_layout);
after
super.onCreate(savedInstanceState);
try be adding setContentView(R.layout.your_layout_name); after super.onCreate(savedInstanceState);
You have to set the setContentView .Here is the Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
}
I think in you Activity inside onCreate methode you forget to give setContentView
Before initializing the UI components you must set view for activity inside onCreate methode
ie setContentView(R.layout.yourxml)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxml);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
Additional Info
also since your are using ListActivty you must provide listview id as
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="194dp" />
Otherwise it will cause another error
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.