Ok, I have been on this for 6 hours now and for some reason, I just don't get it.
I am trying to make an app to calculate something using a certain formula.
Now consider it as a grade point average calculating thing. I have a button which should allow the user to add two more spinners to the layout and thereby proceeding further. Now I made a method for this purpose. When I call this method for the first time the spinners are added. But it doesn't work the second time.
It also doesn't work if I put it in the button onClick() method. I have gone through multiple examples posted online and also on stackoverflow, maybe I am making some stupid mistake. Just guide me through this, please.
Here is the code:-
SGPA.class
public class SGPA extends AppCompatActivity {
View v;
Context c;
LinearLayout subjectData;
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
//int id = View.generateViewId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sgpa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
//TODO if 2015 or 2013 regulations
//assuming 2015
fill2015();
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sgpa, null);
subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
c = this;
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addViewForAnotherSubject();
setContentView(v);
}
});
setContentView(v);
}
void addViewForAnotherSubject()
{
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
subjectData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
void fill2013()
{
}
}
activity_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="innominatebit.srmite.SGPA">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_sgpa" />
</android.support.design.widget.CoordinatorLayout>
content_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="6dp"
android:paddingEnd="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingStart="6dp"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="innominatebit.srmite.SGPA"
tools:showIn="#layout/activity_sgpa">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/subjectdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
All help is appreciated. Thanks in advance!
Ok, I got it. i made another test application for debugging purposes until I got it. Seems I did not need a layout inflator at all.
Here's the code:
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
fill2015();
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
final LinearLayout myData=(LinearLayout)findViewById(R.id.myData);
addforanothersubject(myData);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addforanothersubject(myData);
}
});
}
void addforanothersubject(LinearLayout myData)
{
Context c=MainActivity.this;
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
myData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
}
And the xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="aaa.test.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/myData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Hope it helps someone.
Related
I want to have a layout constantly on top of everything and visible all the time, however, once I swipe to the next element in the ViewPager, the FrameLayout is getting behind the ViewPager.
<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:gravity="center_vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I tried to place a button instead of and on top of the FrameLayout and, guess what, the button remains on top of everything without going behind the ViewPager after each sroll.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="197dp"
android:layout_height="286dp">
</Button>
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="197dp"
android:layout_height="286dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</FrameLayout>
Here is my code in MainActivity
private ViewPager2 videosViewPager;
private SurfaceView previewDisplayView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewLayoutResId());
previewDisplayView = new SurfaceView(this);
setupPreviewDisplayView();
videosViewPager = findViewById(R.id.videosViewPager);
List<VideoItem> videoItems = new ArrayList<>();
VideoItem life = new VideoItem();
String path = "android.resource://"+getPackageName()+"/"+ R.raw.video;
life.videoURL = Uri.parse(path);
videoItems.add(life);
VideoItem rest = new VideoItem();
String path2 = "android.resource://"+getPackageName()+"/"+ R.raw.video1;
rest.videoURL = Uri.parse(path);
videoItems.add(rest);
videosViewPager.setAdapter(new VideoAdapter(videoItems));
}
private void setupPreviewDisplayView() {
previewDisplayView.setVisibility(View.GONE);
ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
viewGroup.addView(previewDisplayView);
previewDisplayView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
Log.d("Surface","Surface Created");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onPreviewDisplaySurfaceChanged(holder, format, width, height);
Log.d("Surface","Surface Changed");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(null);
Log.d("Surface","Surface destroy");
}
});
}
I guess my issue is in adding SurfaceView(previewDisplayView)in ViewGroup.
What can I do to make the previewDisplayView get always on top of everything? By the way, I tried bringToFront(), but after a swipe the previewDisplayLayout is automatically going behind the ViewPager.
Thank you in advance
Have you tried android:elevation property ? I think it is what you're looking for,
Hope will help
Good day - I'm new to recyclerViews and connecting to Room database. I've got one working with the help from a tutorial and now I want to add to it. My aim is to:
Implement a Room database which in turn presents data to the user via a recyclerView.
Have 1 recyclerView in the main activity and a 2nd recyclerView in a second activity. I'm trying to have a different layout in each activity as shown in the following links.
Main activity:
Second activity:
The issue I'm having is even though the XML files are unique for each activity, along with the variables within, the name of the recyclerView, and also the methods for each activity, the layout in the second activity shows in the first activity.
activity_main has the following:
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="100dp"
android:id="#+id/recycler_view111"
android:layout_width="match_parent"
android:layout_height="114dp"
tools:listitem="#layout/testing" />
The second activity XML file has the following:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="488dp"
tools:listitem="#layout/testing1_item" />
Can someone please help me identify why the same recyclerView is shown across the 2 activities please?
To trouble-shoot, I've:
Gone through all the XML files to ensure there is no issue with the names of the files/methods etc.
Copied the layout from the activity_main xml and applied it to the second xml file. This then in turn displayed the layout that I'm testing within in MainActivity across both activities.
MainActivity uses the activity_main.xml which uses a test file called testing.xml - code as follows:
MainActivity:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), AllWordsActivity.class);
startActivity(intent);
}
});
RecyclerView recyclerView111 = findViewById(R.id.recycler_view111);
recyclerView111.setLayoutManager(new LinearLayoutManager(this));
recyclerView111.setHasFixedSize(true);
NoteAdapter adapter = new NoteAdapter();
recyclerView111.setAdapter(adapter);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
{
#Override
public void onChanged(List<Note> notes)
{
adapter.setNotes(notes);
}
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="500dp"
android:layout_marginRight="50dp"
android:background="#color/cardview_shadow_start_color"
android:maxLines="1"
android:text="Open new Activity!"
android:textColor="#color/black"
android:textSize="20dp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="100dp"
android:id="#+id/recycler_view111"
android:layout_width="match_parent"
android:layout_height="114dp"
tools:listitem="#layout/testing" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
testing.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/text_view_priority1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textColorHighlight="#color/black"
android:text="1"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:id="#+id/text_view_title1"
android:layout_width="323dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginStart="183dp"
android:layout_toStartOf="#id/text_view_priority1"
android:ellipsize="end"
android:maxLines="1"
android:text="Title1"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:id="#+id/text_view_description1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#id/text_view_title1"
android:text="Description1" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
The second activity is based on a class called AllWordsActivity" which uses activity_all_words which uses note_item test - code as follows:
AllWordsActivity.java
private NoteViewModel noteViewModel;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_words);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);
//noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
{
#Override
public void onChanged(List<Note> notes)
{
adapter.setNotes(notes);
}
});
activity_all_words.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".AllWordsActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="488dp"
tools:listitem="#layout/note_item" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
note_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/text_view_priority"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:textColor="#color/purple_500"
android:text="1"
android:textAppearance="#style/TextAppearance.AppCompat.Large"/>
<TextView
android:id="#+id/text_view_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Title"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:maxLines="1"
android:layout_toStartOf="#id/text_view_priority"
android:layout_alignParentStart="true"
android:ellipsize="end"/>
<TextView
android:id="#+id/text_view_description"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/text_view_title"
android:text="Description" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Code for noteAdapter:
```public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder>
{
private List notes = new ArrayList<>();
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_item, parent, false);
return new NoteHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position)
{
Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
}
#Override
public int getItemCount()
{
return notes.size();
}
public void setNotes(List<Note> notes)
{
this.notes = notes;
notifyDataSetChanged();
}```
The problem is that both activities use the same adapter for their list view: NoteAdapter adapter = new NoteAdapter();. In turn, NoteAdapter only inflates note_item.xml for each item in the list:
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_item, parent, false);
To fix this, you need to either change NoteAdapter to work with multiple layout files for its rows. Or you need to create a different adapter for each RecyclerView.
Original Answer:
It sounds like you are seeing the same items displaying in both of your recycler views. The problem is that both the MainActivity and the AllWordsActivity have these lines:
NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
{
#Override
public void onChanged(List<Note> notes)
{
adapter.setNotes(notes);
}
});
So both activities are getting data from the same ViewModel to display. To display different data, use a different ViewModel in the MainActivity.
p.s. I suggest using consistent naming. You have AllWordsActivity and NotesAdapater. Either use AllWords or Notes, but don't mix and match.
I need to set a ListView's Adapter which is located in a Fragment from an Activity.
I have tried to set the Adapter from the Activity with the code below, but it returns a NullPointerException, presumably because the Fragment has not been loaded yet.
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
Is there a way to run the above code after the Fragment has loaded in, or a better way of doing this altogether? I have attempted to use a method that would set the Adapter inside the Fragment itself, but I still need to wait for the fragment to be loaded before I instigate it from the Activity or I will still receive a NullPointerException. I would also need to put an ArrayList<HashMap<String, String>> into a bundle, which there is no documentation on how to do so if I was going to use that method.
I have also tried to instigate a method that holds the set adapter code that is located inside the Activity from the Fragment's onActivityCreated() method which still returned a NullPointerException.
Am I not referencing the ListView element correctly from the Activity or is there a way to wait for the Fragment to load in, so I don't receive an error?
TabbedActivity.java / Main Parent Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(6);
mViewPager.setAdapter(mSectionsPagerAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// ...
interpretGrades(retrieveGrades());
}
public void interpretGrades(String input) {
// take CLASSES~~MAINCLASSDIVIDER~~GRADES
// turn it into Classes = Classes
// Grades = Grades
final String[] ServerResponse = input.split("~~MAINCLASSDIVIDER~~");
final String[] Classes = ServerResponse[0].split(";");
final String[] Grades = ServerResponse[1].split(";");
ArrayList<HashMap<String, String>> item_list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Classes.length; i++) {
HashMap<String, String> item = new HashMap<String, String>();
String[] ClassSplit = Classes[i].split("-");
String Period = ClassSplit[0];
String Class = ClassSplit[1];
item.put("Period", Period);
// --.substring(1, --.length()) removes first character from string (in this case, a whitespace from the class name)
item.put("Class", Class.substring(1, Class.length()));
item.put("Grade", Grades[i]);
item_list.add(item);
}
String[] list_input = new String[] { "Period", "Class", "Grade" };
// PUT THE STRING VALUES FROM list_input INTO THE XML LAYOUT VALUES
int[] layout_values = new int[] { android.R.id.text1, android.R.id.text2, R.id.grade};
// LISTVIEW LAYOUT (XML FILE)
int list_layout = grade_list;
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
}
Tab2Pulse.java / Fragment
public class Tab2Pulse extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.tab2chat, container, false);
return mainView;
}
}
tab2chat.xml / Fragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab2chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/gradeListView"
style="#android:style/Widget.Material.Light.ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
activity_tabbed.xml / Main Activity Layout
<android.support.design.widget.CoordinatorLayout
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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/main_gradient"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<View
android:layout_width="match_parent"
android:layout_height="12dp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/main_gradient"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white" />
</android.support.design.widget.AppBarLayout>
<View
android:id="#+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_below="#id/toolbar"
android:background="#drawable/toolbar_dropshadow" />
<!--<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
android:layout_width="368dp"
android:layout_height="495dp">-->
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="#*android:dimen/navigation_bar_height"
android:background="#drawable/login_gradient" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});
First of all create a container view in your main layout which is going to hold the new Edittexts.
<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.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}
I get inputs from the user and then put them in a ListView, but when I click on the button, it works with no errors, opening the layout and immediately closing it. When I try with arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_list);, it works perfect.
public class HomeChat extends AppCompatActivity {
ListView listView;
EditText writeSms;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> m_list = new ArrayList<String>();
String emriUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_chat);
writeSms = (EditText) findViewById(R.id.shkrunSms);
listView = (ListView) findViewById(R.id.listView);
emriUser = getIntent().getExtras().getString("emri");
arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_chat,m_list);
listView.setAdapter(arrayAdapter);
}
public void sentSmsButton(View view) {
String mesazhet = emriUser + ": " + writeSms.getText().toString();
if (writeSms != null && writeSms.length() > 0) {
m_list.add(mesazhet);
arrayAdapter.notifyDataSetChanged();
writeSms.setText("");
}else
{
Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_LONG).show();
}
}
}
Here is the chat 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:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
</LinearLayout>
The problem is that ArrayAdapter is expecting a layout with only a single TextView in it. So, your layout with a LinearLayout containing a TextView won't work. Try changing R.layout.list_chat to simply:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>