Textview still visible even if listview gets an item - java

I have an imageview, 2 textviews that needs to appear when the listview is empty. I want the 2nd textview to be clickable
as well. Here is my code -
<ImageView
android:id="#android:id/empty"
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/flower" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="120sp"
android:text="Warning" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="200sp"
android:text="Click Me" />
The image view disappears when the list view gets an item but the text views are still visible.
How can I make these 2 textviews disappear when the list view gets an item? Also how can I make my second text view clickable?

You can easily make them invisible. In your activity do this when you add an item to the list:
TextView tvXY = (TextView) findViewById(R.id.[TheIdHere]);
tvXY.setVisibility(View.GONE);
In your xml file you will have to change the id of the textviews too. Choose different ids for both of them. You can change the id by editing this:
#android:id/empty
To something like this.
#+id/[TheIdHere]

To make textView clickable you can setOnClickListener to that textView like this:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Related

How does setOnFocusChangeListener work in Android?

I have designed a simple Android Application to test how setOnFocusChangeListener works.
The Java Code:
public class MainActivity extends AppCompatActivity {
TextView tv;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.textView);
et = findViewById(R.id.edittext);
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
tv.setText("Edit Text is focused!!");
}
else {
tv.setText("You stopped focussing on editText");
}
}
});
}
}
The XML Code:
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nothing happened"
android:textSize="30sp"
android:gravity="center"
android:id="#+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Write here!!!"
android:id="#+id/edittext" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me!!" />
</LinearLayout>
But when I click on the edit text to write something, Edit Text is focused!! and displayed on the text view.
After stopping the focus, the EditText is never displayed on screen, even when I click anywhere else on the screen or on a button. I would like to know when the else part of onFocusChange will be displayed?
I guess it is cause the editText is never leaving the focus.
On the GUI editor or XML, there is a property named "focusOnTouch" which editText Views have on true by default that let you get the focus for the editText, but if there is no other view with that property on true too, no view could get the focus.
That property exists for every View, set it to true for each View you want to consider to get the focus when clicking, otherwise, the focus won't be passed to other views like your textView or button.

Android button not clickable when turning visible

Hi Im posting this after not finding any possible answer here.
I have an invisible LinearLayout LL1 in a fragment that I turn visible when a recylerView data is empty, that LL1 has a button on it. The problem is simply that the button is not clickable ! I tried setting the listener in different ways but still not working. Here's the code below:
Here's the xml file:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.main.MainActivity"
android:layout_marginTop="3dp">
<LinearLayout
android:id="#+id/msg_emty_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_empty_box" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aucune vente n'a été trouvée !"
android:textSize="15dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/greydark"/>
<Button
android:id="#+id/btnAddSale"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Créer une vente"
android:textColor="#color/whiteTextColor"
android:background="#drawable/centre_button">
</Button>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_db"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_sale_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add_db"
android:layout_margin="20dp"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorAccesGreen"
android:src="#drawable/ic_edit"/>
</android.support.design.widget.CoordinatorLayout>
And here's the java:
private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
if(saleArrayList.isEmpty()){
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}else{
msgLayout.setVisibility(View.GONE);
recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView_db.setLayoutManager(layoutManager);
recyclerView_db.setAdapter(adapter_db);
adapter_db.notifyDataSetChanged();
}
}
Do you guys see what's causing this weird problem ?
You can try with requestFocusFromTouch()
Call this to try to give focus to a specific view or to one of its
descendants.
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.requestFocusFromTouch();
Hide your RecyclerView when there's no items (set visibility to gone). The problem is that it's capturing the clicks, since it's placed on top of the button.
as u said in comments you not using creatSaleBtn.setClickable(false); anywhere, then you have to hide the recyclerView using recyclerView_db.setVisibility(View.GONE);
why? because it by default fits the full-screen due too match_parent for width and height.
please add recyclerView_db.setVisibility(View.GONE); in the saleArrayList.isEmpty() check
so your new code will be :
if(saleArrayList.isEmpty()){
recyclerView_db.setVisibility(View.GONE);
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}
SwipeRefreshLayout's visibility should be GONE after recyclerview is empty.
of coarse recyclcerview is empty but it is still visible so your click events are caught by SwipeRefreshLayout witch is defined on top of LL1.
EDIT
instead of recyclerview, set swipe_refresh_db visibility to gone cos I doubt recyclerview is a child of swipe_refresh_dd so if you set recyclerview's visibility to gone, swipe_refresh_db is still visible and gets click events.
give it a try

Automatically add EditText below when the previous has been filled

