listView Centered in a dialog in Android - android

I am trying to have the content of the ListView centered when displayed in a dialog.
This is my code to show the ListView
String[] myItems = {"item1", "item2", "item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.setting_layout, myItems);
ListView list = (ListView) deleteDialog.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setFadingEdgeLength(5);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
// TODO Auto-generated method stub
}
}
And this is the xml part:
TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:textSize="25sp"
and this is the layout of delete dialog:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dividerHeight="5dp"
android:gravity="center">
</ListView>
Its running but the content is always displayed to left.
I tried this: How to set the text at center in ListView android application?
but it didn't work.
Hope anyone can guide me through.

this will work for u
<?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="wrap_content" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dividerHeight="5dp"
android:gravity="center">
</ListView>

You can try with
android:layout_gravity="center_vertical|center_horizontal"
I hope it helps

Change your LinearLayout to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">

Related

android: expand/collapse a recyclerview item

I have a recyclerview list. Each item in the recyclerview contains the following layout. Later when one clicks on the sample1 id, then sample3.xml should get inserted inbetween sample1 and sample2. For that I have created a linearlayout with id insertat in this layout.
<?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="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample1"
android:text="Sample1"/>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/insertat">
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample2"
android:text="Sample2"/>
</LinearLayout>
onclicking id sample1 I want to insert a layout file sample3.xml inside linearlayout id insertat.
sample3.xml is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toinsert">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Sample1"
android:id="#+id/sample3"/>
</LinearLayout>
How can I achieve this in recyclerview code.
I did not get exact requirement but , if you want just visible the view after on click, you can keep your layout by merge both as like below and can visible and invisible based on click event.
<?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="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample1"
android:text="Sample1"/>
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/insertat">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Sample1"
android:id="#+id/sample3"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/sample2"
android:text="Sample2"/>
</LinearLayout>
LinearLayout layout = (LinearLayout)findViewbyId(R.id.insertat);
mKeepPlayingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setVisibility(View.visible);
}
});
First get a reference of the insertat:
LinearLayout insertAtView = (LinearLayout) findViewById(R.id.insertat);
Inflate sample3 view:
View sample3View = getLayoutInflater().inflate(R.layout.sample3, insertAtView, false);
Then add sample3 to insertat:
insertAtView.addView(sample3View);
Full code:
findViewById(R.id.sample1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout insertAtView = (LinearLayout) findViewById(R.id.insertat);
View sample3View = getLayoutInflater().inflate(R.layout.sample3, insertAtView, false);
insertAtView.addView(sample3View);
}
});

Listview inside Listview only one Element

Hi i'm trying to put a listview inside a listview inside a listview.
The only problem is only the first listview is showing all elements correctly.
Every listview after that only contains one element.
UPDATE:
Creating my own non-scrollable listview fixed the problem.
https://stackoverflow.com/a/24629341/3713144
Here is some code:
activity_anlagen_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"></include>
<TextView
android:id="#+id/anlagenName_textview"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center|center_vertical"
android:padding="3dip"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:layout_width="fill_parent"
android:layout_height="4dp"
android:background="#424242" />
<ListView
android:id="#+id/listChecklisten"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
anlagen_checklisten_listview.xml:
<?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="60dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/checkliste_name"
android:layout_gravity="center_horizontal"
android:gravity="center|center_vertical"/>
<ListView
android:id="#+id/listChecklistenPunkte"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and so on...
What i'm trying to do now is setting an ArrayAdapter for all listviews:
AnlagenDetailsActivity:
...
long anlagenId = getIntent().getLongExtra("anlagen_id", 0);
Log.d(logger, "Details for Anlage: " + anlagenId);
LocalAnlagenAccessor accessor = new LocalAnlagenAccessor(this);
Anlage anlage = accessor.readAnlage(anlagenId);
TextView anlagenName = (TextView) findViewById(R.id.anlagenName_textview);
anlagenName.setText(anlage.getName());
ListView listView = (ListView) findViewById(R.id.listChecklisten);
AnlagenChecklistenAdapter adapter = new AnlagenChecklistenAdapter(this);
listView.setAdapter(adapter);
adapter.addAll(anlage.getChecklisten());
...
AnlagenChecklistenAdapter:
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View checklisteView = inflater.inflate(R.layout.anlagen_checklisten_listview, parent, false);
TextView checklisteName = (TextView) checklisteView.findViewById(R.id.checkliste_name);
checklisteName.setText(getItem(position).getArt());
ListView listView = (ListView) checklisteView.findViewById(R.id.listChecklistenPunkte);
AnlagenChecklistenPunktAdapter adapter = new AnlagenChecklistenPunktAdapter(checklisteView.getContext());
listView.setAdapter(adapter);
adapter.addAll(getItem(position).getChecklistenPunkte());
return checklisteView;
}
...
The same goes on for one more listview.
What am I doing wrong here?

sliding menu using jfeinstein10 library

