FindViewById returns NULL Scroll View - android

I'm working on an android application and i have this problem. I want to add checkboxes dynamically in a scroll view which will be displayed in a dialog.
The problem is layout is null when going through this expression ScrollView layout = (ScrollView) findViewById(R.layout.custom);
Here is the function.
private void openCustomMenu() {
// TODO Auto-generated method stub
ScrollView layout = (ScrollView) findViewById(R.layout.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
final Dialog dialog = new Dialog(this);
dialog.setContentView(layout);
dialog.setTitle("Bus Stops");
}
And here my xml file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</ScrollView>
Thank you for you help !
EDIT : new XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/custom">
</LinearLayout>
</ScrollView>
and new function.
private void openCustomMenu() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Bus Stops");
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
}

change findViewById(R.layout.custom); to findViewById(R.id.custom); , so in your code, do this :
ScrollView layout = (ScrollView) findViewById(R.id.custom);
you are trying to get the id from the layouts and not from the ids.

Related

How to Find Edit Text Box and Spinner from dynamically created Can anyone

Im not able to find out Spinner and Edit text in Layout
Code is as below It only Find Out EditText in Multiple Time
try {
int count = my_linear_layout1.getChildCount();
//EditText ed[] = new EditText[count];
for (int i = 1; i < count; i++) {
View row = my_linear_layout1.getChildAt(i);
row = my_linear_layout1.findFocus();
if(row instanceof EditText)
{
// you got the Edit Text if (v.getClass().equals(TextView.class))
EditText textOut = (EditText) row.findViewById(R.id.OBSpn);
String data = textOut.getText().toString();
}
else if(row instanceof Spinner)
{
// you got the Spinner
Spinner Spin = (Spinner) row.findViewById(R.id.SPNOBS);
String data1 = Spin.getSelectedItem().toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Instead of LinearLayout, Use ListView.
Follow this doc.
https://developer.android.com/reference/android/widget/ListView
My question is why cant the ScrollView be the parent layout, relative layout is not needed.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<!-- Add extra -->
</LinearLayout>
</ScrollView>

Fragment with cardslib to fill the whole screen

I tried making an app using this tutorial (example 2). I created a navigation drawer activity.
My main fragment xml (added card):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_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$PlaceholderFragment"
android:id="#+id/mainfragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/abovecards">
<it.gmariotti.cardslib.library.view.CardListView
android:id="#+id/myList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card:list_card_layout_resourceID="#layout/list_card_thumbnail_layout"
style="#style/list_card.thumbnail"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
In my main activity activity I added:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cardthingy();
}
public void cardthingy(){
int listImages[] = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher1,
R.mipmap.ic_launcher, R.mipmap.ic_launcher1, R.mipmap.ic_launcher};
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i<5; i++) {
// Create a Card
Card card = new Card(this.getActivity());
// Create a CardHeader
CardHeader header = new CardHeader(this.getActivity());
// Add Header to card
header.setTitle("Test: " + i);
card.setTitle("Some text here");
card.addCardHeader(header);
CardThumbnail thumb = new CardThumbnail(this.getActivity());
thumb.setDrawableResource(listImages[i]);
card.addCardThumbnail(thumb);
cards.add(card);
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this.getActivity(), cards);
CardListView listView = (CardListView) getActivity().findViewById(R.id.myList);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
//this I tried from googling possible solutions
FrameLayout mainy = (FrameLayout) getActivity().findViewById(R.id.container);
mainy.setLayoutParams(new DrawerLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
RelativeLayout fragy = (RelativeLayout) getActivity().findViewById(R.id.mainfragment);
fragy.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
ScrollView feed = (ScrollView) getActivity().findViewById(R.id.scrollView);
feed.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout rLGreen = (LinearLayout) getActivity().findViewById(R.id.abovecards);
rLGreen.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}
}
The problem is that it is showing a scrollable screen that only fills up space of 1 card instead of the whole screen:
As you can see in my xml and java, I've tried some things to fix this, but unfortunately I'm out of ideas.
Try changing
LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT
to
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT
Ok. The solution was pretty simple. The problem was fixed by removing the scrollview from the xml
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollView" >
</ScrollView>
The card list view automatically becomes scrollable after a certain amount of items.
And the following lines weren't necessary any more:
//this I tried from googling possible solutions
FrameLayout mainy = (FrameLayout) getActivity().findViewById(R.id.container);
mainy.setLayoutParams(new DrawerLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
RelativeLayout fragy = (RelativeLayout) getActivity().findViewById(R.id.mainfragment);
fragy.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
ScrollView feed = (ScrollView) getActivity().findViewById(R.id.scrollView);
feed.setLayoutParams(new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout rLGreen = (LinearLayout) getActivity().findViewById(R.id.abovecards);
rLGreen.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));

Connecting xml to programmatic layout in Android

I have a custom layout java file that I create and add views to like this:
PredicateLayout layout = new PredicateLayout(this);
for (int i = 0; i < someNumberThatChangesEveryTime; i++)
{
TextView t = new TextView(this);
t.setText("Hello");
t.setBackgroundColor(Color.RED);
t.setSingleLine(true);
layout.addView(t, new PredicateLayout.LayoutParams(2, 0));
}
setContentView(layout);
What I would like to do is define my TextView in an xml, so I can add it like so:
TextView t = (TextView) findViewById(R.id.textview);
And of couse also add other kinds of views. I have no idea how to write this xml file, or how to connect it to this activity. The layout I'm using is this one: Line-breaking widget layout for Android
Find the id of the root layout in xml and add other ui elements programatically to the root layout.
activity_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"
android:orientation="vertical"
andorid:id="#+id/ll
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Blank text" />
</RelativeLayout>
Your MainActivity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
TextView tv= (TextView) findViewById(R.id.tv);
// add other ui elements to the root layout ie Relative layout
}
}

How to add a button control to an android xml view at runtime?

I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?
Thanks
I see the error u r doing here
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.
Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html
Ok, I have got it to work.
The steps are the following:
First inflate the xml layout, ie,
View view = View.inflate(this, R.layout.main, null);
Then instantiate the container object from the xml layout into a ViewGroup class, ie,
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);
Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,
container.addView(buttonsLayout);
this.setContentView(view);
You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/label"
android:layout_width="fill_parent"/>
<LinearLayout android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContainer = (ViewGroup)view.findViewById(R.id.container);
}
Now you are all set to add other views to your container ViewGroup:
LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);
In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.
just try this:
LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);
now create button dynamically like this
Button btn1 = new Button(this);
btn1.setText=("Button 1");
mainLinearLayout .addView(btn1);
now if you want to add onether linearlayout then add it below button then
LinearLayout llinner = new LinearLayout(this);
Button btn2 = new Button(this);
btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);
llinner.addView(btn2 );
mainLinearLayout .addView(llinner);
Try this :
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);
This might help you
Here is what I did to display a set of runtime buttons on table layout.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = View.inflate(this, R.layout.activity_main, null);
TableRow myrow = (TableRow) view.findViewById(R.id.myrow);
Button btn1 = new Button(this);
btn1.setText("Button 1");
myrow .addView(btn1);
Button btn2 = new Button(this);
btn2.setText("Button 2");
myrow .addView(btn2);
this.setContentView(view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/myview"
android:layout_width="404dp"
android:layout_height="691dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow android:id="#+id/myrow"></TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
AVD Pixel4API30
Output screenshot

Android LayoutWidth being disregarded by cascaded use of LayoutInflater

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

Categories

Resources