public class PostDetails extends AppCompatActivity {
TextView like,dislike,date,time,name,username;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private ArrayList<PostInfo> postInfos;
int i;
ImageView glideImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
i = getIntent().getIntExtra("intVariableName", 0);
postInfos = new ArrayList<>();
like = findViewById(R.id.like);
dislike = findViewById(R.id.dislike);
name = findViewById(R.id.name);
username = findViewById(R.id.username);
date= findViewById(R.id.date);
time = findViewById(R.id.time);
glideImage = findViewById(R.id.glideImage);
getData();
}
public void getData(){
reference.addValueEventListener (new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot posts = dataSnapshot.child ("Posts");
for (DataSnapshot time: posts.getChildren ()){
DataSnapshot url = time.child ("Url");
DataSnapshot name = time.child ("Name");
DataSnapshot username = time.child ("Username");
DataSnapshot date = time.child ("Date");
DataSnapshot likes = time.child("Likes");
DataSnapshot times = time.child("Time");
DataSnapshot dislikes = time.child("DisLikes");
PostInfo postInfo = new PostInfo (String.valueOf (url.getValue ())
,String.valueOf (name.getValue ())
,String.valueOf (username.getValue ())
,String.valueOf (date.getValue ())
,time.getKey()
,String.valueOf(likes.getValue())
,String.valueOf(dislikes.getValue())
,String.valueOf(times.getValue()));
postInfos.add (postInfo);
}
like.setText(postInfos.get(i).likes);
dislike.setText(postInfos.get(i).dislikes);
name.setText(postInfos.get(i).name);
username.setText(postInfos.get(i).username);
date.setText(postInfos.get(i).date);
time.setText(postInfos.get(i).time);
Glide.with(getApplicationContext()).load(postInfos.get(i).Url).into(glideImage);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (PostDetails.this, "Error 411: " + databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
Here are six TextView which are initialised but when I try to write the values using setText, it shows this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at lifeline.learn.com.hotornot.PostDetails$1.onDataChange(PostDetails.java:67)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zzcal(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
I tried many things to get rid of this exception but fails every time. I am new to Android Programming so help me out of this exception. I filtered many questions on overflow but doesnt find any appropriate answer which can solve this exception.Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".PostDetails">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:id="#+id/glideImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/glideImage"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/likes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/like"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:text="#string/dislikes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/dislike"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="#+id/date"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="#string/time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="#string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="#string/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="#string/value"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
In your XML, there is no id,like this: android:id="#+id/fullscreen_content"
as you know findViewById may return null,please check your view initialize...
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {#link #onCreate}.
*
* #return The view if found or null otherwise.
*/
#SuppressWarnings("TypeParameterUnusedInFormals")
#Nullable
public abstract <T extends View> T findViewById(#IdRes int id);
Demo:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#0099cc"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:baselineAligned="false"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:id="#+id/fullscreen_choose_controls"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/fullscreen_esop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_esop"
android:textAllCaps="false"
android:textColor="#android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:ellipsize="end"
android:gravity="center"
android:text="#string/dummy_button"
android:textColor="#android:color/holo_red_dark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/fullscreen_android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_android"
android:textAllCaps="false"
android:textColor="#android:color/holo_blue_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/fullscreen_make_your_choice"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="#string/vs_make_your_choice"
android:textColor="#android:color/holo_red_dark"
android:textSize="50sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
Related
I've got a NestedScrollView inside of a ScrollView because I have an Activity with three hidden fragments that contain text, and when you click one of the fragments it opens to reveal the text. But the thing is that there is quite a bit of text so I'm using NestedScrollView so the reader can read all of it, and the first time it works fine (text doesn't overlap), and then second time when I close the fragment and re-open it the text starts overlapping...
So as mentioned, first time it doesn't happen, and then I close the fragment and reopen it and this happens:
So why is this happening and how can I fix it?
Fragment inside Activity
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="none"
tools:context=".Fragment.PrivacyFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/Grey"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginEnd="20dp"
android:padding="8dp"
android:textColor="#color/colorBlack"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/button_gradient_two">
<RelativeLayout
android:id="#+id/relative_layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="10dp">
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/arrow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="25dp"
android:padding="8dp"
android:src="#drawable/icon_search_100" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:padding="8dp"
android:text="Settings"
android:textColor="#color/White"
android:textSize="22sp"
android:textStyle="bold" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image_profile"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:elevation="16dp"
android:src="#drawable/profile_placeholder" />
<com.google.android.material.card.MaterialCardView
style="#style/CustomCardViewStyle"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:layout_marginTop="150dp"
app:cardElevation="12dp">
<RelativeLayout
android:id="#+id/relative_layout_two"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:text="Jack Sparrow"
android:textColor="#color/Black"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="jacksparrow"
android:textColor="#color/Grey"
android:textSize="22sp"
android:textStyle="" />
<TextView
android:id="#+id/bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/username"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:gravity="center_horizontal"
android:text="This is my bio!"
android:textColor="#color/Grey"
android:textStyle="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bio"
android:orientation="vertical">
<TextView
android:id="#+id/privacy"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/privacy"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_privacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/security"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/security"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_security"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/help"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/help"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/about"
style="#style/SelectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="#string/about"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<FrameLayout
android:id="#+id/container_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
SettingsActivity
public class SettingsActivity extends AppCompatActivity {
private ImageView mImageProfile, mBackArrow;
private TextView mName, mUsername, mBio, mPrivacy, mSecurity, mHelp, mAbout;
private FirebaseUser mFirebaseUser;
private FrameLayout mPrivacyContainer, mSecurityContainer, mHelpContainer, mAboutContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_tester);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mImageProfile = findViewById(R.id.image_profile);
mName = findViewById(R.id.name);
mUsername = findViewById(R.id.username);
mBio = findViewById(R.id.bio);
mPrivacy = findViewById(R.id.privacy);
mSecurity = findViewById(R.id.security);
mHelp = findViewById(R.id.help);
mAbout = findViewById(R.id.about);
mImageProfile.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_profile_picture));
mName.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_name));
mUsername.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_username));
mBio.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.profile_animation_bio));
mPrivacyContainer = findViewById(R.id.container_privacy);
mSecurityContainer = findViewById(R.id.container_security);
mHelpContainer = findViewById(R.id.container_help);
mAboutContainer = findViewById(R.id.container_about);
mPrivacy.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
});
mSecurity.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_security, new SecurityFragment(), null).addToBackStack(null).commit();
if (mSecurityContainer.getVisibility() == View.GONE) {
mSecurityContainer.setVisibility(View.VISIBLE);
} else {
mSecurityContainer.setVisibility(View.GONE);
}
});
mHelp.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_help, new HelpFragment(), null).addToBackStack(null).commit();
if (mHelpContainer.getVisibility() == View.GONE) {
mHelpContainer.setVisibility(View.VISIBLE);
} else {
mHelpContainer.setVisibility(View.GONE);
}
});
mAbout.setOnClickListener(v -> {
getSupportFragmentManager().beginTransaction().add(R.id.container_about, new AboutFragment(), null).addToBackStack(null).commit();
if (mAboutContainer.getVisibility() == View.GONE) {
mAboutContainer.setVisibility(View.VISIBLE);
} else {
mAboutContainer.setVisibility(View.GONE);
}
});
getUserInfo();
setBackArrow();
}
private void getUserInfo() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user != null) {
mName.setText(user.getFullname());
mUsername.setText(user.getUsername());
mBio.setText(user.getBio());
if (user.getImageurl() == null) {
mImageProfile.setImageResource(R.drawable.profile_placeholder);
} else {
Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void setBackArrow() {
mBackArrow = findViewById(R.id.arrow_back);
mBackArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
You are getting the fragment overlapped because there is a new fragment added each time.
You simply need to do a check if the fragment exits already then there is no need to add a new fragment. You can assign a 'tag' when adding fragment. Code as follows-
if (fragmentManager.findFragmentByTag("First Fragment") == null)
fragmentManager.beginTransaction().add(R.id.fragment, new FirstFragment(), "First Fragment").commit();
OR
Rather then using .add() method use .replace() to make sure fragment got replaced each time.
According to your Activity code, you need to choose the correct way, but make sure there is no fragment getting re-created without destroying previous one.
Happy Coding !!
You are adding two fragments on top of each other.
Instead of add use replace when executing the fragment transaction:
From:
getSupportFragmentManager().beginTransaction().add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
To:
getSupportFragmentManager().beginTransaction().replace(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mPrivacyContainer.getVisibility() == View.GONE) {
mPrivacyContainer.setVisibility(View.VISIBLE);
} else {
mPrivacyContainer.setVisibility(View.GONE);
}
I have two radiobutton groups that need to be initialized when loading the screen, so I used this code:
mVisibilityPublic.setChecked(true);
But when I load the screen I get the following (first is the wrong behavior, second is the expected one):
I've tried with all these:
mVisibilityPublic.setSelected(true);
mVisibilityPublic.performClick();
mVisibilityPublic.setActivated(true);
mVisibilityPublic.invalidate();
mVisibilityPublic.requestLayout();
But I get the same result.
<ScrollView 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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="#dimen/form_vertical_spacing"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".createevent.CreateEventActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/create_event_privacy"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="#+id/create_visibility_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/createevent_visibility_public"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_public_event"/>
<RadioButton
android:id="#+id/createevent_visibility_private"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_private_event"/>
<RadioButton
android:id="#+id/createevent_visibility_ppv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv"/>
</RadioGroup>
<LinearLayout
android:id="#+id/ppv_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="#string/create_event_ppv_section"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="#+id/ppv.currency.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.myapp.ui.AutoCompleteCombo
android:id="#+id/ppv.currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_ppv_currency">
</com.myapp.ui.AutoCompleteCombo>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/ppv.price.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1">
<android.support.design.widget.TextInputEditText
android:id="#+id/ppv.price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_ppv_price"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RadioGroup
android:id="#+id/ppv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/ppv_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_allcountries"/>
<RadioButton
android:id="#+id/ppv_allexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_except"/>
<RadioButton
android:id="#+id/ppv_only"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_some"/>
</RadioGroup>
<com.myapp.countries.CountriesMultiCombo
android:id="#+id/create.ppv.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout>
<View
android:id="#+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/general_info"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="-32dp"
android:layout_marginRight="-32dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#color/colorDivider"
/>
<LinearLayout
android:id="#+id/geoblock_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="#string/geo_blocking_settings_title"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="#+id/geo_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/geo_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_none"/>
<RadioButton
android:id="#+id/geo_blockonly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_block_only"/>
<RadioButton
android:id="#+id/geo_blockexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_block_all_except"/>
</RadioGroup>
<com.myapp.createevent.countries.CountriesMultiCombo
android:id="#+id/create.geo.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout> <Button
android:id="#+id/test_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLICK ME"/>
</LinearLayout>
</ScrollView>
Note that if I select the option manually (like I did with the second example in the image) I get the proper result. Its only if I do it programmatically that its missing the inner circle.
public class TestFragment extends Fragment {
private Unbinder mUnbinder;
#BindView(R.id.createevent_visibility_public)
RadioButton mVisibilityPublic;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_advanced_opts, container, false);
mUnbinder = ButterKnife.bind(this, inflate);
return inflate;
}
public static TestFragment newInstance(int tvId, int eventId) {
TestFragment testFragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(TV_ARGUMENT, tvId);
args.putInt(EVENTID_ARGUMENT, eventId);
testFragment.setArguments(args);
return testFragment;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mVisibilityPublic.setChecked(true);
}
}
This question already has answers here:
Add positive button to Dialog
(3 answers)
Closed 5 years ago.
I am new in android app development. I am making a project in android-studio.I want to show pop up dialog whatever I have declared in xml file but OK button is not showing. If anyone knows about it, please share your answer.xml and java codes are below
image_layout.xml
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!--<ImageView android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" android:src="YOUR IMAGE"/>-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewbluetooth"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingTop="180dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth not enabled"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enable bluetooth in settings and restart this application"
android:textStyle="bold"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="OK" android:onClick="dismissListener"/>
</LinearLayout>
</android.support.v7.widget.CardView></LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="litifer.awesome.game.litifer_carddemo.MainActivity"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView1"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardElevation="0dp"
android:clickable="true"
app:cardBackgroundColor="#android:color/transparent"
android:background="#33FF0000"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageBeacon"
android:src="#drawable/beacon"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView1text"
android:text="Beacon Demo"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView2"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardElevation="0dp"
android:clickable="true"
app:cardBackgroundColor="#android:color/transparent"
android:background="#33FF0000"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageWifi"
android:src="#drawable/wifi"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView2text"
android:text="Wifi Demo"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView3"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardElevation="0dp"
android:clickable="true"
app:cardBackgroundColor="#android:color/transparent"
android:background="#33FF0000"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageCustomerReview"
android:src="#drawable/customerreview"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView3text"
android:text="CustomerReview Demo"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView4"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardElevation="0dp"
android:clickable="true"
app:cardBackgroundColor="#android:color/transparent"
android:background="#33FF0000"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imagecardview"
android:src="#drawable/cardview"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView4text"
android:text="CardView Demo"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView5"
app:cardCornerRadius="5dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:cardElevation="0dp"
android:clickable="true"
app:cardBackgroundColor="#android:color/transparent"
android:background="#33FF0000"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imagedemo"
android:src="#drawable/demo"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView5text"
android:text="Test All Demo"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout> </ScrollView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView beacon,cardview,customerreview,demo,wifi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beacon = (ImageView)findViewById(R.id.imageBeacon);
beacon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
} else {
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth is not enable :)
Dialog settingsDialog = new Dialog(MainActivity.this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout
, null));
settingsDialog.show();
}
}
}
});
}
}
try this
Dialog settingsDialog = new Dialog(this);
settingsDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(R.layout.image_layout);
settingsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
settingsDialog.setCancelable(true);
settingsDialog.setTitle("Title...");
settingsDialog.show();
I'm making a football league app, and i need to show the classification.
I'm trying to show a header that indicates what represents each field and a listview with the teams below.
I'm using a SimpleCursorAdapter to fill the list, but if the values have a different number of digits the list is misaligned. ¿How can i align it or at least add a 0 before the one-digit numbers with the adapter?
The soure code of the fragment and the xml:
ClassificacioFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] from = { EquipsContract.EquipEntry.COLUMN_NAME_NOM,
EquipsContract.EquipEntry.COLUMN_NAME_VICTORIES, EquipsContract.EquipEntry.COLUMN_NAME_DERROTES,
EquipsContract.EquipEntry.COLUMN_NAME_EMPATS, EquipsContract.EquipEntry.COLUMN_NAME_PUNTS};
int[] to = {R.id.item_nom, R.id.item_victories, R.id.item_derrotes, R.id.item_empats, R.id.item_punts};
View rootview = inflater.inflate(R.layout.fragment_classificacio, container, false);
ListView listView = (ListView) rootview.findViewById(R.id.classificacio);
listView.invalidateViews();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(rootview.getContext(), R.layout.equip_row,
DBHandler.getDbInstance(getContext()).cursorClassificacio(), from, to);
listView.setAdapter(adapter);
String size = Integer.toString(DBHandler.getDbInstance(getContext()).getAllEquips().size());
Toast toast = Toast.makeText(getContext(), size, Toast.LENGTH_LONG);
toast.show();
return rootview;
}
fragment XML:
<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"
tools:context="idi.francesc.footballleague.ClassificacioFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header_classificacio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/header_equip"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="28dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/header_victories"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/header_d"
android:layout_marginEnd="30dp"
/>
<TextView
android:id="#+id/header_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/header_derrotes"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/header_e"
android:layout_marginEnd="27dp"
/>
<TextView
android:id="#+id/header_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/header_empats"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/header_p"
android:layout_marginEnd="38dp"
/>
<TextView
android:id="#+id/header_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/header_punts"
android:textSize="18sp"
android:layout_marginEnd="12dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<ListView
android:id="#+id/classificacio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header_classificacio">
</ListView>
</RelativeLayout>
</FrameLayout>
Listview Row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/item_nom"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textSize="20sp"
android:padding="2dp"
android:layout_marginStart="10dp"
android:text="F.C. Barcelona"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/item_victories"
android:padding="2dp"
android:textSize="15sp"
android:text="10"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/item_derrotes"
android:layout_marginEnd="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/item_derrotes"
android:padding="2dp"
android:textSize="15sp"
android:text="20"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/item_empats"
android:layout_marginEnd="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/item_empats"
android:padding="2dp"
android:textSize="15sp"
android:text="30"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/item_punts"
android:layout_marginEnd="25dp"/>
<TextView
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="wrap_content"
android:id="#+id/item_punts"
android:padding="6dp"
android:textSize="18sp"
android:text="40"
android:gravity="center_vertical"
android:layout_marginEnd="2dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
And this is the result:
thanks.
You can simplify your ListView Row to use LinearLayout with weight parameters. It's more efficient and prevents issues with TextView's floating width.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="9">
<TextView
android:id="#+id/item_nom"
android:layout_width="0dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textSize="20sp"
android:padding="2dp"
android:layout_marginStart="10dp"
android:layout_weight="4"
android:text="F.C. Barcelona"/>
<TextView
android:layout_width="0dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:id="#+id/item_victories"
android:textSize="15sp"
android:text="10"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:id="#+id/item_derrotes"
android:textSize="15sp"
android:text="20"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:id="#+id/item_empats"
android:textSize="15sp"
android:text="30"
android:gravity="center"/>
<TextView
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="0dp"
android:layout_weight="2"
android:id="#+id/item_punts"
android:textSize="18sp"
android:text="40"
android:gravity="center"/>
</LinearLayout>
At first all of your text in the TextView must be center aligned.
android:gravity="center"
Then, pogramatically you should find the largest TextView and set the width of each other TextView in that row.
Or you use TableLayout ;)
Create a function for appending 0 with numbers less than 10.
public String appendZero(int input) {
if (input >= 10) {
return String.valueOf(input);
} else {
return "0" + String.valueOf(input);
}
}
While setting the text, call like this :
item_victories.setText(appendZero(victory_count.get(position)));
Hope this one will works for you.
this is my code where there is an error.
i think that every its ok but i dont know where is the error
this is my code:
public class MainActivity extends Activity implements OnItemSelectedListener{
private EditText promAcum;
private EditText credCur;
private EditText sem;
private EditText num_examenes;
private EditText editText13;
private EditText editText14;
private EditText editText15;
private EditText editText16;
private EditText editText17;
private EditText editText18;
private EditText editText19;
private EditText editText20;
private EditText editText21;
private Spinner spinner1;
public String valorPromAcum;
public String valorCredCur;
public String valorSem;
public String[] numeroExamenes={"3","4","5","6"};
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new MainFragment());
ft.commit();
}
promAcum = (EditText) findViewById(R.id.promAcum);
credCur = (EditText) findViewById(R.id.credCur);
sem = (EditText) findViewById(R.id.sem);
editText13 = (EditText)findViewById(R.id.editText13);
editText14 = (EditText)findViewById(R.id.editText14);
editText15 = (EditText)findViewById(R.id.editText15);
editText16 = (EditText)findViewById(R.id.editText16);
editText17 = (EditText)findViewById(R.id.editText17);
editText18 = (EditText)findViewById(R.id.editText18);
editText19 = (EditText)findViewById(R.id.editText19);
editText20 = (EditText)findViewById(R.id.editText20);
editText21 = (EditText)findViewById(R.id.editText21);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,numeroExamenes);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
"On Button Click : " +
"\n" + String.valueOf(spinner1.getSelectedItem()) ,
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});}
this is my xml code
<LinearLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/materias"
android:textSize="20sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:inputType="text" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/creditos"
android:textSize="20sp" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginBottom="30sp">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cantidad de Examenes"
android:textSize="20sp" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:text="Examen" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:text="Nota" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:layout_weight="2"
android:text="%" />
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"/>
<EditText
android:id="#+id/editText20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal"/>
<EditText
android:id="#+id/editText21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/guardar" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:onClick="agregarNotas"
android:text="#string/materias" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="#string/nueva" />
</LinearLayout>
logcat error line 78 : spinner1.setadapter(adapter);
enter code here09-03 11:25:46.344: E/AndroidRuntime(7841): Caused by: java.lang.NullPointerException
09-03 11:25:46.344: E/AndroidRuntime(7841): at com.jonathanar.promapp.MainActivity.onCreate(MainActivity.java:78)
any suggestions are appreciated
You are trying to retrieve Views from Activity, but they are in Fragment.
getActivity().findViewById(R.id.yourId); is what you should do whenever you are using findViewById() method.
The NullPointerException is not a problem with instantiating your adapter, but rather with the way you are getting the Spinner.
If the spinner exists in the fragment you need to specify its context as so.
just putting findViewById() is really this.findViewById(), where this is your activity. In other words your telling the method to find views in the content view of the activity.
If the spinner is in the content view of the fragment, you need to instead use
fragment.getActivity().findViewById(R.id.???);
this way findViewById() knows to find your spinner in the context of the fragment.
Hope this helps!