Reusuing UI for each quest in Quizz app? - android

I've a scenario where users take a quiz that has 40+ question. Creating 40+ activities is tedious task & i want to know is there anything exists to reuse?
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: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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Who founded Apple?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:text="Fill answers here"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:clickable="true" />
</RelativeLayout>
In my MainActivity.class, im normally doing like
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// ....Go to next activity through Intent...
}
});
Look inside onClick, I need to have each activity for each question that is not good.
Any other solution?

You probably want to create a data structure for your quizes if you haven't. Once you have a datastructure you can think about what kind of adapter you want to use: that is, the mechanism that will link your set of quizes to the UI. For instance, when needing to display a list of something, listview is used with an ArrayAdapter (or BaseAdapter for more custom styled listview).
In the support libraries there is something called a viewpager, which is a set of pages you can swipe horizontally between. You might consider using this.
Or you can create your own view class that has all the views you need to display the quiz, add a method like switchQuiz(Quiz q) which changes the ui with the new quiz information, and use this view in a single activity. Specifically you can take your activity_main.xml layout file, and instead of using it as the layout of an activity, make a custom view (public class QuizView extends RelativeLayout { }) that has a a way to update the ui for the switch quiz functionality. Read here for more information of how to do this: http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

Create two buttons, prev and next..
prev take you to prev question and next take you next question
Create a class question containing following variable
int number_of_option;
String[] options;
String question;
String answer;
Create a class quiz containing ArrayList<question> list;
add required method to add question, get question , update answer..etc..etc
Create variable current_position_in_UI in quiz class to display that question in activity.
On next/prev button click call a method of quiz to get question fields. something like list.get(position) and update your views with those values on that call. Also update your current_position_in_UI. Hope it helps :)

Related

Giving id's to dynamically created views

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".

Android transition: Buttons not clickable after transition [duplicate]

I have 2 layouts which contain the same buttons
layout_1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and
layout_2.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Please assume these are all valid layouts etc.(I am just adding the relevant code.).
So in my fragment ,I inflate and use layout_1.xml in onCreateView.I want to toggle between the 2 scenes using button_1.
I can set the listener for button_1 in layout_1.xml during the onCreateView().
The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?
It would actually appear that a proper way to do this would be to on the second scene you define an action to be performed as such:
mSecondScene.setEnterAction(new Runnable() {
#Override
public void run() {
((Button) mSecondScene.getSceneRoot().findViewById(R.id. button_1)).setOnClickListener( ... );
}
This will allow you to set your ClickListener on the View without the data binding to a generic click listener method. Then you can perform the Transition to the second scene and viola.
In general, it is not a good idea to have multiple views with the same id. This is what caused the confusion here.
Note: Below is the solution used by OP that was suitable for their specific needs:
One simple solution is to use the onClick attribute in the XML file. You can assign the same onClick method to multiple items. Like this:
And in your activity.java add this:
public void buttonClicked(View v){
Log.d("TAG","Button clicked!!"
// do stuff here
}
2nd option:
When you set a listener for one button with the id of button_1, it does not set the listener for both buttons, it only sets it for the first one. If you want to set the same listener for both, all you need to do is to assign these button different ids and then assign them the same listener.
This is what you should do:
Listener myListener = new Listener(){.. blah blah....};
((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);
3rd option:
findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)
in this case, you need add some id to your layout files, for example: layout_1.xml:
<RelativeLayout
android:id="+id/id_of_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Listview BaseAdapter class getview is being called multiple times for same position

actually my problem is same as this guy's.But I don't know how to resolve it.Here's my listview xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:background="#CCCCCC"
android:id="#+id/tabs">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCCCCC"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/NewTask"
android:id="#+id/tab1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/Friends"
android:id="#+id/tab2"
/>
<Button
android:layout_width="fill_parent"
android:background="#CCCCCC"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="1"
android:text="#string/AddPeople"
android:textSize="20sp"
android:id="#+id/tab3"
/>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:layout_below="#+id/tabs"
/>
</RelativeLayout>
I am trying to call a function in my listactivity from the onpostExecute of my AsyncTask class.
Here's the function of my listactivity.
public void SetImage(Bitmap bmp,int index)
{
View v = this.ls.getChildAt(index);
ImageView img = (ImageView)v.findViewById(R.id.tasks_userimg);
img.setImageBitmap(bmp);
}
This bmp is the bitmap downloaded in the asynctask class and ls is the listview and the index is the index of the item in the listview.When the postexecute runs I call this function with the arguments to update the listview item image.
The problem is exactly same as I've mentioned in the link i.e when I scroll it gives me error.But he solved the problem by changing the layout width of listview but it is not working here.
Seems that like many others , you have some problems with this special view .
I highly recommend watching the lecture "the world of listView".
They talk about a lot of topics related to listView , including the topic of calling getView multiple times on the same view. In short , the reason why it occurs is because getView is also called for measuring the listView items . You can use (and should) use the convertView in order to avoid un-needed inflating and fetching of data .
About the question itself , you should not use findViewById on the listView items , since they are getting re-used . For example , a view that was set for the 0-th position may be set to the 7-th position in case the user has scrolled down .
if you wish to update a single item from the listView , you can use something like this:
// itemIndex is the index of the item to update
final View v=listView.getChildAt(itemIndex-listView.getFirstVisiblePosition());
//now you update your view according to what you wish .
//you must of course handle the cases that you can't get the view .
Of course , once you do it , you will also have to update your data behind the adapter , so that the next time the user scrolls to this item , it will have the bitmap shown to it.
What you should do depends on the asyncTask you've used .
I assume that you download multiple bitmaps while the listview fills its items . If that's the case , check out this sample , or use my suggestion : have an asyncTask for each viewHolder you have . For each time getView is called , cancel the old one and create a new one for the current item.
1) Define your custom adapter
2) Apply holder pattern
3) Use some image uploader (for example this)
Take a look simple example

