Android setVisibility(View.Visible) not working on a layout - java

I have a layout that I am trying to make visible and it is currently not working. The layout I want to make visible has the id "goal_reminder" below. The visibility is set to "GONE" in the xml.
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<com.View.pages.ActivityPage
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/activity_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/activitylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e6ee"
android:clipToPadding="false"
android:divider="#null"
android:paddingBottom="80dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:id="#+id/goal_reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent_color"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:layout_width="match_parent"
android:layout_height="145dp"
android:src="#drawable/sunsetforgoal" />
<ImageView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/goal_reminder_title"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="35dp"
android:src="#drawable/logo" />
<TextView
android:id="#+id/goal_reminder_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="No goal in progress"
android:textColor="#color/text_color"
android:textSize="26sp" />
<TextView
android:id="#+id/goal_reminder_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/goal_reminder_title"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Create a new goal in your \nprofile."
android:textColor="#color/text_color"
android:textSize="18sp" />
</RelativeLayout>
<TextView
android:id="#+id/playground_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:autoLink="all"
android:gravity="center_horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="#string/welcome_string"
android:textSize="18sp"
android:visibility="gone" />
</com.View.pages.ActivityPage>
Here is the onFinishInflate(I am using the Screenplay/Flow libraries so that is taking place of onCreate). As you can see I am trying to set goalReminderLayout.setVisibility(View.Visible) and it's not actually making it visible. I have tested this same line of code outside of the if statements and it works just fine. I have also tested to make sure it's reaching that line of code in the if statements and that part is working fine, the lastTriggerDate is being saved properly in Parse. I am lost on why it's working fine outside of the if statements in the onFinishInflate. I also tested with a Log.d("TestVisiblity", goalReminderLayout.getVisibility()); which returns a 0(Visible) so it seems like its visible but its not actually showing up on my screen.
Date lastTriggerDate = new Date();
Boolean noDateInParse = false;
if (currentUser.getLastTriggerDate() != null) {
lastTriggerDate = currentUser.getLastTriggerDate();
} else {
noDateInParse = true;
}
Boolean inToday = DateUtils.isToday(lastTriggerDate.getTime());
if (!inToday) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
} else if (noDateInParse) {
currentUser.setLastTriggerDate(currentTime);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
goalReminderLayout.setVisibility(View.VISIBLE);
fireGoalReminderAlert();
} else {
e.printStackTrace();
}
}
});
}
Here is the code for fireGoalReminderAlert(); It's just set to set the visibility back to gone after 10 seconds. I commented this line out when testing and still had no luck so I don't think this is causing the problem.
public void fireGoalReminderAlert() {
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
goalReminderLayout.setVisibility(View.GONE);
}
};
mHandler.postDelayed(mRunnable, 10 * 1000);
}

When you set a layout to visible from gone it's always a good idea to invalidate view to allow a redraw on the layout.
goalReminderLayout.setVisibility(View.VISIBLE);
goalReminderLayout.invalidate();
look at the android documentation for more info
http://developer.android.com/reference/android/view/View.html

Try this
Handler(Looper.getMainLooper()).post {
goalReminderLayout.setVisibility(View.VISIBLE);
}

Related

Android studio automatic horizontalscrollview infinite scrolling till end then repeat

Im using android studio , i have xml which have horizontalscrollview
Then linearlayout then multiple imageviews ..
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
What i need is to make this horizantalscrollview to keep automatically infinite scrolling the images till the end then repeat the scrolling again and again.
what i mean by auto scrolling that the horizontalscrollview keep scrolling without using hand
Did you try to use wrap_content in layout_width?
Now, u adapt your width layout to the width of the screen, i think that if u put the value wrap_content it will solve ur problem (u have layout_height=wrap_content, but the height is "asociated" with a Vertical scroll, not with horizontal scroll):
android:layout_width="wrap_content"
I hope this help u.
ok Thanks for your support .. i found the solution
Timer timer1 = new Timer("horizontalScrollViewTimer");
timer1.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (scrollingLeft) {
if (scroll.getScrollX() == 0) {
scroll.smoothScrollBy(5, 0);
scrollingLeft = false;
} else {
scroll.smoothScrollBy(-5, 0);
}
} else {
if (scroll.canScrollHorizontally(View.FOCUS_RIGHT)) {
scroll.smoothScrollBy(5, 0);
} else {
scroll.smoothScrollBy(-5, 0);
scrollingLeft = true;
}
}
}
});
}
}, 1000, 50);

