Giving id's to dynamically created views - android

I'm a beginner in coding, and I would love some help. I want to make an alarm application. On my main page fragment, I added a button that will add an alarm into a LinearLayout inside a ScrollView. The alarm will have three TextViews in it, and a button for activation/deactivation.
Here is how I would like my alarm to look like (this is currently not being used anywhere in my coding; I created it just to have a visual aid of what I'm aiming to make):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:id="#+id/alarm_fl"
android:background="#mipmap/white_box">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="9.5dp"
android:id="#+id/alarm_activation_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:textSize="10pt"
android:id="#+id/alarm_time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="11.5dp"
android:textSize="7pt"
android:id="#+id/alarm_ampm"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"
android:textSize="5pt"
android:id="#+id/alarm_day"/>
</FrameLayout>
This is how I'm currently testing my alarms in the fragment:
addAlarm.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
LayoutInflater alarm_inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.alarm_ll);
View alarm_view = alarm_inflater.inflate(R.layout.alarm_layout, parent);
TextView alarm_time = (TextView) alarm_view.findViewById(R.id.alarm_time);
alarm_time.setText("9시 45분");
TextView alarm_ampm = (TextView) alarm_view.findViewById(R.id.alarm_ampm);
alarm_ampm.setText("오후");
TextView alarm_day = (TextView) alarm_view.findViewById(R.id.alarm_day);
alarm_day.setText("월,화,수,목,금");
Button activation_button = (Button) alarm_view.findViewById(R.id.alarm_activation_button);
activation_button.setBackgroundResource(R.mipmap.checkbox_deactivated);
}
});
where alarm_ll is the LinearLayout that I want to populate with newly created alarms.
And it appeared to me that I need unique id's for each of the Buttons and TextViews to manipulate them separately.
Now here are my questions:
Is this the right approach if I want to add views programmatically whenever the button is clicked? Or is there a better, simpler way?
My alarms would eventually need to be objects. Would it be possible for a non-activity class like User, or in this case Alarm, to have a layout for it's own?
How do I give unique id's to each view when creating via a button click?
When I test-run my application now, the layouts I add into alarm_ll won't be saved, so if I shift to another activity and come back, alarm_ll will be reset. How do I save these in a fragment, when they are not in primitive data types?
I'm sorry to ask so many questions at once, but I would really love some answers or suggestions. Thank you.

I assume that you want to have the ability to set multiple alarms. This is a perfect use of ListView or RecyclerView which allows you to inflate several copies of the same view and display a list based on some underlying data.
I suggest that you learn about creating "model" which are objects that store the data. These "model" objects should be separate from the view objects which display the data. There are several design patterns which are commonly used for this kind of separation: Model-View-Controller, Model-View-Presenter, and Model-View-ModelView.
android:id in XML is typically used to be able to get an object for the view in the Java code. When you create a view dynamically, either the way you showed in your question or by inflating XML, you already have this object, so I do not see a need for assigning an ID to these dynamically created views.
When using one of the design patterns which I suggested in #2, you will also create a way to store the data. Android provides an API to store information in a database. This can easily be displayed in a ListView or RecyclerView by using an "adapter".

Related

Recyclerview add radiobutons programmatically

I am creating an Android app with Java which displays a question and 3 or 4 options for it (MCQ app). I have used a RecyclerView with a TextView in the XML to hold the question.
However, I am confused on how to create the radio button/checkbox for the option part since those are dynamic. Meaning, a question might have any number of options as radio button from 1 to 6 depending on the entries in the database. Also, few of the question might have checkbox rather than radio button again depending on the DB entries.
Since these are decided at runtime, there is no way I can keep these radiobuttons/checkboxes in the XML file. Initially I tried creating them inside onBindViewHolder method but doing that significantly slows down the scrolling since this method gets called even while scrolling. Since this is a very common use case, I would like to believe the framework must have readily available solution for this which I am yet to discover.
I do not want to add all the possible radiobuttons/checkboxes and play with the visibility of those in runtime.
You can create your xml file as -
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Question Text " />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</LinearLayout>
In Your adapter let you have arraylist of Questions say
ArrayList<Question> queList;
Instance of Question contain
class Question{
String question;
int nubOfOptions;
String[] options;
int selectedOption = 0;
}
now First take the id of radioGroup in ViewHOlder as -
RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.radioGroup);
Now in onBindViewHolder you can do as -
onBindViewHolder(int position, ....){
Question que = queList.get(position);
radioGroup.removeAllViews();
for(int count=0; count<radioBtnCount;count++){
RadioButton btn= new RadioButton(context);
btn.setText("you option");
if(selectedOption==count+1){
btn.setCheck(true);
}
radioGroup.addView(btn);
}
}
you have to remove all views otherwise you will face problem while scroll.

Android how to create textview one over another

I am developing an Android application with a textview updated by one event and at the same place where the textview is present, I want 1 more textview so that other event can update this new textview. How do i achieve in having 1 textview on other
I'm assuming that you're asking how you could have two TextView components overlaying each other. There are a few way you could do this.
Frame Layout
Use a Frame Layout to determine the area in which the TextViews will occupy. Like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Credit goes to https://stackoverflow.com/a/2634059/3769032
Create a Compound View
This is fairly in-depth for the type of question you are asking. CompoundViews are a collection of typical views, such as a TextView, that you can create if you plan on re-using the view frequently.
If you plan on overlaying the TextViews often, I recommend this. So check out this tutorial.
Use only one TextView
Having two overlayed textviews can become messy really quickly. If you have two pieces of text overlayed is becomes impossible to read. So since the content of your textview is based on an event. Use the same event listener in your java code to determine the content of your TextViews.
For example, in your on click listener you might have...
TextView tv1 = (TextView) findViewByID(R.id.tv1);
public void onClick(View view){
if (first_event_happened){
tv1.setText("One event happened");
} else if(second_event_happened){
tv1.setText("A different event happened");
}
}
These conditions might mean checking the type of view that was clicked, or checking its id (what I usually do). Please comment if things aren't clear. Some clarification on your question would be helpful too.
use relative layout and also you can set text on exiting textView like when event one triggered textView.setText(your text) and same when event two triggered textView.setText(your text)
There is no trick to this. Just put two TextViews in a RelativeLayout at the same position and they will draw overtop of one another. Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first textview"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second textview"/>
</RelativeLayout>
You can make each one visible or invisible by using TextView.setVisibility(...) or you can set their text with TextView.setText(...).

