Button not responding to onClick method - java

I am developing an Android app and I want to inflate a dialog with the press of a button (I've already done that), but one specific button does not work, and I don't really know why.
My code:
public class SearchUser extends AppCompatActivity{
private static final String TAG = "SearchUser";
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.btnSearchUser);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(SearchUser.this);
View mView = getLayoutInflater().inflate(R.layout.searchpopup, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
});
}
});
dialog.show();
}
});
}
LAYOUT XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btnUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/user"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="0.0"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="#+id/mapView"
mapbox:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/btnSearchUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/search_user"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="0.0"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="#+id/mapView"
mapbox:layout_constraintVertical_bias="0.129" />
<Button
android:id="#+id/logoutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/log_out"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="1.0"
mapbox:layout_constraintStart_toStartOf="#+id/mapView"
mapbox:layout_constraintTop_toTopOf="parent"
mapbox:layout_constraintVertical_bias="0.0" />
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="parent"
mapbox:mapbox_cameraTargetLat="-34.6131500"
mapbox:mapbox_cameraTargetLng="-58.3772300"
mapbox:mapbox_cameraZoom="12"
mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10">
</com.mapbox.mapboxsdk.maps.MapView>
<Button
android:id="#+id/startButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:enabled="true"
android:text="#string/crear_fiesta"
android:textColor="#color/mapboxGrayLight"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent" />
I have tried putting a toast message inside the onClick method but nothing happens, so I guess it just does not enter there.
Anyone know why? Thanks for any answer :D

You're overriding the wrong onCreate(), replace it with:
protected void onCreate(#Nullable Bundle savedInstanceState)
Edit: are you sure that this xml should be inflated in class SearchUser? Do you have a class MainActivity like the xml states?

Place the btnSearchUser button below the MapView in XML.
You've also configured MapView with match_parent, which is not applicable to a constraint layout parent. Set the height and width of MapView to 0dp as well.

Related

Can't dismiss a popup window (other solutions do not work)

I am trying to create a simple PopupWindow with a text entry field.
I know there are many questions of this type, but none seem to solve my problem. This is the XML of my popup layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="#color/white"
android:gravity="center"
android:padding="15dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj produkt po identyfikatorze"
android:textSize="15sp"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:textSize="20sp"
android:hint="Identyfikator"
android:minWidth="250dp"
android:id="#+id/insert_edit"
android:gravity="center"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_ok"
android:text="OK"/>
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_cancel"
android:text="Anuluj"/>
</LinearLayout>
</LinearLayout>
Now, I want to create this layout in my main activity when a FAB is pressed. This is how I do it:
public class ScrollingActivity extends AppCompatActivity {
private ActivityScrollingBinding binding;
private int ACTIVITY_LOAD_DATABASE = 1;
AbstractDatabase database;
RowAdapter rowAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = Room.databaseBuilder(getApplicationContext(), AbstractDatabase.class, "magazyn_db")
.allowMainThreadQueries().build();
binding = ActivityScrollingBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
FloatingActionButton fab = binding.addCode;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.println(Log.INFO, "VIEWs", "Showing popup");
PopupWindow codePopup;
View popupView = getLayoutInflater().inflate(R.layout.insert_code, binding.getRoot());
codePopup = new PopupWindow(popupView);
codePopup.setFocusable(true);
codePopup.setOutsideTouchable(true);
View cancelButton = codePopup.getContentView().findViewById(R.id.insert_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
codePopup.dismiss();
}
});
View acceptButton = codePopup.getContentView().findViewById(R.id.insert_ok);
acceptButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View editView = codePopup.getContentView().findViewById(R.id.insert_edit);
String id = ((EditText)editView).getText().toString().trim();
insertNewElement(id);
codePopup.dismiss();
}
});
}
});
[...]
For an unknown reason my popup doesn't go dismissed, both when I click outside of it, or press the cancel button (when I tried to print some logs from the handler, they worked). I have tried many solutions, also inflating it with no root, but then 'showAtPosition' doesn't even show it, and no solutions here on stackoverflow seem to help me even though there are many of them.
Is anyone able to point the problem here?
public void dismiss () Disposes of the popup window. This method can be invoked only after showAsDropDown(android.view.View) has been executed. Failing that, calling this method will have no effect.

EditText doesn't update its text

