I have a custom dialog with the format of Header + listview + footer. The corresponding code is as follows: (I don't mind to share the code but it is over 800 lines) ...
/* Setup dialog layout */
View header = (View)getLayoutInflater().inflate(R.layout.stockcountheader, null);
View footer = (View)getLayoutInflater().inflate(R.layout.stockcountfooter, null);
final Dialog listDialog;
listDialog = new Dialog(this, android.R.style.Theme_Dialog);
listDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.dialog, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
final AdditemAdapter adapter = new AdditemAdapter(this, R.layout.additemrow, updateitems);
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
filterrow.addHeaderView(header);
filterrow.addFooterView(footer);
filterrow.setAdapter(adapter);
i = adapter.getCount();
listDialog.show();
In the header, I have a radiobutton and intend to control the visibility of the listview row. So, I have the following code: ...
radiostockresGroup = (RadioGroup)listDialog.findViewById(R.id.radiostockres);
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(radiostockresGroup.getCheckedRadioButtonId());
radiostockresGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radiostockresGroup, int checkedId) {
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(checkedId);
switch(checkedId) {
case R.id.radioresno:
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
break;
case R.id.radioresyes:
listDialog.findViewById(R.layout.tblayout2).setVisibility(View.VISIBLE);
break;
}
}
});
However, it give me the java null pointer error in the "listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE); " line. The xml for the listview and the dialog is as follows:
stocktakerow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="#+id/tblayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
<EditText android:id="#+id/txtinput"
android:layout_toRightOf="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
</TableRow>
</TableLayout>
</RelativeLayout>
I try to use " filterrow.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);" and still got the same null pointer.
Please kindly give me a hand, thanks a lot in advance!
Kelvin
If you look at your code, you will notice:
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
Essentially, you are saying that R.id.tblayout2 is a member of the same thing as R.id.dialoglist. I don't think they are in this case.
Not seeing what your class extends, I can only suppose that this should work. If it doesn't, can you please post the class definitions? Just the line like public class MainActivity extends Activity { will do for both classes.
findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
Related
I'm trying to write a function that opens a popup window, and populates it with button links to all of the previous pictures or recorded audio files the app has recorded. I tried to write the function so that it will work for audio files or images. The goal is to have a pop-up window appear on the screen which has a button for each of the previous files and will open the designated file on click. The app will successfully open the popup window, but it will only be populated by the auto-defined button (back) which will close the popup window. However, white space appears underneath the back button, which increases the height of the scroll view and allows for scrolling within the pop-up window but no buttons actually appear.
Initially, my problems came from not including pw.getContentView() (pw is my popupwindow) for each of my layout elements, but I'm not sure what it does exactly so that may be a part of the problem.
This is the function that is called to open the popup window.
private void display_media(int check, String header_text){
//The check variable is a 1 or 0 depending on if it's an image or an audio.
// header_text is a simply text to replace the default header.
try{
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, (ViewGroup) findViewById(R.id.show_media));
pw = new PopupWindow(layout,width/4,height/3,true);//width and height are predefined values
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
LinearLayout l_l = (LinearLayout) pw.getContentView().findViewById(R.id.scroll_linear);
for(String media_file: files){ // this is defined and explained below
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button new_button = new Button(this);
if(check==0){
new_button.setId(image_id_count);
image_id_count++; // this is initialized to 3000
button_id = new_button.getId();
}
else{
new_button.setId(audio_id_count);
audio_id_count++; // this is initialized to 2000
button_id = new_button.getId();
}
new_button.setText(media_file);
l_l.addView(new_button,params);
if(check==0)
button_two= (Button) pw.getContentView().findViewById(button_id);
else
button_two = (Button) pw.getContentView().findViewById(button_id);
button_two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
pw.dismiss();
// this will be replaced with code to open the image or audio, but I haven't written it yet.
}
});
}
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
To parse the audio or image files, I use this function. It is successfully getting the names of the files. It is called with either "mp4" or "png" as the argument.
private void parse_Files(String fileType){
files = new ArrayList<String>();
File folder = new File(abs_path); // abs_path is the path to the folder with the files
File[] list_of_files = folder.listFiles();
for(int count = 0; count < list_of_files.length;count++){
String file_name = list_of_files[count].getName();
String[] parsed_list = file_name.split("\\.");
if(parsed_list[1].equals(fileType))
files.add(parsed_list[0]);
}
}
Finally here is the xml file that opens as the popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding = "15dp"
android:background="#DCDDF7">
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp"/>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:background="#FFFFFF" >
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scroll_linear">
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Thanks!
Edit:
I tried to change it to a list view, but I am getting the same issue. It opens a pop-up and there are x buttons corresponding to x associated files, but none of the buttons have text or do anything on click. What am I missing?
Here is the revised code:
private void display_media(int check, String header_text){
try{
display_media_array=new String[files.size()];
display_media_array=files.toArray(display_media_array);
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, null);
pw = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
ListView listView = (ListView) layout.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, display_media_array));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
pw.dismiss();
}
});
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
And here is the new xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCDDF7"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp" />
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</ListView>
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp" />
</LinearLayout>
Any help is appreciated, thanks!
I need to create a custom ListPreference dialog so that I can add some header text (a TextView) above the List (ListView).
I've created MyListPreference class that extends ListPreference and overrides onCreateDialogView():
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = (View) inflater.inflate(R.layout.dialog_preference_list, null);
return v;
}
My XML layout dialog_preference_list.xml contains:
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</LinearLayout>
Problem: The TextView is displayed below the ListView instead of above. I need the TextView to be above. I've tried both with LinearLayout and RelativeLayout (using "below" or "above" attributes) with no success: I can't find a way to put the TextView above the ListView... The layout is pretty simple and I cannot see why the list stays above...
Also, note that the problem occurs on both a real device (Nexus 4, Android 4.2.2) and the emulator. However, when looking at the layout rendered in Eclipse's graphical layout, the layout is correct! See both attached pictures.
Any idea on how to solve this?
Layout rendered on the device (incorrect):
Layout rendered on Eclipse (correct):
Edit with solution 10.07.2013
As suggested by the accepted answer, the problem comes from the use of builder.setSingleChoiceItems() in ListPreference's onPrepareDialogBuilder().
I've fixed it by extending ListPreference and overriding onCreateDialogView() to build the Dialog without the builder so that I can create a custom View showing the header text above the list items.
GPListPreference.java:
public class GPListPreference extends ListPreference {
...
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
builder.setNegativeButton(null, null);
builder.setPositiveButton(null, null);
}
private int getValueIndex() {
return findIndexOfValue(getValue());
}
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView) inflater.inflate(R.layout.dialog_preference_list, null);
TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
header.setText(getDialogMessage()); // you should set the header text as android:dialogMessage in the preference XML
lv.addHeaderView(header);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), R.layout.dialog_preference_list_singlechoice, getEntries());
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(getValueIndex() + 1, true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setValueIndex(position - 1);
getDialog().dismiss();
}
});
return lv;
}
}
dialog_preference_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true" />
dialog_preference_list_singlechoice.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="2dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
dialog_preference_list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
I think the problem is with the way ListPreference works. ListPreference uses Builder.setSingleChoiceItems() to create the rows with the RadioButtons, and it has preference over the custom layout you are trying to add (in your case a TextView and a ListView inside a LinearLayout. The solution is extending DialogPreference instead. Here is a link to a GitHub where I created a custom DialogPreference that does what you need. I haven't coded the RadioButton logic.
I guess it's a theming issue. Try changing the theme of your dialog inside the constructor make it something like setStyle(STYLE_NO_TITLE, R.style.AppTheme). Your base app theme with no_title style.
If this is not the issue than it might be related with the ListPreference class itself. It might be overriding your layout for consistency in theming the preference views. However, I have not used ListPreference before, so its just a guess.
Can you reproduce the same result by playing with the themes in XML graphical layout preview?
Another option you can try is to add the TextView as a header to the ListView like this:
TextView textView = new TextView(getActivity());
ListView listView = new ListView(getActivity());
listView.addHeaderView(textView);
The addHeaderView takes a View so you theoretically have anything you want to be the header, but I have only used a TextView.
The link above is broken. On this solution the idea is overriding the ListPreference, and inflating your own listview, with the data defined on the ListPreference.
#Override
protected View onCreateDialogView() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = new ListView(getContext());
// Inflate the view into the header only if a message was set
if (getDialogMessage() != null && ! getDialogMessage().equals("") ) {
TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null);
header.setText(getDialogMessage());
lv.addHeaderView(header, null, false);
}
// Create a new adapter and a list view and feed it with the ListPreference entries
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
R.layout.custom_dialog_single_choice_list_adapter, getEntries());
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(getValueIndex() + 1, true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setValueIndex(position - 1);
getDialog().dismiss();
}
});
return lv;
}
Another important thing is to call onPrepareDialogBuilder and not calling super in it. This will avoid that the listview appears twice.
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
// Not calling super, to avoid having 2 listviews
// Set the positive button as null
builder.setPositiveButton(null, null);
}
private int getValueIndex() {
return findIndexOfValue(getValue());
}
Where dialog_preference_list_header is in my case only a TestView, but it could be a more complex view, and custom_dialog_single_choice_list_adapter could be something like this:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="2dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
My main layout main.xml has only a Button, a EditText and an empty ListView as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:id="#+id/input_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="#+id/input_field"
android:layout_height="40dip"
android:layout_width="fill_parent"
android:layout_weight="5"
/>
<Button
android:id="#+id/send_btn"
android:layout_width="60dip"
android:layout_height="30dip"
android:text="#string/send"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/output_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#006633"
android:visibility="gone"
>
<ListView
android:id="#+id/output_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"
>
<!-- When "send" button in above input_area is pressed,
text from EditText field show here programmatically as a new row of the listview-->
</ListView>
</LinearLayout>
</LinearLayout>
As you see above, there are two child LinearLayout hosted by main LinearLayout.
The 1st child LinearLayout with id input_area consists of a EditText and a Button.
The 2nd child LinearLayout with id output_area is an LinearLayout with an empty ListView, AND its visibility is set to "gone".
The feature I am going to implement is very simple, that's in the input_area, when user input some text in the EditText field, and press the send button, then the input string should be shown in the output LinearLayout as a new row of the listview programmatically.
What I tried is the java code below:
EditText inputTxt = (EditText) findViewById(R.id.input_field);
Button sendBtn = (Button) findViewById(R.id.send_btn);
LinearLayout outputArea = findViewById(R.id.output_area);
//Updated by Saurabh
ListView lv = findViewById(R.id.output_list);
MyListAdapter mAdapter = new MyListAdapter(this, arraylist);
//Update finished
sendBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
int visibility = outputArea.getVisibility();
if(visibility==View.GONE)
// set ListView visible
outputArea.setVisibility(View.VISIBLE);
//get user input
String userInput = inputTxt.getText().toString(); // show this string in a new row of listview
//BUT how to dynamically add new row to the listview here???
}
});
But I am not sure how to add new row to the listview programmatically, anyone can help me?
By the way, I have another layout xml fiel (row.xml) which defined each row's layout.
-----------------------UPDATE------------------------------------
Each row of the list contain a icon and a text string. My list adapter and row layout are showing below:
My adapter:
private static class MyListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
ArrayList<String> arraylist;
public MyListAdapter(Context context, ArrayList<String> arraylist) {
mInflater = LayoutInflater.from(context);
this.arraylist = arraylist;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.userInput = (TextView) convertView.findViewById(R.id.user_input);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setImage(???);
holder.userInput.setText(arraylist.get(position));
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView userInput;
}
}
my list row layout:
<?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="horizontal"
>
<ImageView
android: id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/user_input"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="10dip"/>
</LinearLayout>
Make a Global variable as below
ArrayList<String> arraylist = new ArrayList<String>();
On Click of send button update the adapter that you are setting on ListView by adding
String userInput = inputTxt.getText().toString();
arraylist.add(userInput)
in adapter and then call
adapter.notifyDataSetChanged();
Update
I updated answer in your question and in this post copy your new Adapter class and use that
I'm trying to center two textViews that are in a LinearLayout. This LinearLayout is nested in another one, with a ListView-element.
I think my XML is pretty correct. I fill my textViews dynamically in my Adapterclass.
XML:
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacaturesnummer"
android:layout_width="wrap_content"
android:textColor="#color/Accent"
android:text="x"
/>
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacatures"
android:layout_width="wrap_content"
android:text="y"
/>
</LinearLayout>
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView android:layout_height="fill_parent"
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:text="Er zijn geen jobs die voldoen aan uw criteria..."
android:gravity="center"/>
</LinearLayout>
Adapterclass:
/*
* Klasse VacatureAdapter
*/
private class VacatureAdapter extends ArrayAdapter<Vacature>{
private ArrayList<Vacature> vacatures;
public VacatureAdapter(Context context, int textViewResourceId, ArrayList<Vacature> vacatures){
super(context, textViewResourceId, vacatures);
this.vacatures = getArray();
//System.out.println("Array vacatureadapter: " + v);
}
#Override
public View getView(int position, View convertview, ViewGroup parent){
View view = convertview;
if(view==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.vacature_list_item, null);
//view.setBackgroundColor((position % 2) == 1? Color.LTGRAY: Color.WHITE);
}
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
Vacature vaca = vacatures.get(position);
if(vaca != null){
TextView tvNaam = (TextView) view.findViewById(R.id.vacatureNaam);
TextView tvWerkveld = (TextView) view.findViewById(R.id.vacatureWerkveld);
TextView tvRegio = (TextView) view.findViewById(R.id.vacatureRegio);
if(tvNaam != null){
tvNaam.setText(vaca.getTitel());
if(tvWerkveld != null){
tvWerkveld.setText("Werkveld: " + vaca.getWerkveld());
if(tvRegio!=null){
tvRegio.setText("Regio: "+vaca.getRegio());
}
}
}
}
return view;
}
}
The weird thing is that if my spinner runs, he shows the texts set in my XML correctly, if the spinner stops and fills my ListView it only shows one digit of my number and when I scroll once he shows my number completly plus the second TextView. I don't quite understand what's wrong, maybe some code needs te be put somewhere else?
I've solved my problem by putting my setText code in my Runnable method, after I dismiss my Dialog. Now It looks like this:
private Runnable returnRes = new Runnable(){
public void run(){
if (arrVacatures.size() == 0){
dialog.dismiss();
}
if(arrVacatures!=null && arrVacatures.size() > 0){
adapter.notifyDataSetChanged();
for(int i= 0; i< arrVacatures.size();i++){
adapter.add(arrVacatures.get(i));
}
dialog.dismiss();
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
adapter.notifyDataSetChanged();
}
}
};
I am building a pyramid of buttons and want the size of the pyramid to be able to change dynamically. To accomplish this, I have extremely basic XML files representing the activity, each row of the activity, and each button. I am modeling the solution from the accepted response to this question. The pyramid constructs correctly, but the 50dip button width is not being adhered to. Any ideas why? Is there a better way of doing this?
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pyramid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
/>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"/>
btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
Main Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflate();
}
private void inflate() {
LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid);
for (int row = 1 ; row <= mSize; ++row) {
View rowView = getLayoutInflater().inflate(R.layout.row, null);
LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
for (int column = 1; column <= row; ++column) {
View btnView = getLayoutInflater().inflate(R.layout.btn, null);
Button btn = (Button) btnView.findViewById(R.id.button);
btn.setId(row*10 + column);
rowLayout.addView(btnView);
}
pyramidLayout.addView(rowView);
}
}
In btn.xml, change layout_width="50dip" to width="50dip".
Thanks you for this tutorial. It really helps me :)
In my case, I have a view (2 texts, 1 image, 1 button) who represents 1 result.
When the user click on "search" I need to display it many time. So your example was perferct for me