My app contains a few textViews which are supposed to be scrolling horizontally. This works when my layout is first loaded but after clicking on a button to load another layout and then re-click on that button to load back the first layout those textViews start scrolling after a "big" delay(like 20 secs+). I'm trying to figure out the source of this problem with the Layout Inspector, Hierarchy Viewer etc. but no luck.
UPDATE: I noticed something strange today.When i go from the 1st layout to the 2nd one and vice versa, although the TextViews stop scrolling, if i close and re-open the phone's screen, the scrolling works like a charm. This seems totally strange to me, do you know what's causing this and why?
CardViewActivity.java:
public class CardViewActivity extends AppCompatActivity {
private ImageView cardArtImageView;
private TextView leaderSkillDescText;
private TextView superAttackTitleText;
private TextView superAttackDescText;
private TextView passiveSkillTitleText;
private TextView passiveSkillDescText;
private TextView hpText;
private TextView attText;
private TextView defText;
private TextView costText;
private Button arrowButton;
private int selectedItemPosition;
private boolean isBtnClicked = false;
// Listener member field for each layout's button. This listener will be used recursively
private View.OnClickListener arrowButtonListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// When the arrowButton is clicked, choose the right layout based on the button's state
int resID = isBtnClicked ? R.layout.cardview_refined : R.layout.cardview_expand_details;
setContentView(resID);
// If we're in the first layout, initialize the cardArtImageView field
if(isBtnClicked) {
cardArtImageView = findViewById(R.id.cardArtImageView);
}
viewDefinitions(!isBtnClicked);
initCardViewData(selectedItemPosition);
setSelectedViewsInit();
// Set the arrowButton's listener to this listener (recursively)
arrowButton.setOnClickListener(arrowButtonListener);
// toggle our flag field
isBtnClicked = !isBtnClicked;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_refined);
// Retrieving the data sent over from MainActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
selectedItemPosition = bundle.getInt("Card Index");
}
//Toast.makeText(this, "WIDTH: " + SCREEN_WIDTH, Toast.LENGTH_SHORT).show();
// Initializing our views
cardArtImageView = findViewById(R.id.cardArtImageView);
viewDefinitions(false);
setSelectedViewsInit();
initCardViewData(selectedItemPosition);
arrowButton.setOnClickListener(arrowButtonListener);
}
/**
* Sets the required textViews as selected to allow automatic scrolling
*/
private void setSelectedViewsInit() {
leaderSkillDescText.setSelected(true);
superAttackTitleText.setSelected(true);
superAttackDescText.setSelected(true);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setSelected(true);
passiveSkillDescText.setSelected(true);
}
}
/**
* Adds the views's definitions
*
* #param initPassiveInfo used to decide whether or not the passiveSkillDesc & ..Title != null
* so that they can be defined
*/
private void viewDefinitions(boolean initPassiveInfo) {
leaderSkillDescText = findViewById(R.id.leaderSkillDesc);
superAttackTitleText = findViewById(R.id.superAttackTitle);
superAttackDescText = findViewById(R.id.superAttackDesc);
if (initPassiveInfo) {
passiveSkillTitleText = findViewById(R.id.passiveSkillTitle);
passiveSkillDescText = findViewById(R.id.passiveSkillDesc);
} else {
Log.d("Definitions", "Passive info == null");
}
hpText = findViewById(R.id.HP);
attText = findViewById(R.id.ATT);
defText = findViewById(R.id.DEF);
costText = findViewById(R.id.COST);
arrowButton = findViewById(R.id.arrowButton);
}
/**
* Initialize the cardViewActivity's views with the data from the CardInfoDatabase.java class
*
* #param selectedItemPosition Used to initialize this activity's views if the intent was called from the MainScreen Fragment
*/
private void initCardViewData(int selectedItemPosition) {
if (cardArtImageView != null) {
cardArtImageView.setImageResource(CardInfoDatabase.cardArts[selectedItemPosition]);
}
leaderSkillDescText.setText(CardInfoDatabase.leaderSkills[selectedItemPosition]);
superAttackTitleText.setText(CardInfoDatabase.superAttacksName[selectedItemPosition]);
superAttackDescText.setText(CardInfoDatabase.superAttacksDesc[selectedItemPosition]);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setText(CardInfoDatabase.passiveSkillsName[selectedItemPosition]);
passiveSkillDescText.setText(CardInfoDatabase.passiveSkillsDesc[selectedItemPosition]);
}
hpText.setText(CardInfoDatabase.hp[selectedItemPosition].toString());
attText.setText(CardInfoDatabase.att[selectedItemPosition].toString());
defText.setText(CardInfoDatabase.def[selectedItemPosition].toString());
costText.setText(CardInfoDatabase.cost[selectedItemPosition].toString());
}
}
firstLayout.xml:
<?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"
android:background="#drawable/card_view_bg"
tools:layout_editor_absoluteY="25dp">
<ImageView
android:id="#+id/cardArtImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/cardDetailsImageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="#string/card_image" />
<!--
<ImageView
android:id="#+id/cardDetailsImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/card_details_box" /> -->
<!-- Implement scrolling text
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="225dp"
android:background="#drawable/card_details_closed">
<TextView
android:id="#+id/COST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/leaderSkillDesc"
android:layout_marginBottom="9dp"
android:layout_toEndOf="#+id/superAttackDesc"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="00"
android:textColor="#color/white"
android:textSize="17sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="33dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/ATT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="73dp"
android:layout_toEndOf="#+id/HP"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/DEF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="42dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
</RelativeLayout>
<TextView
android:id="#+id/leaderSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_above="#+id/superAttackTitle"
android:layout_alignParentEnd="true"
android:layout_marginBottom="13dp"
android:layout_marginEnd="37dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintVertical_bias="0.626" />
<TextView
android:id="#+id/superAttackTitle"
android:layout_width="245dp"
android:layout_height="15dp"
android:layout_above="#+id/superAttackDesc"
android:layout_alignStart="#+id/superAttackDesc"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/superAttackDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="#+id/superAttackDesc"
android:layout_width="255dp"
android:layout_height="15dp"
android:layout_alignEnd="#+id/leaderSkillDesc"
android:layout_alignParentBottom="true"
android:layout_marginBottom="74dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="#+id/arrowButton"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_marginBottom="7dp"
android:layout_marginStart="75dp"
android:background="#drawable/arrow_up"
android:textOff=""
android:textOn="" />
</RelativeLayout>
</LinearLayout>
SecondLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#drawable/card_view_bg"
tools:layout_editor_absoluteY="25dp">
<!-- Implement scrolling text
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/card_details_open">
<TextView
android:id="#+id/COST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout3"
android:layout_marginTop="48dp"
android:layout_toEndOf="#+id/superAttackDesc"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="00"
android:textColor="#color/white"
android:textSize="17sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/relativeLayout3">
<TextView
android:id="#+id/HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="33dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/ATT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="73dp"
android:layout_toEndOf="#+id/HP"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/DEF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="42dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
</RelativeLayout>
<TextView
android:id="#+id/leaderSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:layout_below="#+id/COST"
android:layout_marginEnd="36dp"
android:layout_marginTop="16dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintVertical_bias="0.626" />
<TextView
android:id="#+id/superAttackTitle"
android:layout_width="245dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_below="#+id/leaderSkillDesc"
android:layout_marginTop="23dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/superAttackDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/superAttackDesc"
android:layout_width="255dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/superAttackTitle"
android:layout_below="#+id/superAttackTitle"
android:layout_marginTop="5dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/passiveSkillTitle"
android:layout_width="255dp"
android:layout_height="17dp"
android:layout_alignStart="#+id/superAttackDesc"
android:layout_below="#+id/superAttackDesc"
android:layout_marginBottom="3dp"
android:layout_marginStart="8dp"
android:layout_marginTop="23dp"
android:background="#color/passiveSkillNameBackground"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="7dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textColorHighlight="#android:color/black"
android:textSize="13sp"
android:textStyle="italic"
android:visibility="visible"
tools:layout_editor_absoluteX="207dp"
tools:layout_editor_absoluteY="543dp" />
<TextView
android:id="#+id/passiveSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/passiveSkillTitle"
android:layout_below="#+id/passiveSkillTitle"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
android:visibility="visible"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="602dp" />
<Button
android:id="#+id/arrowButton"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_marginBottom="7dp"
android:layout_marginStart="75dp"
android:background="#drawable/arrow_down"
android:textOff=""
android:textOn="" />
</RelativeLayout>
</RelativeLayout>
I'd suggest using a RecyclerView with a custom Adapter, RecyclerView can be used vertically or horizontally.
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 want to make pin input with Buttons and TextView in Android.
I have Textview for pin , and 9 Buttons for user to press as digits.
What i want when user press four buttons to change my Textview letter by letter(Button by Button) , and when entered fourth button to start code
Here is image:
Any tips please?
Here is my code:
content_main.xml :
<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:background="#color/white"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.group.digit.razvoj.appointment.MainActivity"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/pinLayoutMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:orientation="vertical"
android:paddingRight="#dimen/left_margin_pin">
<LinearLayout
android:id="#+id/pinTvLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/between_margin"
android:layout_marginTop="#dimen/between_margin"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/pinTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/pin"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/passLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/pinEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="****"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="1"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="2"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="3"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/secondRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="4"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b5"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="5"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b6"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="6"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/sthirdRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/b7"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="7"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b8"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="8"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b9"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="9"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/forthow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="1"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/b0"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textColor="#color/black"
android:textSize="30sp" />
<Button
android:id="#+id/bback"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="-"
android:textColor="#color/black"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init buttons
for (int i = 0; i < MAX_PIN_NUM; i++) {
buttonsPinWidget[i] = (Button) findViewById(buttonsPinId[i]);
}
pinentry = (TextView) findViewById(R.id.pinEntry);
}
You should to use one onClickListener for everyone button with switch case inside of it.
In every case of switch case block your should addor delete (if you have the button for deleting) one character it TextView.
After switch case block check the text length in your TextView. If the length of it equals 4, you can try to authorization or login or other actions.
Sorry for bad English.
Very interesting question first of all.
The only solution I can think of is to create a public method which will always check the length of the textview (which I think should be edittext) and add that method for every button. Once the length of the textview is 4, trigger your code you want.
Let me know if you need some more clarification
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s=txtView.getText().toString();
if(s.length()<3){
s=s+btn.getText().toString();
txtView.setText(""+s);
}else if(s.length()==3){
s=s+btn.getText().toString();
txtView.setText(""+s);
// write your code
}
}
});
I am developing an app to help people know when it is time for a phone upgrade. I ran into a little problem along the way, mainly when trying to test the functionality of the code. I rated all 6 categories and pressed the continue button. Only the rating for the "Game" shows up. I don't know what is going on since I have all six textviews included.
Shows only one rating
Java:
package com.inducesmile.phoneupgrade;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class Page1 extends Activity {
TextView tvBatt, tvPerf, tvAttr, tvCam, tvVal, tvGame;
RatingBar rateBattery, ratePerformance, rateCamera, rateValue, rateGaming, rateAttractiveness;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Init();
}
public void Init() {
tvBatt = (TextView) findViewById(R.id.tvBatt);
tvPerf = (TextView) findViewById(R.id.tvPerf);
tvAttr = (TextView) findViewById(R.id.tvAttr);
tvCam = (TextView) findViewById(R.id.tvCam);
tvVal = (TextView) findViewById(R.id.tvVal);
tvGame = (TextView) findViewById(R.id.tvGame);
rateBattery = (RatingBar) findViewById(R.id.rateBatt);
rateCamera = (RatingBar) findViewById(R.id.rateCam);
rateGaming = (RatingBar) findViewById(R.id.rateGame);
rateValue = (RatingBar) findViewById(R.id.rateValue);
ratePerformance = (RatingBar) findViewById(R.id.Performance);
rateAttractiveness = (RatingBar) findViewById(R.id.rateAttr);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.btnCont) {
String strBatt = String.valueOf(rateBattery.getRating());
tvBatt.setText(strBatt);
String strAttr = String.valueOf(rateAttractiveness.getRating());
tvAttr.setText(strAttr);
String strCam = String.valueOf(rateCamera.getRating());
tvCam.setText(strCam);
String strPerf = String.valueOf(ratePerformance.getRating());
tvPerf.setText(strPerf);
String strGame = String.valueOf(rateGaming.getRating());
tvGame.setText(strGame);
String strVal = String.valueOf(rateValue.getRating());
tvVal.setText(strVal);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:background="#FFF000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="How Important Are These Features to You?"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="#000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue"
android:textColor="#FFFFFF"
android:id="#+id/btnCont"
android:background="#drawable/buttonshape"
android:onClick="onButtonClick"
android:layout_below="#+id/Performance"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/textView2" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateCam"
android:layout_alignLeft="#+id/rateGame"
android:layout_alignStart="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateGame" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/rateCam" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateValue"
android:layout_below="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Performance"
android:layout_below="#+id/rateValue"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Battery Life:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gaming:"
android:id="#+id/textView4"
android:layout_alignTop="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Camera:"
android:id="#+id/textView5"
android:layout_below="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Attractiveness:"
android:id="#+id/textView6"
android:layout_marginTop="14dp"
android:layout_alignTop="#+id/rateAttr"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Value:"
android:id="#+id/textView7"
android:layout_alignTop="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Performance:"
android:id="#+id/textView8"
android:layout_below="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBatt"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvGame"
android:layout_alignBottom="#+id/textView4"
android:layout_toRightOf="#+id/textView4"
android:layout_toEndOf="#+id/textView4"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvCam"
android:layout_alignBottom="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:layout_toEndOf="#+id/textView5"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvAttr"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvVal"
android:layout_alignBottom="#+id/textView7"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPerf"
android:layout_alignBottom="#+id/textView8"
android:layout_toRightOf="#+id/textView8"
android:layout_toEndOf="#+id/textView8"
android:text="____" />
Your code is fine, try rebuild/clean the project.
If you're using Android Studio, go to Build -> Clean Project and then Build -> Rebuild Project.
I believe to have the textview, but it cannot be cast to listview error in the following code below:
Essentially I am trying to populate information from Parse into a textfield. This I believe is the part of the code that is problematic.
here it throws a "textview cannot be cast to android.widget.listview"
public void done(List<ParseUser> userlistactivityname, ParseException e) {
if (e == null) {
for (int i = 0; i < userlistactivityname.size(); i++) {
mUserActivityNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistactivityname);
mUserActivityNameRetrieved.setText(userlistactivityname.get(i).get("ActivityName").toString());
}
} else {
//else..
}
}
Below is the XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue_bac3"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/profilePictureresult"
android:layout_marginTop="16dp"
android:alpha="0.7"
android:src="#drawable/left_right" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageView2"
android:alpha="0.7"
android:src="#drawable/left_arrow2" />
<TextView
android:id="#+id/userlistactivityname"
android:layout_width="220dp"
android:layout_height="50dp"
android:cacheColorHint="#android:color/transparent"
android:layout_below="#+id/textView1"
android:layout_toLeftOf="#+id/imageView2"
android:divider="#null" >
</TextView>
<ListView
android:id="#+id/userlistheadline"
android:layout_width="280dp"
android:layout_height="90dp"
android:layout_below="#+id/userlistname"
android:layout_centerHorizontal="true"
android:cacheColorHint="#android:color/transparent"
android:divider="#null"
android:textAlignment="center" >
</ListView>
<com.parse.ParseImageView
android:id="#+id/profilePictureresult"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_below="#+id/userlistheadline"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:alpha="1"
android:background="#drawable/border_image"
android:cropToPadding="true"
android:padding="3dp"
android:scaleType="centerCrop" />
<ListView
android:id="#+id/userlistage"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_below="#+id/profilePictureresult"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:divider="#null" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/userlistage"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:text="Activity Name"
android:textColor="#d2f2a2"
android:textSize="23sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="#+id/userlistactivityname"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:alpha="0.7"
android:background="#ADD8E6"
android:text="Confirm"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="serif" />
<TextView
android:id="#+id/userlistname"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:cacheColorHint="#android:color/transparent"
android:clickable="false"
android:divider="#null"
android:drawSelectorOnTop="false"
android:fadingEdge="vertical|none"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:listSelector="#android:color/transparent"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="vertical|none"
android:scrollingCache="false"
android:smoothScrollbar="false"
android:textAlignment="gravity" >
</TextView>
Thanks in advance.
you have used the ID "userlistactivityname" more than once, e.g. you define a textview and give it the id userlistactivityname, but later you also have a button with the same id "userlistactivityname"
<TextView
android:id="#+id/userlistactivityname"
...
android:divider="#null" >
</TextView>
<Button
android:id="#+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="#+id/userlistactivityname"
...
android:typeface="serif" />
and maybe you even have another widget defined as "userlistactivityname".
Search all your layouts for "userlistactivityname" and make sure that the id is unique!
When you want a view to be below / above / leftOf another view, you reference the view using the code
#id/view_id
when you want to add a new id to the Android R file, you use the code
#+id/new_view_id
if you use #+id/ all the time, you can create problems in your R file. Try changing all references in your code to make it cleaner, and only using #+id/ when specifying android:id
<Button
android:id="#+id/btnMatchConfirm"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_below="#id/userlistactivityname"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:alpha="0.7"
android:background="#ADD8E6"
android:text="Confirm"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="serif" />
Strangely, my listView's onClick no longer works after I added two Buttons: Submit and Show Result.
Here is the list view xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".CulturalActivity"
android:background="#EFEFEF"
android:id="#+id/AdLayout"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:divider="#android:color/transparent"
android:descendantFocusability="blocksDescendants"
android:scrollbars="none"
android:dividerHeight="10dp"
android:listSelector="#drawable/list_selector" />
</RelativeLayout>
Here is the layout of each list row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="315dp"
android:clickable="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:background="#drawable/bg_card"
android:orientation="horizontal"
android:padding="5dip" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="-60dp"
android:background="#2093CD"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="52dp"
android:text="Audit"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout2" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Farm/Grp"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignTop="#+id/tile"
android:layout_toRightOf="#+id/textView4" />
<!--thumbnail-->
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/thumbnail_image"
android:layout_marginTop="35dp"
android:gravity="center"
android:id="#+id/tile">
<ImageView
android:id="#+id/thumbImage"
android:layout_width="50dp"
android:clickable="false"
android:layout_height="50dp"
android:src="#drawable/opened" />
</LinearLayout>
<TextView
android:id="#+id/crophead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignBottom="#+id/tile"
android:layout_alignLeft="#+id/title" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Certification"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/certification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/title"
android:text="Test"
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audit Type"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/tile"
android:layout_alignLeft="#+id/textView2"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/audittype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/certification"
android:maxLines="2"
android:text="ES"
android:textColor="#343434"
android:textSize="17sp" />
<View
android:layout_width="fill_parent"
android:layout_height="3px"
android:layout_marginTop="200dp"
android:background="#21265b"
android:id="#+id/view" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/view"
android:layout_alignLeft="#+id/view"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_alignParentBottom="false" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginRight="15dp"
android:layout_alignTop="#+id/textView6"
android:layout_alignRight="#+id/startdate" />
<TextView
android:id="#+id/startdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="30/05/1992"
android:textSize="17sp"
android:layout_alignTop="#+id/enddate"
android:layout_toRightOf="#+id/relativeLayout2"
android:layout_marginLeft="6dp"
android:layout_alignParentBottom="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="2/13/14"
android:textSize="17sp"
android:id="#+id/enddate"
android:layout_below="#+id/textView7"
android:layout_alignRight="#+id/textView7" />
<TextView
android:id="#+id/farm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/audittype"
android:maxLines="3"
android:layout_alignTop="#+id/title"
android:text="Tea Estate Nagarcoil Tamil Nadu, India "
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/crop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/farm"
android:layout_alignTop="#+id/crophead"
android:maxLines="2"
android:text="Wheat without its chaff and barn but not brown"
android:textColor="#343434"
android:textSize="17sp" />
<Button
android:id="#+id/upsync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/blue_thumb"
android:layout_below="#+id/enddate"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Show Results"
android:textColor="#android:color/white" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/upsync"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_below="#+id/enddate"
android:background="#drawable/blue_thumb"
android:text="Submit"
android:textColor="#android:color/white" />
</RelativeLayout>
Here is the click snippet of the list view:
listview.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
long audit_id;
try{
audit_id = (new CustomListAdapter(HomeList.this, c).getAuditID(position));
changeTileStatus(audit_id,"opened");
System.out.println("listClicked");
sendAuditAndIntent(audit_id);
}catch(Exception e)
{
System.out.println("ERROR # HOMELIST list onClick: "+e);
}
}
private void sendAuditAndIntent(long audit_id) { //sends audit id and api key
Intent intent = new Intent(HomeList.this,ChapterActivity.class );
Bundle extras= new Bundle();
extras.putString("audit_id",String.valueOf(audit_id));
extras.putString("api_key", api_key);
intent.putExtras(extras);
startActivity(intent);
}
private void changeTileStatus(long audit_id, String value) {
AuditTableManager tile= new AuditTableManager(HomeList.this);
tile.open();
tile.updateEntry(audit_id, value);
tile.close();
}
});
OnItemClickListener is not called if cells contain clickable Views. It is an Android feature. You can handle clicks by OnClickListener then - set it to your two buttons (not the listView itself).
In your adapter:
public View onCreateView(..., final int position){
...
convertView.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction1(position);
}
});
convertView.findViewById(R.id.button2).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction2(position);
}
});
...
}
Try android:Focusable="false" and also android:clickable="false" in the custom row TextView