EDIT2:
Using
fragTransaction.add(fragment, tag);
instead of
fragTransaction.add(id, fragment);
OR
Removing all Fragments code in MainActivity makes the app works again, for some odd reason.
Getting the text via .getText().toString() returns null/empty when button is pressed.
Tried to manually .setText("something"), it works when .getText().toString() is called but,
when the text is modified, calling .getText().toString() will still return "something".
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
Fragment loginFrag = new LoginFragment();
fragTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragTransaction.add(R.id.mainFrag, loginFrag);
fragTransaction.commit();
}
LoginFragment.java
public class LoginFragment extends Fragment{
EditText fieldID;
EditText fieldPassword;
Button logButton;
private FirebaseAuth mAuth;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_menu, container, false);
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
mAuth = FirebaseAuth.getInstance();
view = v;
fieldID = v.findViewById(R.id.fieldID);
fieldPassword = v.findViewById(R.id.fieldPassword);
logButton = v.findViewById(R.id.button_login);
logButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signIn(fieldID.getText().toString(), fieldPassword.getText().toString());
}
});
}
private void signIn(String email, String password) {
System.out.println("Email: " + email);
System.out.println("Pass: " + password);
if(!email.isEmpty() && !password.isEmpty()) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("Auth", "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
view.findViewById(R.id.logoMain).setVisibility(View.GONE);
} else {
// If sign in fails, display a message to the user.
Log.w("Auth", "signInWithEmail:failure", task.getException());
//updateUI(null);
}
}
});
}
}
}
Main Activity
<?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"
tools:context=".presenter.Activity.MainActivity">
<fragment
android:id="#+id/mainFrag"
android:name="com.raze.mfa.presenter.Fragment.LoginFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Layout
<?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="#FFFFFF"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="110dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoMain"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/bak"
tools:srcCompat="#drawable/bak" />
<ImageView
android:id="#+id/iconlock"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldPassword"
app:layout_constraintHorizontal_bias="0.523"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iconuser"
app:srcCompat="#mipmap/lockicon" />
<ImageView
android:id="#+id/iconuser"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="320dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldID"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/usericon" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/loginText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldPassword"
app:layout_constraintVertical_bias="0.188" />
<EditText
android:id="#+id/fieldPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldID" />
<EditText
android:id="#+id/fieldID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/logoMain"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="108dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/fieldID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/logoimg" />
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_login"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Unrelated:
I've Googled mostly everything but it seems that I haven't understand the basics of using Fragments, Activites, context, Views, and etc.
Ok I see, you are using a <fragment/> tag already in your MainActivity layout, and then you add with the FragmentTransaction a fragment to it.
The transaction is for FrameLayout containers that are empty and take a fragment. When you add the fragment statically through the <fragment/> tag the fragment is already loaded into the screen.
Something weird was happening when you added a new fragment to a fragment tag, maybe it duplicated the fragment and it was reading the one that is behind the actual fragment and it wasn't getting the input from the user.
Read here for the proper use of FragmentTransaction: https://developer.android.com/training/basics/fragments/fragment-ui
Taken from the docs above, see how they use a FrameLayout as the container.
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
}

I implemented card views in XML on Android Studios. How can I have this act as buttons?

I'm new to Android and recently created a dashboard for my app. I followed a tutorial but it only covered the front end. I created a layout called content_main.xml with three Cardviews. Each of these Cardviews contains the code
android:clickable="true"
Beginning the back end of things, I created a java receiver class called DashboardActivity (as seen below). I want to make my Cardviews clickable so that when one is clicked, I am taken to another screen within my app. The other screens in my app I am trying to get to are fragments. Each of these screens has its own java class in my "Fragments" folder. One of them, of which I try calling on in the code that follows is called SettingsViewFragment().
So far, I have tried the following. It does nothing.
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = (CardView) findViewById(R.id.dash_settings);
cd2 = (CardView) findViewById(R.id.dash_profile);
cd3 = (CardView) findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment1 = new SettingsViewFragment();
}
});
}
}
UPDATE: FULL CODE
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = findViewById(R.id.dash_settings);
cd2 = findViewById(R.id.dash_profile);
cd3 = findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Testing 123 123 123");
}
});
}
}
And my XML: (it continues for 3 cards this way)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.newproject.DashboardActivity"
tools:showIn="#layout/toolbar">
<FrameLayout
android:id="#+id/dashframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/bg_circle1"
android:src="#drawable/ic_settingspicture"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="View your settings"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow along with guided tutorials"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
Replace
Fragment fragment1 = new SettingsViewFragment();
With this
Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Main_Layout,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Change Fragment fragment1 = new SettingsViewFragment (); by Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();
//if show toast The problem is not the onClick, the problem is when loading the fragment
Fragments should be instantiated with the newInstance() static method.
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment settingsFragment = SettingsFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.contentView, settingsFragment);
fragmentTransaction.commit();
}
});
In the SettingsFragment
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
XML for content_main
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<FrameLayout
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>
This will transact the fragment into the FrameLayout that is positioned below the CardViews. Now this is probably not ideal, you would most likely want to launch a new activity that will host the fragment but this should work. Unless this is already in some Activity that hosts the fragment. But the key code is in the onClick.

