two textviews listview android - android

I have implemented search functionality in my listview which is in my navigation drawer. But currently it has only one textview. I want to be able to add two textviews on the same line without the need of a different class or adapter. Having said that, I still want to be able to search the first textview. So basically my question is : How do I add two textviews to a row in a listview without using any other class?
Here's my code:
listitem_row.xml
<!-- This is the xml which i use to display one textview on a list row-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
</LinearLayout>
main_activity.xml
<!--This is my main activity xml where I have the navigation drawer and the
listview inside the drawer-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:drawableLeft="#drawable/search"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint="Search" >
</EditText>
<RelativeLayout
android:id="#+id/empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<TextView
android:id="#+id/empty_text"
android:text="#string/notfound"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#ffababab"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center">
</TextView>
</RelativeLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
//This is my mainactivity
public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};
//I have already added the String elename to my listview
//and I want to add String nos to it without using any other class.
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The line below adds the string elename to the listview
//but using the same line i want to add the string nos if possible.
adapter = new ArrayAdapter<String>(this, R.layout.listitem_row,R.id.textView1, elename);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
//Change view according to numbers
drawer.closeDrawer(linearLayout);
}
});
//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
//The line below removes all the spaces while searching
String searchedquery = cs.toString().replaceAll(" ","");
//The line below searches the listview and shows the results
MainActivity.this.adapter.getFilter().filter(searchedquery);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//The code below hides the keyboard when the user touches the listview
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
navList.setEmptyView(findViewById(R.id.empty));
}
}

For binding two TextViews you need to create Custom Array Adapter. Go to below links for better understanding:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
http://prativas.wordpress.com/category/android/custom-listview-in-android-using-custom-arrayadapter/
http://jmsliu.com/1390/rss-reader-app-android-tutorial-1-listview-and-arrayadapter.html
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Update:
ListView searching Functionality:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
http://www.myandroidsolutions.com/2013/08/04/android-listview-with-searchview/
http://sunil-android.blogspot.in/2013/09/custom-listview-alertdialog-with-filter.html
http://android-solution-sample.blogspot.in/2011/10/android-search-in-custom-listview.html
Here are the ideas. Go to any links and customized as per your requirement.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>

Related

How can I adjust the size deppending on the visible content in an android view?

I'm creating a list view in an android app and each item expands and collapses.
Here is each item's code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/alarme_layout"
android:backgroundTint="#color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="#+id/textView_aHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="00:00"
android:textSize="26sp"
android:textColor="#color/colorOnPrimary"
android:layout_marginBottom="10dp"/>
<Switch
android:id="#+id/switch_aEstado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="#+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="25dp">
</LinearLayout>
<ImageView
android:id="#+id/imageView_arrowDown"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_detalhesExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora">
<CheckBox
android:id="#+id/checkbox_repetir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/repetir"
android:textColor="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"/>
<LinearLayout
android:id="#+id/layout_diasRepetirButtons"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_repetir"
android:layout_alignStart="#+id/checkbox_repetir"
android:layout_marginTop="10dp"
android:visibility="invisible">
</LinearLayout>
<LinearLayout
android:id="#+id/ultimaLinhaExpanded"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/editText_mensagem"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="wrap_content"
android:hint="#string/mensagemHint"
android:inputType="text"
android:backgroundTint="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"
android:textColor="#color/colorSecondary"
android:shadowColor="#color/colorSecondary"
android:textColorHighlight="#color/colorSecondaryLight" />
<ImageView
android:id="#+id/imageView_arrowUp"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_up_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
And here is the "getView()" method of list view:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.alarme, null);
final Alarme alarme = alarmes.get(position);
//main view
TextView hora = convertView.findViewById(R.id.textView_aHora);
Switch estado = convertView.findViewById(R.id.switch_aEstado);
LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir);
final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown);
//detalhes
final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded);
CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir);
LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons);
EditText editText = convertView.findViewById(R.id.editText_mensagem);
final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
hora.setText(alarme.getHora());
estado.setChecked(alarme.isLigado());
cbRepetir.setChecked(alarme.isRepete());
arrowDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowDown.setVisibility(View.INVISIBLE);
arrowUp.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.VISIBLE);
alarme.setCollapsed(false);
}
});
arrowUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowUp.setVisibility(View.INVISIBLE);
arrowDown.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
alarme.setCollapsed(true);
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
alarme.setMensagem(s.toString());
}
});
arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE);
arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
editText.setText(alarme.getMensagem());
return convertView;
}
I thought each item would have its size wrapped depending on the visible content, but I have this result instead:
This is the item when expanded:
https://imgur.com/gallery/oIv50dT
This is the item when collapsed:
https://imgur.com/gallery/dmzw5GU
I wanted the collapsed item to have its size wrapped to the content.
I am going to guess here that in your XML layout you have set a static height.
You need to set it to be wrap_content which allows for dynamic row heights.
I solved it by changing the visibility of the views from View.INVISIBLE to View.GONE.

