I need create spinner programmatically. I add spinner to linearLayout, its work but I don't know how chache designe it.
Example, I have this:
How remove it?(highlighted in red)
And how remove spinner text to center?
My xml
LinearLayout spinnerL add this my spinner
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id="#+id/spinnerL"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
/>
</RelativeLayout>
and main class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array of choices
View linearLayout = findViewById(R.id.spinnerL);
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
Spinner spinner = new Spinner(this);
//Make sure you have valid layout parameters.
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(spinnerArrayAdapter);
((LinearLayout) linearLayout).addView(spinner );
}
To remove small triangle, simplest method is to set background drawable, for example:
spinner.setBackgroundResource(android.R.drawable.btn_default);
To align text center, you need to create layout for spinner, see examples
Related
In my program, as soon as I create a ListView and I set a Simpleadapter as adapter, I try to get access of a View in this listView in order to change the background of this view depending on a condition. I do it with the method ListView.getChildAt(position). However, I get a nullPointer Exception and i do not understand why. Here is a part of my code that is concerned.
To better understand the code below: I have actually created 2 listViews in the code and Alarm is a class I have implemented. I simply retrieve some pieces of information through this class.
Java Code:
public class SmartAlarm extends AppCompatActivity {
private ListView list_view_alarms;
private ListView list_view_activates;
private List<HashMap<String, String>> listMapOfEachAlarm;
private List<HashMap<String, Integer>> listMapOfActivates;
private SimpleAdapter adapter_alarms;
private SimpleAdapter adapter_activates;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_alarm);
list_view_alarms = (ListView) findViewById(R.id.list_alarm);
list_view_activates = (ListView) findViewById(R.id.list_activate);
listMapOfEachAlarm = new ArrayList<>();
listMapOfActivates = new ArrayList<>();
adapter_alarms = new SimpleAdapter(this, listMapOfEachAlarm, R.layout.item_alarm,
new String[]{"alarm", "title"}, new int[]{R.id.time, R.id.title});
adapter_activates = new SimpleAdapter(this, listMapOfActivates, R.layout.item_activate, new String[]{"alarm_drawable"}, new int[]{R.id.activate});
for (Alarm alarm : alarmList) {
HashMap<String, String> mapOfTheNewAlarm = new HashMap<>();
mapOfTheNewAlarm.put("alarm", alarm.getTime());
mapOfTheNewAlarm.put("title", alarm.getTitle());
listMapOfEachAlarm.add(mapOfTheNewAlarm);
HashMap<String, Integer> mapOfTheAlarmDrawable = new HashMap<>();
if (alarm.getActivated()) {
mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_on);
} else {
mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_off);
}
listMapOfActivates.add(mapOfTheAlarmDrawable);
}
list_view_alarms.setAdapter(adapter_alarms);
list_view_activates.setAdapter(adapter_activates);
for(int i=0; i<list_view_alarms.getCount();i++)
{
if(conditionRespected()){
list_view_alarms.getChildAt(i)
.setBackgroundColor(getResources().getColor (R.color.dark)); //The compilation error is here because list_view_alarms.getChildAt(i) is null
}
}
}
}
activity_smart_alarm.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#drawable/crazy_alarm"
android:orientation="vertical"
android:weightSum="10">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Check the box if you want to activate the game" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list_alarm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:choiceMode="singleChoice"
android:divider="#FF0000"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/list_activate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:choiceMode="singleChoice"
android:divider="#FF0000"
android:dividerHeight="2dp" />
</LinearLayout>
</LinearLayout>
For people interested: To change the background of each view in the listView according to a specific condition, I have finally overrided the method getView() like this :
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = super.getView(position, convertView, parent);
if(condition){
convertView.setBackgroundColor(getResources().getColor(R.color.dark));
}
else
{
convertView.setBackgroundColor(getResources().getColor(R.color.bright));
}
return convertView;
}
You aren't setting your main view. Your listview must be created inside of the view that your activity will actually be using. Right inside of your onCreate() add setContentView(R.layout.activity_main);
You also have to create a layout (in this case activity_main) and your listView has to be child of the activity_main layout
This might be what your activity_main looks like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
//and inside of here would be your listview
<android.support.v7.widget.ListViewCompat
android:id="#+id/list_alarm"
android:layout_width=""
android:layout_height=""></android.support.v7.widget.ListViewCompat>
</RelativeLayout>
I know that this question has been asked a lot of times already but I have went to all of the answers and none of them worked for me.
So, issue with the App is that I am trying to populate an ArrayList into a ListView using an ArrayAdapter and the app MainActivity doesn't show anything but white screen when launched.
Here's
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceholderFragment extends Fragment{
private ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/46",
"Weds - Cloudy - 72/63",
"Thurs - Rainy - 64/51",
"Fri - Foggy - 70/46",
"Sat - Sunny - 76/68"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
return rootView;
}
}
fragment_main.xml
<FrameLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
Here's list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="#+id/list_item_forecast_textview"
>
</TextView>
Of course the activity is not showing anything, you're not placing the fragment inside your activity! Use this in your activity's onCreate():
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
Where R.id.container is a layout inside your main_activity_layout file (empty layout)
UPDATE:
have these 2 file:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
fragment_main:
<FrameLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
keep the 3° layout file, it's ok.
Now, in your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
}
also:
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
//move this after the initialization of adapter
listView.setAdapter(mForecastAdapter);
When you set the ListView adapter,the adapter's reference is null and you set the null adapter for ListView.You should create the adapter before set ListView adapter.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
listView.setAdapter(mForecastAdapter);
Is your PlaceHolderFragment within your MainActivity.java file?
If so, try separating it on its own file PlaceHolderFragment.java..
As far as I know, java only allows one public class per source file.. That maybe the reason of your problem..
I want to display all items of an ArrayList<String> on a ListView. I use an ArrayAdapter<String> for this. The problem is that only the first item ('foo' in my example) of the ArrayList is shown. Can someone tell me why? Am I missing something?
I've made an minimal example using the generated code (Tabbed Action Bar Spinner Activity) from Android Studio 2.
My FooActivity:
public class FooActivity extends AppCompatActivity {
...
public static class PlaceholderFragment extends Fragment {
private ListView fooListView;
private ArrayAdapter<String> mAdapter;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_foo, container, false);
fooListView = (ListView) rootView.findViewById(R.id.foo_list);
updateList();
return rootView;
}
private void updateList() {
ArrayList<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getContext(),
R.layout.item_foo,
R.id.foo_string,
strings);
fooListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
}
My fragment_foo.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.foo.activities.FooActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/foo_list"
android:layout_below="#+id/total_activities"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And the item_foo.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="match_parent">
<TextView
android:id="#+id/foo_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_string"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
If it helps I can post the generated activity_foo.xml (I didn't made any changes there) and the complete FooActivity class too.
The Problem actually was my activity_foo.xml which had a NestedScrollView. I've added android:fillViewport="true" and now it shows every item.
My NestedScrollView now looks like this:
<android.support.v4.widget.NestedScrollView
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true"/>
On any ListView, if you use android:layout_height="wrap_content", you're only going to see one item. Try android:layout_height="match_parent". If the ListView only takes up part of the layout, you'll need a LinearLayout with weights or a RelativeLayout with constraints. But wrap_content won't work for a height on a ListView, ever.
Your Listview look something like this
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/foo_list"
android:layout_marginTop="10dp"/>
I have removed from your list view android:layout_below="#+id/total_activities"
and
android:layout_alignParentBottom="true"
For others: It's okay to nest a ListView inside another layout, and it's also fine to use android:layout_height="wrap_content" on Layouts surrounding the ListView. (So long as the outer-most Layout allows for match_parent)
What is not fine, however, is when the TextView being used to populate the ListView is defined with layout_height="match_parent". It will cover up all of the subsequent rows after the first.
Bad:
<TextView
android:id="#+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Good:
<TextView
android:id="#+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
I have an AutoCompleteTextView and I want to set a filter on it, the problem I'm having is when I go to type something it'll only let me type 1 character in the text field. if I remove textView.setFilters from the code it works fine I just don't have any filters. I have also tried android:textAllCaps="true" in my xml file and it doesn't work. any help would be great, thanks.
My code:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
String[] MyArray = getResources().getStringArray(R.array.myarray);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
textView.setFilters(new InputFilter[]{new InputFilter.AllCaps(), new InputFilter.LengthFilter(40)});
textView.setAdapter(adapter);
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=".myactivity">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textfield"
android:hint="Search..."/>
<Button
android:id="#+id/btn"
android:onClick="buttonClickHandler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something"
android:layout_toRightOf="#+id/textfield"/>
</RelativeLayout>
With the help of #pskink's post I figured it out
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textfield);
String[] MyArray = getResources().getStringArray(R.array.myarray);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, MyArray);
textView.setThreshold(1);
InputFilter[] filters = {
new InputFilter.AllCaps(),
new InputFilter.LengthFilter(40),};
textView.setFilters(filters);
textView.setAdapter(adapter);
I have the following activity:
<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="es.xxx.xxx.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#CCFF0000"
android:id="#+id/lyNetworkError">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No hay conexión a internet"
android:textAlignment="center"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"/>
</RelativeLayout>
In its FrameLayout the app will load other fragments.
This is the onCreate code of activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Constants.setAppContext(this);
setContentView(R.layout.activity_main);
Log.d("LoadFragment", "1 "+ loadFragment);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit();
}
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
fragmentManager = getSupportFragmentManager();
lyNetworkError = (LinearLayout) findViewById(R.id.lyNetworkError);
}
The problem is that LinearLayout (That contains TextView) doesn't show (is posible that fragment render over LinearLayout, because if I remove getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit(); the LinearLayout appears)
So, how can I show the LinarLayout over fragment (loaded inside FrameLayout)?
If the LinearLayout and your Fragments are displaying in the correct location on screen when each is shown individually, then you can simply reverse the order of of the FrameLayout and LinearLayout in your XML.
The problem is that RelativeLayout allows its children to overlap. The last item in the RelativeLayout will appear "above" or "on top" of other items in the layout. Since you haven't specified any layout constraints for your views, the RelativeLayout puts them both in the default position, which is the top left corner. Since your FrameLayout is set to fill the parent view's width and height, it will overlay everything else.
If you actually want the LinearLayout to appear above the FrameLayout, then you can use RelativeLayout's positioning properties (explained very well here) to position your views.
Specifically, you would be looking for something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/lyNetworkError"
android:id="#+id/container"/>
The android:layout_below attribute tells the FrameLayout that you want it to always be below the view with ID lyNetworkError (below as with text on a piece of paper, not in 3-dimensional space).