Linear layout not getting visible OnClickListener

I have two layouts. I want to keep one layout gone when the activity is loaded and it should be visible onClick of another layout. so I have added this code OnClickListener of the layout.
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
And have set visibility of the layout gone in xml file..
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginRight="10dp"
android:drawableRight="#drawable/ic_expand_more_black_24dp"
android:text="Additional contact (optional)"
android:cursorVisible="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:id="#+id/LinearLayoutFrom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_weight="1"
android:hint="Name"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_weight="1"
android:hint="Phone"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OR"
android:id="#+id/textView19"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView13"
android:layout_marginLeft="48dp"
android:hint="Input if you're not receiver" />
</LinearLayout>
Unable to understand whats going wrong.. The listener is not getting called at all.. Please help..
Your problem is that your EditText is capturing the click event when you click on it. If you click somewhere else in the LinearLayout it should work.
You can replace the EditText with a TextView if you don't need the user to edit the content.
Change
linearLayoutFrom.setVisibility(View.VISIBLE);
To
findViewById(R.id.YOURLAYOUTID).setVisibility(View.VISIBLE);
I think you can't access to local vars in sub methods
It Seems That Layout Visibility is Already set to GONE, so Onlick listener is not working on hidden View and it should not work too.
INVISBLE means you are trying to add a listener to a view which is not there. You can add the listener to a visible view only.
WORKAROUND
1) Try to make a dummy view which is visible but having the same color as background.
2) Try to set the listener for parent and check the position (whether the position does
belongs to INVISIBLE view).
Please try to set the onClickListener to the EditText and to the ImageView and no to the LinearLayout
The problem is that the Handler of the EditText is most important that the LinearLayout handler.
Almost you can try to make a break point to the OnClick and see what is happend
This is an example to explain you how to do that:
in MainActivity Class:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout linearLayoutFrom;
private LinearLayout additionalContactFrom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
additionalContactFrom = (LinearLayout) findViewById(R.id.LinearLayoutAdditionalContactFrom);
linearLayoutFrom = (LinearLayout) findViewById(R.id.LinearLayoutFrom);
linearLayoutFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"linearLayoutFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (additionalContactFrom.getVisibility() == View.GONE) {
additionalContactFrom.setVisibility(View.VISIBLE);
} else {
additionalContactFrom.setVisibility(View.GONE);
}
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"additionalContactFrom clicked!!!", Toast.LENGTH_SHORT)
.show();
if (linearLayoutFrom.getVisibility() == View.GONE) {
linearLayoutFrom.setVisibility(View.VISIBLE);
} else {
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
}
}
in xml file:
<FrameLayout 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:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayoutAdditionalContactFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayoutFrom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginRight="50dp"
android:background="#android:color/holo_green_light"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:hint="Input if you&apos;re not receiver"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
This is very important that when you want add some view (for example
add a linearlayout to another linerlayout). you should use framelayout or
relativelayout(do not use linearlayout) for do that.

java.lang.NoSuchMethodError: android.app.Dialog.create

I have created custom Dialog layout. When user press on the button the dialog is shown. Android version lollipop shows me the dialog perfectly but in case of lower version than lollipop it throws me error.
Here is my code:
public class MainActivity extends AppCompatActivity {
private Button click;
private Dialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=(Button)findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(click))
{
dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.check_in_weight_dialog);
dialog.create();
dialog.show();
}
}
});
}
And here is my custom dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check In Weight"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="20">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:inputType="number"
android:hint="Current Weight"
android:background="#drawable/editext_border"
android:id="#+id/edtCheckInWhgt"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="10">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:gravity="center"
android:id="#+id/btncheckin"
android:textColor="#android:color/holo_blue_light"
android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>
</LinearLayout>
dialog.create();
this is for lolipop only. Remove this line
Try this way ,It works fine (Lolipop)
custom_dialog = new Dialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth);
custom_dialog.getWindow().setBackgroundDrawable(new ColorDrawable((0xff000000)));
custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custom_dialog.setCancelable(false);
custom_dialog.setContentView(R.layout.check_in_weight_dialog);
custom_dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Color.parseColor("#FFFFFF"));
custom_dialog.show();
Or Remove dialog.create();
dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.check_in_weight_dialog);
dialog.show();

Categories