How to dynamically add suggestions (views) and be able to control the textView in it

I have a fragment in my app that is a basic chatbot. For that chatbot, I want to be able to serve suggestions of replies the user can reply.
I have layout of the reply I want in an xml file.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:layout_gravity="end"
android:padding="4dp"
app:strokeColor="#color/suggestion_border"
app:strokeWidth="2dp"
app:cardElevation="0dp"
app:cardCornerRadius="8dp">
<TextView
android:id="#+id/chat_suggestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="18dp"
android:textColor="#color/chat_text_color"
android:text="Suggestion"/>
</com.google.android.material.card.MaterialCardView>
While produces something like this
and I have a incoming_msg.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/chat_message"
// irrelevent
android:textSize="18sp" />
<TextView
android:id="#+id/chat_time"
// irrelevent
android:text="12:90 PM"
android:textColor="#b4b2b2"
android:textSize="12sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/chat_time">
<LinearLayout
android:id="#+id/suggestion_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</ScrollView>
And because the conversation fragment is a recycler view. I have a onBindViewHolder
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
switch (holder.getItemViewType()) {
case 1:
IncomingMsg iM = (IncomingMsg) holder;
iM.msg.setText(msg.get(position).getMessage());
iM.dt.setText(df.format(msg.get(position).getTime()));
if (!msg.get(position).getSuggest().isEmpty()) {
if (iM.s.getChildCount() != msg.get(position).getSuggest().size()) {
for (Suggestion tempSuggestion: msg.get(position).getSuggest()) {
if (tempSuggestion.isWithImage()) {
// TODO
} else {
iM.s.addView(getLayoutInflater().inflate(R.layout.outgoing_suggestion, iM.p));
}
}
}
}
break;
}
}
And at this point, I do not know how to continue, as I couldn't be able to scroll the suggestion list sideway and I couldn't control the text shown in the suggestion.

List page RecyclerView infinite scrolling not working when putted in NestedScrollView