Text over button on Android like in the Apollo Music Player

I'm a novice on the Android platform when it cames to development. However I'm going further from basic Views and I'd like to create something like the following buttons:
This is what I want to achieve. I first tought that a Button with a custom background would have sufficed. However I don't know any way to make that small darker line with the text inside. All of the image reacts like a button and gets highlighted when you touch it.
Can you help me?
If you look at the source code for Apollo you can see ArtistsFragment is not made up of Buttons but rather an inflated RelativeLayout created by a subclass of the SimpleCursorAdapter class.
Since any view can have an OnClickListener you can make any create a layout to look however you want and still have it act like a button:
// Or load it as an item from an existing layout.
View myView = this.getLayoutInflater().inflate(R.layout.anything);
myView.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// Do stuff.
}
});
Every segment with an image could be a Layout with the background set to the appropriate image. Then, you just put the button inside of the layout.
You have to use Framelayout or RelativeLayout. For example:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/your_drawabele" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="your_text" />
</FrameLayout>

Android: failed to setContentView when switching to ListActivity

[update] I got the error, which says "Your content must have a ListView whose id attribute is 'android.R.id.list'". Appearently nothing in my xml is ListView. But is that required?
This is an follow-up issue on my previous question
android: which view should I use for showing text and image?
I read the article about creating ListView for LinearLayout. However, my following code failed at the setContentView() function when I changed "extends Activity" to "extends ListActivity", any idea why?
private TextView mSelection;
//private ImageView mImages;
static final String[] keywords = new String[]{"China", "Japan", "USA", "Canada"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactLayout);
mSelection = (TextView)findViewById(R.id.ContactNames);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.contactlayout, R.id.ContactNames,keywords);
setListAdapter(adapter);
}
My Layout is from this article: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ContactNames"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
I think you misunderstood the other posts I showed you in the previous question. They were explaining how to use a custom layout for each row in your list, not how to define the entire layout file for the activity. You need something like this:
(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:id="#android:id/list">
</ListView>
Note the very important line android:id="#android:id/list". You must have that in your ListView as that's what tells Android where your list is. The cacheColorHint is useful if your background isn't black - see this post for more details about that.
With the above lines you can give your activity a list that will be recognised properly. Here's a basic example:
public class TestProject extends ListActivity {
final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listrow, R.id.textview, ITEMS);
setListAdapter(adapter);
}
}
Then the listrow layout is just this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"/>
</LinearLayout>
This is a really simple layout. If you want to get something more complicated, changes are you'll have to use a BaseAdapter, as that gives you calls getView(...) before each row. In that you can use different layouts depending on the contents of each row. However, BaseAdapter looks scary when you first try it, so be warned! :)
Yes, if you are using a ListActivity, you need to have a ListView who's id is android.R.list in your layout file.
If you aren't using a ListView in your layout, and I don't see one in there, then switch to using a regular Activity.
Actually, your (custom) layout doesn't need a ListView when using a list activity. The easy way to solve this is just remove the setContentView() line altogether. In simple terms, when you do it, Android "assumes" the layout you're using to contain a single full-screen ListView, and provides it for you.
If you want a different (richer) interface for the Activity though, you must code the XML and use the informed ID for Android to know how to show the list implied by the activity being a ListActivity after all. Note that the layout for an item isn't the same as the list, and although I haven't tried that, I assume you can have a custom item layout without having an explicit ListView in the activity layout.

Categories

Resources