I have a xml file: row.xml which contains a EditText (et) and a Spinner.
The activity starts, I "inflate" row.xml in a View and I set a listener that - when et changes, a row.xml is automatically inflated.My code is like below.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/spinnerDay"
android:id="#+id/nameUser"
android:theme="#style/myEditText"
android:layout_marginRight="18dp"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="38dp"
android:id="#+id/spinnerDay"
android:layout_alignParentRight="true"
android:popupBackground="#FFFFFFFF"
android:background="#drawable/selector_spinner">
</Spinner>
</RelativeLayout>
My problem is to set a listener for the new row in order to repeat the trick.
LinearLayout v = new LinearLayout(context);
v.addView(rowView);
View rowView = inflater.inflate(R.layout.row, null);
((LinearLayout) rootView.findViewById(R.id.userList)).addView(v);
R.id.userList is the View where I inflate row.xml
Listener for EditText
((EditText) rootView.findViewById(R.id.nameUser)).setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
LinearLayout v = new LinearLayout(view.getContext());
v.addView(rowView);
((LinearLayout) rootView.findViewById(R.id.userList)).addView(v);
return false;
}
});
I am not an expert programmer so I do ask: what is the best way to a product such an effect and retry the data inserted?
Everything is made more tricky because I am working in Fragments in a FragmentActivity and I am supposed to get the data in the last one, before confirm...how can I save the et's content?
Any help will be appreciated, thank you!

Horizontal scroll and focus on the last number in a TextView

I am doing a calculator app.
I have a TextView where the user writes the numbers to elaborate.
The TextView is inside a HorizontalScrollView. The HorizontalScrollView is inside a vertical LinearLayout. The TextView is single line, and, when the user writes a long number, the number comes out of the screen.
The TextView should follow the last number added by the user, so he can view the last numbers added without scrolling the screen. I don't want an autoscroll function that repeat the scroll every second, i only want focus on the last number added.
This is the TextView
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/numberDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:hint="0"
android:scrollHorizontally="true"
android:maxLines="1"
android:layout_gravity="right"
android:textSize="70sp" />
</HorizontalScrollView>
Try this:
In your activity create variables for your scroll view and text view:
TextView textView;
HorizontalScrollView scrollView;
Initialize them in onCreate():
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView); // Add this id to xml
textView = (TextView) findViewById(R.id.numberDisplay);
Each time you update text of your text view update scroll position of the scroll view:
scrollView.scrollTo(textView.getRight(), textView.getTop());
Update:
I realized that if you call textView.setText(...) and then immediately scrollView.scrollTo(...) it does not work since text view size is not updated immediately. You should rather update scoll position like this:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(textView.getRight(), textView.getTop());
}
});

ListView with Load More Button and Circle ProgressBar

I spent an hour trying to add "Load More" Button and indeterminate ProgressBar to the footer of my ListView.
The supposed scinario works like this:
When the button is clicked, ProgressBar is shown while AsyncTask is downloading 20 items. when the items are inserted to the ListView, the ProgressBar dismisses and the Button appears again in the footer.
I come to the following solution and would be nice if you have better solution:
layout/progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
/>
And assuming you have the following fields in the Activity:
private ListView listview;
private Button loadMoreButton;
private ProgressBar progressBar;
after preparing your list view add the footer like this, (at the end of the activity.onCreate()):
loadMoreButton = new Button(this);
loadMoreButton.setText("Load More");
progressBar = (ProgressBar) LayoutInflater.from(this).inflate(R.layout.progress_bar, null);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//downloadItems();
listview.removeFooterView(loadMoreButton);
listview.addFooterView(progressBar);
}
});
When the data is ready (either first page or subsequent pages), call the following after adapter update.
//adapter.notifyDataSetChanged();
listview.removeFooterView(progressBar);
listview.addFooterView(loadMoreButton);
If you have better solutions, please share it in the answers.
Instead of removing and adding a view as footer.. you can have both button and progressbar in the same footer view and make visibility VISIBLE and Visibility GONE according to the requirement.
I came to a solution without adding/removing the footer. Just put the two views (Load More Button and ProgressBar) in LinearLayout. Add the linearlayout as listview footer for first time only. Then change between the button and progressbar by changing visibility property.
Here is the updated code:
layout/list_footer.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="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/load_more_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Load More"/>
<ProgressBar android:id="#+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminate="true" />
</LinearLayout>
call this method at the end of Activity.onCreate() to add footer and handle button click (load data, hide button and show progress).
private void prepareListFooter() {
View view = (View) LayoutInflater.from(this).inflate(R.layout.list_footer, null);
loadMoreButton = (Button) view.findViewById(R.id.load_more_button);
progressBar = (ProgressBar) view.findViewById(R.id.load_progress);
listview.addFooterView(view);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//downloadMore();
loadMoreButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
});
}
also this code after adapter change to show the button and hide the progress.
adapter.notifyDataSetChanged();
loadMoreButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);

Categories

Resources