I have taken two recyclerviews as I have to show two lists in which one is horizontal list and another is vertical with infinite scroll, for scrolling of both the lists I have added them in nestedScrollView but infinite scroll of vertical list is not working, there is an pagination logic after first loading of items when I scroll down then api gets called and then it loads items in list
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/llRecentlyBought"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/txtVRecentlyBought"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_grey_color"
android:padding="10dp"
android:text="#string/recently_bought"
android:textSize="#dimen/font_s_ize_levelone_signup" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerRecentlyBought"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:paddingStart="#dimen/padding_home_1"
android:paddingEnd="#dimen/padding_home_1"
android:scrollbars="none"
android:focusable="false"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/products_in_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/llRecentlyBought"
android:layout_alignParentStart="true"
android:layout_gravity="top"
android:layout_marginTop="5dp"
android:background="#android:color/white"
android:descendantFocusability="blocksDescendants"
android:fadingEdge="none"
android:paddingStart="#dimen/padding_home_1"
android:paddingEnd="#dimen/padding_home_1"
android:paddingBottom="#dimen/padding_home_1"
android:scrollbars="none"
android:focusable="false"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
BaseAttacher mBaseAttacher = Mugen.with(productsInGridRecyclerView, new MugenCallbacks() {
#Override
public void onLoadMore() {
if (indexOfCurrentPage < TOTAL_PAGE_AVAILABLE) {
indexOfCurrentPage += 1;
// loadNextPage(indexOfCurrentPage);
if (isLastPage == false) {
loadNextPage(indexOfCurrentPage);
} else if (isLastPage == true) {
// Toast.makeText(getActivity(), "No more products", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), getResources().getString(R.string.txt_no_product_found), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public boolean isLoading() {
return isLoading;
}
#Override
public boolean hasLoadedAllItems() {
return false;
}
}).
start();
mBaseAttacher.setLoadMoreEnabled(true);
mBaseAttacher.setLoadMoreOffset(2);
above is the baseattacher to vertical list which is not getting called when I scroll down, without nestedscrollview its working help me with infinite scrolling of list in nestedscrollview
libary used https://github.com/vinaysshenoy/mugen
no need of nestedscrollview
i have just added app:layout_behavior="#string/appbar_scrolling_view_behavior" to parent layout
it works !!

Reset results and TextUtil

So, I got a feedback from reviewer and fixed most of my problems but there are two problems that I can’t find solution (I will use bold font to highlight them) the first problem comes after I click result button more than one time because the previous result is added. I know that I should add all the methods checkQuestionOne to checkQuestion6 in the button method but this is not possible so far because the method needs (View or view value) which is not working and I don’t know why so I decided not to include the four Radio buttons methods in it. Second when the checkQuestionFive is blank or there is no value the app crashes I was told to use if (!TextUtils.isEmpty(gettingQuestionFive.getText())), then I add Toast message into the if statement but it still crashes. Any help will be appreciated!!
Thank you!
Java code:
public class MainActivity extends AppCompatActivity {
int health = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkQuestionOne(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_one_yes:
if (checked)
health += 1;
break;
}
}
public void checkQuestionTwo(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_two_yes:
if (checked)
health ++;
break;
}
}
public void checkQuestionThree(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_three_no:
if (checked)
health ++;
break;
}
}
public void checkQuestionFour(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_four_no:
if (checked)
health ++;
break;
}
}
**public void checkQuestionFive**() {
EditText gettingQuestionFive = findViewById(R.id.sleep_hours);
if (!TextUtils.isEmpty(gettingQuestionFive.getText())){
Toast displayError = Toast.makeText(this, "You missed question 5, plase don't leave it blank", Toast.LENGTH_LONG);
displayError.show();
}
int answerQuestionFive = Integer.parseInt(gettingQuestionFive.getText().toString());
if (answerQuestionFive >= 7) {
health ++;
}
}
public void checkQuestionSix() {
CheckBox checkBoxOneA = findViewById(R.id.fruits_check_box);
CheckBox checkBoxOneB = findViewById(R.id.chips_check_box);
CheckBox checkBoxOneC = findViewById(R.id.candy_check_box);
CheckBox checkBoxOneD = findViewById(R.id.all_three_check_box);
if (checkBoxOneA.isChecked() && !checkBoxOneB.isChecked() && !checkBoxOneC.isChecked() && !checkBoxOneD.isChecked()) {
health++;
}
}
**public void overView(View view)**{
checkQuestionFive();
checkQuestionSix();
Toast displayToast = Toast.makeText(this, health + " is your score. If is 4 or above, you have a healthy life", Toast.LENGTH_LONG);
displayToast.show();
health = 0;
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="com.example.martinevtimov.quizapp2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1. Do you eat healty?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_one_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionOne" />
<RadioButton
android:id="#+id/question_one_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionOne" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group2" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2. Do you work out?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_two_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionTwo" />
<RadioButton
android:id="#+id/question_two_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionTwo" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group3" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3. Do you drink?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_three_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionThree" />
<RadioButton
android:id="#+id/question_three_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionThree" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/group4" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4. Do you smoke?"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="#+id/question_four_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"
android:onClick="checkQuestionFour" />
<RadioButton
android:id="#+id/question_four_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No"
android:onClick="checkQuestionFour" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5. How many hours do you sleep?"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="#+id/sleep_hours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:inputType="number" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6. What is a healthy snack?"
android:textSize="30sp"
android:textStyle="bold" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fruits"
android:id="#+id/fruits_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chips"
android:id="#+id/chips_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Candy"
android:id="#+id/candy_check_box" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All three"
android:id="#+id/all_three_check_box" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="overView"
android:text="Check my health"
android:textAllCaps="true" />
</LinearLayout>
</ScrollView>
You can see the Logcat and the message error I have. The purpose of this app is to make a quiz app with 3 different inputs (EditText, Radio Buttons, and CheckBoxes). When the EditText is left blank, a crash appears. It seems there is some problem with the onClick attribute in your code but I can figure it out. The second problem comes from clicking the check button more than one time than more points are added to the previous points, I tried to add all the question methods into one and then set the global health variable to 0 but is not really working. Also, take a look of the two pictures below my code.
Screen Picture 1
Screen Picture 2

Flipping to next screen shows black color in background

In vertical flip effect,when flip to next screen the background shows black color it happens only in GT_N7100 mobile...How to fix this problem???can anyone please help me...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical"
>
<ImageView
android:id="#+id/bar"
android:layout_width="480dp"
android:layout_height="1dp"
android:layout_marginTop="36dp"
android:scaleType="fitXY"
android:src="#drawable/line" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bar"
android:layout_margin="5dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
/>
<ImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="330dp"
android:layout_below="#+id/title"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop" />
If you are performing any heavy task on main thread in onCreate() method, that might block the onCreate() UI; to avoid this issue you can run a separate thread for this task.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splace_layout);
mContext = this;
Thread th = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
mTask();
} catch (Exception e) {
// TODO: handle exception
}
}
});
th.start();
}

Categories