I created a sample application to test how sliding menu works. Shown below in the screenshot is what i get as of now. But when I click on the categories button (shown in image below) I should get a secondary menu as shown in the zomato app's screenshot below. How can I do this ? Am I proceeding in the correct way or not?
my SlidingFragmentActivity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
con = this;
setSlidingActionBarEnabled(false);
setContentView(R.layout.main);
sm = getSlidingMenu();
sm.setMode(SlidingMenu.RIGHT);
sm.setShadowDrawable(R.drawable.shadowright);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
sm.setBehindScrollScale(1.0f);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.5f);
//sm.setSecondaryMenu(R.layout.properties);
//sm.setSecondaryShadowDrawable(R.drawable.shadow);
setTitle("Sliding Bar");
// set the Behind View
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
}
my SampleListFragment :
public class SampleListFragment extends SherlockFragment {
private static final String[] Radio_buttons = new String[] { "Distance",
"Rating" };
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
ListView radio_list = (ListView) view.findViewById(R.id.RadioList);
Button categories = (Button) view.findViewById(R.id.sampleButton);
radio_list
.setAdapter(new ArrayAdapter<String>(MainActivity.con,
android.R.layout.simple_list_item_single_choice,
Radio_buttons));
radio_list.setItemsCanFocus(true);
radio_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
categories.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.sm.showSecondaryMenu();
}
});
return view;
}
}
main.xml
<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="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Sliding menu demo...!!!" />
</RelativeLayout>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SEARCH"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:id="#+id/searchTextLayout"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginBottom="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip" >
<ImageButton
android:id="#+id/searchTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#685E5C"
android:scaleType="fitCenter"
android:src="#drawable/abs__ic_search" />
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/searchTextButton"
android:background="#drawable/background_black_border_full"
android:padding="8dp"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SORT BY"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="134dp" >
<ListView
android:id="#+id/RadioList"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
<Button
android:id="#+id/sampleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Categories" />
</LinearLayout>
</ScrollView>
SlidingMenu does NOT do this, zomato uses a custom implementation.
SlidingMenu will let you have a menu on the left and the right, but not two on either side.
I would look at using a view pager or a custom implementation. EitherWay I don't know of anything out of the box to do this. I might be worth looking at Android Views for inspiration.
It's a bit late, but still let me post my answer in case it will help someone in future. If you want to show another menu you can use
setMode(SlidingMenu.LEFT_RIGHT);
setSecondaryMenu(R.layout.yourSecondMenu);
and in your button click
showSecondaryMenu(true);
and perform your actions in that class.

ListFragment Textview unnecessary lines

ListFragment generates the following view :
I am trying to eliminate the white lines and nothing has helped so far. Any suggestions?
Layout code:
<TextView xmlns:android= " http: //schemas.android.com/apk/res/android"
android:id="#+id/Genere_list"
android:textAppearance="?android:attr/listViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:textColor="#android:color/white" />
The code used to create the view:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.test_album_view, Generes.GENERE_TITLES));
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.test_album_view, Generes.GENERE_TITLES));
}
You need to create an ListView.xml like the following:
<?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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f19000"
android:dividerHeight="1dip" >
</ListView>
</LinearLayout>
What you want to change are these lines:
android:divider="#f19000"
android:dividerHeight="1dip"
You can also use
android:divider="#android:color/transparent"
to make your lines transparent.
As said, you have to use this:
android:divider="#null"
android:dividerHeight="0dp"
In ListView:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp">
</ListView>

Using OnItemClickListener on ListView inside TabActivity

I am having a silly problem with a OnItemCliskListener for my ListView. It works well when the XML is very simple but after I improve its complexity, the OnItemCliskListener stop working
In other words, I have a ListView with just a TextView. But if I turn my ListView into a more complex list with other TextView, an Icon a CheckBox, the ClickListener stop working.
If I run exactly the same code changing just the XML, the OnItemCliskListener stops working. I would be rather grateful if you could help me
WORKING LIST VIEW:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<TextView android:id="#+id/title"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</RelativeLayout>
NOT WORKING LISTVIEW:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/icon" android:id="#+id/appIcon"
android:layout_width="30px" android:layout_height="40px"></ImageView>
<CheckBox android:id="#+id/appCheck"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"></CheckBox>
<TextView android:id="#+id/title"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView android:id="#+id/subtitle"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon" android:layout_below="#+id/title"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
</RelativeLayout>
MY JAVA ACTIVITY:
public class MyTabsActivity extends TabActivity implements OnTabChangeListener {
private static final String LIST3_TAB_TAG = "List3";
private RelativeLayout layoutTab3;
private MyArrayAdapter adapterListViewTab3;
private TabHost tabHost;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
...
...
// ----------------- BUILD TAB3
// That is my Tab of Running App
layoutTab3 = (RelativeLayout) findViewById(R.id.running_app_relative_layout);
// Fill my ListView of Running App
ListView runningAppList = (ListView) findViewById(R.id.running_app_listview);
List<String> list3Strings = new ArrayList<String>();
list3Strings.add("Item 1");
list3Strings.add("Item 2");
list3Strings.add("Item 3");
list3Strings.add("Item
adapterListViewTab3 = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, list3Strings);
runningAppList.setAdapter(adapterListViewTab3);
// ????????????????????????????????????????????????????????????????????????????????????????????
// >>>>>>>>>>>> THIS OnItemClickAdapter DOESN'T WORK !!!!! WHY ????????
runningAppList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MyTabsActivity.this, "EUREKA", Toast.LENGTH_SHORT).show();
}
});
...
// add Tab3
TabSpec tab3 = tabHost.newTabSpec(LIST3_TAB_TAG).setIndicator(LIST3_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return layoutTab3;
}
});
tabHost.addTab(tab3);
layoutTab3.setVisibility(View.INVISIBLE);
...
}
}
MY R.layout.main:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/list1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
<ListView android:id="#+id/list2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
<RelativeLayout android:id="#+id/running_app_relative_layout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:id="#+id/coco" android:layout_alignParentTop="true"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/running_app_listview"
android:scrollbars="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="fill_vertical"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
I've figured out that when a custom ListView contains focusable elements, onListItemClick won't work (I think it's the expected behaviour). So I've just removed the focus from the custom view and it did the trick. In other words, I added the following commands to my view:
android:focusable="false"
android:focusableInTouchMode="false"
The full explanation is avaiable here: How to fire onListItemClick in Listactivity with buttons in list?

Categories

Resources