As the title suggests. Button is clearly present and visible in Android Studio. I run the emulator and it dissapears.
I have done numerous searches but cant seem to find a case-specific solution. Could you please take a look and see if you can spot something I cannot.
activity_main.xml:
<Button
android:id="#+id/searchBtn"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="154dp"
android:layout_marginTop="581dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="87dp"
android:background="#drawable/buttons"
android:elevation="15dp"
android:text="SEARCH"
android:visibility="visible" />
MainActivity.Java:
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.searchBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openListActivity();
}
});
}
public void openListActivity() {
Intent intent = new Intent(this, ListActivity.class);
startActivity(intent);
}
The buttons sole purpose at the moment is to open a second activity.
Is there any other potentially relevant code I have missed?
Your margin is too large for the button to render to an immulator or regular device. As you know, the margin is going to put space between your widget and other widgets on the screen. But spacing of that magnitude will force your image to disappear completely. I would reduce my margin and define a more practical layout as your project grows. That should resolve your issue.
Related
I am developing a small project of test, and I wrote the following code.
I already created in the xml file, a button with id called "registerBtn".
I erased the imports of this source code to shorten space of this source code.
In the java file, I created a variable called mRegisterBtn, in the type of Button.
Inside the method called onCreate(Bundle savedInstanceState) the mRegisterBtn receives the method called findViewById(R.id.registerBtn);
However, in the mRegisterBtn.setOnClickListener, the part of new View.OnClickListener appears in gray color, and it is not working when trying to test this code.
This image shows what I really mean. Please, perceive that the the part of new View.OnClickListener appears in gray color. It means a error. But trying to compile, this code runs, but the button simply does not work.
Can anyone know how to fix this error, please?
public class Register2 extends AppCompatActivity {
Button mRegisterBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
mRegisterBtn = findViewById(R.id.registerBtn);
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_LONG).show();
}
});
}
}```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#007FFF"
tools:context=".Register">
<Button
android:id="#+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="Register"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try using the Activity itself as the Context.
If you want a log, then make one
If you want the gray to go away, use a lambda
Log.d("REGISTER", "Setting listener");
mRegisterBtn.setOnClickListener(view -> {
Log.d("REGISTER", "Clicked!");
Toast.makeText(Register2.this, "Testing", Toast.LENGTH_LONG).show();
});
I want to make a bottom dialog which will display a text I provide and I can hide and show it whenever I want. This dialog will be shown in all activities. For example, if the app is not connected to the server right now I will show this bottom dialog saying "No connection" and this dialog will be displayed in any activity which is on the screen. How to make this dialog, I tried to make it in XML but I needed to write its show/hide methods in every activity which is a tedious work.
Here is an image which shows the bottom dialog which I am trying to make.
Create a custom dialog box that is easy to show on all activities. A dialog box can be customized as the style in your picture.
Here is an example, requestFeature() must be called before adding content, Other settings need to be after setContentView().
public class YOUR_DIALOG extends Dialog {
private String mText;
public YOUR_DIALOG(Context context, String text) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mText = text;
}
#Override
public void onStart() {
super.onStart();
Window dialogWindow = getWindow();
dialogWindow.getAttributes().width = android.widget.ListPopupWindow.MATCH_PARENT;
dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
dialogWindow.setBackgroundDrawable(new ColorDrawable(0xffff7320));
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
// Everything else remains the same, as is the case with the normal dialog box.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_dialog_layout);
textview = findViewById(...);
textview.setText(mText);
}
}
And the layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:textAppearance="#android:style/TextAppearance.Material.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:drawablePadding="10dp"
android:drawableLeft="#mipmap/ic_launcher"
android:textStyle="bold"
android:textColor="#fff"/>
</LinearLayout>
In your activity
new YOUR_DIALOG(this,"Dialog").show();
I'm trying to make an application on Android Studio. I have 2 buttons. a plus and a minus. I need to know how to make the text smaller and bigger everytime I click on one of these buttons.
This is my MainActivity.java:
Button Min = (Button)findViewById(R.id.Min);
Min.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(-5);
}
});
This is my .xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Plus"
android:layout_gravity="top|left"
android:text="+"
android:textSize="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Min"
android:layout_gravity="top|right"
android:text="-"
android:textSize="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp"
android:id="#+id/DeText"
android:text="Text"/>
I also have made a screenshot of the page I'm working on. Hopefully this gives some more information about my end product:
You simply set the text size by getting the current size and adding/subtracting the value you want.
Button minButton = (Button)findViewById(R.id.Min);
minButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(COMPLEX_UNIT_PX, t.getTextSize() - 5f);
}
});
It would be better to check if the current text size is not too small before reducing the size.
You should use sp instead of dp for text sizes. From documentation, you can use setTextSize method and it includes two arguments.
void setTextSize (int unit,
float size)
So, you can specify the text size after button click as:
Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView text = (TextView)findViewById(R.id.DeText);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,text.getTextSize()-1);
}
});
I have two different buttons in my activity, and an if statement to show them...
My if statement is working well and show the right button, but one of those buttons is not doing anything when I click on it! (Actually, it's not clickable at all!)
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("LOG", "Clicked!"); // Even this log won't work!
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutImportant.setVisibility(View.VISIBLE); // This line is working
}
if (!isNeeded)
{
// buttonNormal is working well!
buttonNormal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutNormal.setVisibility(View.VISIBLE); // And this one is not working too!
}
The problem is, it won't return any error or log, to know what's the problem! It's log I disabled the button... (which I didn't)
I checked if it's for declarations, but everything is declared well...
I checked the XML, there's no problem there either:
<LinearLayout
android:id="#+id/linearLayoutImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/textViewImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="#string/important"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/buttonImportant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/details" />
</LinearLayout>
...
...
<LinearLayout
android:id="#+id/linearLayoutNormal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end"
android:background="#android:drawable/ic_menu_close_clear_cancel" />
<TextView
android:id="#+id/textViewNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="#string/normal"
android:textStyle="bold" />
<Button
android:id="#+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:text="#string/details" />
</LinearLayout>
EDIT:
Here's the declarations:
LinearLayout linearLayoutImportant, linearLayoutNormal;
Button buttonImportant, buttonClose, buttonNormal;
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutImportant = (LinearLayout) findViewById(R.id.linearLayoutImportant );
buttonImportant = (Button) findViewById(R.id.buttonImportant);
buttonClose = (Button) findViewById(R.id.buttonClose);
buttonClose .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
}
});
buttonNormal = (Button) findViewById(R.id.buttonNormal);
linearLayoutNormal = (LinearLayout) findViewById(R.id.linearLayoutNormal);
}
EDIT 2:
Alright guys, maybe I didn't made it clear...
That isNeeded is a variable and I control it from another place...
When I make it false, the second if will get executed very well, and when I make it true, the first if will execute too, but only the button is not clickable!
EDIT 3:
I also checked .setEnabled(true); or .setClickable(true);, still not working... :/
SOLVED:
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
Try without the if statement in order to know if is a validation problem and you're only setting one listener
The boolean variable isNeeded can take only one of the two values, either true or false. Therefore only one of the if conditions will be executed.
Since only one of the condition is true, you are only setting an onClickListener to one of the buttons and thus the other button won't be clickable and the log message within the onClick method won't be visible in the logs.
Hope this answered your question.
All the best :)
set isNeeded = true before
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
then test it again.
Good lucky!
There was an error posting my ans so i have taken a screenshot of it. Please review it for your answer
I am just getting into android apps, and I have yet to find a tutorial that explains in detail of how to do anything.Can someone show me the code on how to create two buttons (sign in and sign up) in android ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton=(Button)findViewById(R.id.button);
button.setOnClickListener(LogInListener);
signUpButton=(Button)findViewById(R.id.button2);
button2.setOnClickListener(SignUpListener);
}
private OnClickListener LogInListener=new OnClickListener()
{
public void onClick(View v)
{
}
}
Is this the correct way to implement? thanks
activity_main.xml
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="#+id/button"
android:layout_marginTop="61dp"
android:layout_below="#+id/textView3"
android:layout_toStartOf="#+id/button2"
android:layout_toLeftOf="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign UP"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_alignLeft="#+id/editText2"
android:layout_alignStart="#+id/editText2"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp" />
EDIT:
Now that you have edited your question, you just need to do one more thing to declare your Buttons as instance variables. Declare them outside of all methods (onCreate) but inside the mainActivity.
PRE EDIT:
I'll show you what your main activity (Java class) and what your layout (XML file) should look like:
Main Activity:
public class MainActivity extends AppCompatActivity {
Button signIn, signUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signIn = (Button) findViewById(R.id.'idOfButtonFromXMLLayout');
signUp = (Button) findViewById(R.id.'idOfButtonFromXMLLayout');
//Looking at my XML code, the signIn id would be R.id.signInButton
}
The findViewById method is inherited from the AppCompatActivity class, all activities extend the AppCompatActivity class. Older versions of android just extended the Activity class.
The findViewById method takes an int parameter more specifically an id.
The reason a cast is required is because the findViewById method as you would assume returns a type of View, this is then casted to a button.
XML Layout File:
<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:padding="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/signInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In"
<!-- Complete Layout Details--> />
<Button
android:id="#+id/signUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUpText"
<!-- Complete Layout Details--> />
</RelativeLayout>
In the code above I have represented the text of the buttons in two ways...
1) Hard-coded string "Sign In"
2) String resource "#string/signUpText
It is good practice to change your hard-coded strings to the latter format.
If you're new at Android Development some things are just confusing. I would create buttons by doing this:
Define Button in your XML File.
Add Listener to your Button.
Don't forget to add id attribute to your Button.
i would do it this way.
LAYOUT XML FILE
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.onClickListener {
private Button buttonOne;
private Button buttonTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOne = (Button) findViewById(R.id.buttonOne); // id located in your xml file
buttonOne.setOnClickListener(this);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonTwo.setOnCliclListener(this);
}
private void onClick(View v){
switch(v.getId()) {
case r.id.buttonOne: {
// action when buttonOne is clicked
break;
}
case r.id.buttonTwo: {
// action when buttonTwo is clicked
break;
}
}
}
To create a button, you have to code in your xml file, or drag it in your Design view which will do that for you
Which will auto-magicly do this for you, with auto generated values.
-to name your button edit android: text
-edit android: id to edit the "key" that connects your button design in xml to java
If you want to use your button for things like onclicklisteners and such, you will need to "import" it into your java code, like so. The android: id ="#/<>value"and findViewById(R.id.) should be the same (though its not in the photos)
Now simply do this again for every button you want and change the values to your needs. Hope I helped.