Sharing data with custom canvas view

I'm a beginner at android programming, so excuse me if my wording is slightly incorrect.
I have a custom canvas view along with a TextView inside a linear layout, defined in the layout file as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/blah"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="top|center_horizontal"
/>
<com.*.Overlay android:id="#+id/combined"
android:layout_width="fill_parent"
android:layout_height="100dp"
/>
</LinearLayout>
I need to be able to read the text contained in the TextView from within the Overlay custom class that I created.
(The overlay class takes in 2 bitmaps and puts one on top of the other. The bitmaps used will depend on the text in the TextView.)
I considered using intents, but the Overlay class doesn't have an onCreate method.. All my code is within the onDraw method. I also added the necessary constructors.
I'm not sure what to try next, perhaps try accessing the parent linearlayout and then its child textview?
Hope I managed to explain everything in a non-confusing manner
Ok, managed to fix the issue... sort of
I found out that Views need to be contained in Activities.. so I created a new Activity with my custom view as an inner class, passed an intent with the necessary data to the activity and was able to use it successfully in my custom canvas view.
I was a bit surprised I didn't get any responses, but I guess that's because I'm new here

How to handle clicks / touches on repeated custom components?

So, I have a screen that has from 0 to 6 of the same user interface components included via XML. Something similar to this:
<TableRow android:layout_height="150px">
<include android:id="#+id/p1" android:layout_width="fill_parent"
android:layout_height="wrap_content" layout="#layout/numerics_component"
android:onClick="onClick" />
</TableRow>
<TableRow android:layout_height="150px">
<include android:id="#+id/p2" android:layout_width="fill_parent"
android:layout_height="wrap_content" layout="#layout/numerics_component"
android:layout_below="#id/p1" android:onClick="onClick" />
</TableRow>
... etc
Each of those included UI's is a collection of several widgets and custom components that I am reusing.
I want to detect clicks on components in those included bits in my Activity and respond appropriately. The problem is, in my onClick method if I follow the common pattern, I can never tell which of the views got a click:
public void onClick(View view) {
Log.d(loggingName, "Got onClick event on view: " + view);
// Identify the view, and handle appropriately:
switch (view.getId()) {
...
With the above code I can never tell which of the 6 copies of the component got clicked. There must be a good way to do this, but I am not seeing it.
Further, I don't want to hard code the reusable component to one activity because I want to reuse it throughout multiple activities in my app. So ideally, I'd be able to setup the listeners in my Activity.
Any ideas on how I can do this?
Thanks!
You could search the hierarchy to figure out which sub-component has been clicked. I see that each row of your table has a unique id. Thus, even though the layout is being repeated with identical ids, the view hierarchy does not have identical ids in any sub-tree.
Thus, to identify which sub-component has been clicked obtain the id of the parent view (which in your case is a row) and see which one of the 6 rows the view belongs to. Does that make sense?
1) The Android View class allows you to tag each instance of a view via the setTag method.
So, just setTag() on each view with some unique Integer, or even perhaps an object with a method that you will invoke. Then in your click listener, just do a getTag() to differentiate between the various view instances.
2) You can put a unique onClickListener on each of the view instances.
3) You could do a findViewById() on each of the view instances, and store them in member variables, an array, or some other data structure. Then in your onClickListener, you simply compare the View reference passed to onClick() against your list of saved references.

Set, get text and value in listitem view in Android

I have listview collection that show id and name in Android. I following example from this resource site.
But actually that example only display name. I wish similar like checkboxlist in ASP.NET that contain property text and value. So the text to show name and value store id. When user select one item, I could retrieve id by viewtext.
My question is how do I set id and name in same viewtext item? I wish could pass the id to another layout.
I know that possible using multi viewtext then set invisible of viewtext using map. How that could be waste memory.
i would suggest a custom listview. you can google for "custom listview" and will find a bunch of tutorials on that. simply said, it is a listview that has a custom row that you can specify in a xml file.
here is a quite useful tutorial to start with.
here is what i coded, maybe it will help you. note that this is a bit different, because i am not defining a listactivity but a listview widget. but concerning the custom row item, it won't matter.
this will create a listview and specify an adapter for that listview.
CustomListView listview = (CustomListView) findViewById(R.id.list);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String(getApplicationContext(), R.layout.rowoflistview, R.id.label);
listview.setAdapter(arrayadapter);
my rowoflistview.xml looks like the following. it adds an image and a text to each row of the list. you can of course change it to (mostly) whatever you want.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
</LinearLayout>
as i wanted the listview as a widget in my main activity and not as a fullscreen activity, i had to do it other than you when it comes to event listening and click listening. if you want it a bit easier, be sure to extend the listactivity in your custom listview-activity and override the default methods.
hope that was understandable to get a grip on the topic ;)
Views have an associated tag Object map, which could contain your id http://developer.android.com/reference/android/view/View.html#getTag%28int%29

Categories

Resources