Touch not working on list view

I am using touch event on LinearLayout in which there is a list view(Its a child). When I touch on list view part touch event does not gets called. below is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFavourite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Loading"
android:indeterminate="true"
android:progressDrawable="#android:drawable/progress_indeterminate_horizontal"
android:visibility="gone" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:focusable="true">
</ListView>
</LinearLayout>
My animation:
final Animation slidedown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down_half);
llFavourite.setOnTouchListener(new OnSwipeTouchListener(MidnightMainActivity.this) {
#Override
public void onSwipeBottom() {
llFavourite.startAnimation(slidedown);
inflateFavouriteLayout(MODE_REFRESH);
}
});
but when I click on layout part event gets called. Is there any property which i should enable or disable?
Thanks in advance.
android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton.
In your layout xml, add this property to root layout
android:descendantFocusability="blocksDescendants"
from your calling class add
ListView lView = (ListView) findViewById(R.id.list);
lView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(position);
}
});

Can we add multiple listviews in a single layout

I have Three list to display.How to add them on single layout?
my layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
I am using CustomAdapter for each list
mylistview1 = (ListView) findViewById(R.id.list1);
mylistview2 = (ListView) findViewById(R.id.list2);
mylistview3 = (ListView) findViewById(R.id.list3);
//1
CustomAdapterA adapterA = new CustomAdapterA(this, rowItems1);
mylistview1.setAdapter(adapterA);
mylistview1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems1.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
//2
CustomAdapterM adapterM = new CustomAdapterM(this, rowItems2);
mylistview2.setAdapter(adapterM);
mylistview2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems2.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
//3
CustomAdapterS adapterS = new CustomAdapterS(this, rowItems3);
mylistview3.setAdapter(adapterS);
mylistview3.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems3.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
I tried with putting each layout in separate Linear Layout but didnot worked,
can anyone suggest what should be done here???
Try making making each of the ListViews take up exactly 1/3 of the screens height and see if they're visible then.
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>
Initially a listview looks like it occupies the whole screen. You have to set their widths accordingly to see them in one screen.
If your idea is to finish one list the next one will be visible the answer is:
No, you cannot do that. On your adapter you should Override getViewTypeCount() and return 3 and then generate the correct view for each position.
If you're idea is to have each list occupying 1/3 of the screen you must use the layout_weight parameter in your XML like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

ListView item not found

My app is using landscape full screen mode and the navigation drawer. I am using listView in my app along with an edittext. The edittext is the search bar that will search the listview. Both the listview and the edittext are in the navigation drawer. But, when there is no list item that matches the searched word, the listview gets empty.
So how can I add a "Item not found" message instead of the blank listview?
I searched a lot on the internet and found a method setEmptyView(); but I couldn't understand it and hence it is not working. Please help me! Maybe this question is already asked here but please give me an easy explanation.
Here is my code:
MainActivity.java
public class MainActivity extends FragmentActivity {
final String[] data = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
//the code below will automatically close the keyboard when the user will touch the listview
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
}
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="240dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="230dp"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint=" search" >
</EditText>
<TextView
android:id="#+id/notfound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium"
android:gravity="center">
</TextView>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Yes you have mentioned about setEmptyView() correctly, you should use it if you would want to show empty message whenever ListView gets empty.
Now here is a xml layout and code depicts how to use setEmptyView exactly.
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layoutTitlebar" >
<ListView
android:id="#+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000" >
</ListView>
<TextView
android:id="#+id/empty"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/strNoRecordsFound" >
</TextView>
</LinearLayout>
Now, you have to set this empty view (i.e. TextView) to ListView by using:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
// set your adapter here
// set your click listener here
// or whatever else
listViewFriends.setEmptyView(findViewById(R.id.empty));
This method is all that you need
http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)
Have in mind it accepts a view, so you have to inflate the layout and fill out any/all text you may have there by yourself.
EDIT:
Let's assume you have a layout named "empty_text" like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There are no entries in this list"
android:gravity="center"/>
</LinearLayout>
Hint: I burned in the string for example purposes, heed the IDE warning and use a string identifier for I18n's sake
Now you would use this code to make it all work with the ListView:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyTextView = inflater.inflate(R.layout.empty_text, null, false);
listView.setEmptyView(emptyTextView);
This code assumes you are executing inside an Activity, but just in case you don't plan on pasting it on an Activity, any Context instance will work.
That should be it.
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
Then your listview will automatically use this view when its adapter is empty.
Detailed Solution:
Layout:
<ListView
android:id="#+id/listViewFriends"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000">
</ListView>
<TextView
android:id="#+id/empty"
android:text="#string/strNoRecordsFound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Large"
android:gravity="center">
</TextView>
</LinearLayout>
Class File:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
listViewFriends.setEmptyView(findViewById(R.id.empty));
Source

Sliding Drawer, Handle position and width of the drawer.

I recently stumbled upon the sliding drawer in Android, after creating my first project which implements the same I have a few issues. I am using a list view inside of the sliding drawer and for that I need to implement onClickListener and extend Activity, now everything is working fine, albeit the sliding drawer opens and occupies almost all of the space in the view, I want it to open only half screen. I went through various posts on internet which ask me to extend SlidingDrawer and use method onMeasure which is not feasible for me (Or is it?). Secondly I would want to place the handle button on the top not centre. Below is my code:
activity_main.xml~~~>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top"
>
<TextView
android:text=""
android:gravity="center|center_vertical"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<SlidingDrawer
android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideButton"
android:content="#+id/contentLayout"
android:animateOnClick="true"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideButton"
>
</Button>
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/contentLayout"
android:orientation="vertical"
android:gravity="center"
android:padding="10dip"
android:background="#0080FF"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
.java file~~~~>
public class slidingDrawerDemo extends ListActivity implements OnClickListener {
private ArrayAdapter<String> listAdapter ;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
ListView LV = getListView(); //(ListView)findViewById(R.id.list);
String []Test ={"Residential","Commercial","Customised Projects","SEZ","Brochure"};
ArrayList<String> Plist = new ArrayList<String>();
Plist.addAll(Arrays.asList(Test));
listAdapter = new ArrayAdapter<String>(this, R.layout.list_dem, Plist);
LV.setAdapter( listAdapter );
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView LV, View view,int position, long id) {
Toast.makeText(slidingDrawerDemo.this,""+position, Toast.LENGTH_SHORT).show();
if(position==0){
initOne();
}else if (position==1){
initTwo();
}else if (position==2){
initThree();
}else if (position==3){
initFour();
}else if (position==4){
initFive();
}
}
});
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
// slideButton.setBackgroundResource(R.drawable.closearrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
// slideButton.setBackgroundResource(R.drawable.openarrow);
}
});
}
My list_dem.xml file~~~>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
I found the answer:
in my Slider defination in xml:
android:topOffset="100.5dp"

Categories

Resources