Prasad
Prasad

Reputation: 39

How to show same image multiple time in row?

I am developing an application for children. Basic maths for children to learn with fun.

In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.

Is any one know solution for this or any one having source code thn help me

Upvotes: 0

Views: 1670

Answers (1)

Amit Hooda
Amit Hooda

Reputation: 2144

this is the way you can do it ,quite simple though :)

your xml should look like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input1" />
 <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/input2" />
 <Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Result"/>
<HorizontalScrollView 
android:id="@+id/scroll1" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout android:id="@+id/linear1"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView 
android:id="@+id/scroll2" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout 
    android:id="@+id/linear2"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView 
android:id="@+id/scroll3" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout 
    android:id="@+id/linear3"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

and you Activity looks like this

package com.example.stackanswer;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AppleActivity extends Activity {

private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apple);
    final EditText input1 = (EditText)findViewById(R.id.input1);
    final EditText input2 = (EditText)findViewById(R.id.input2);
    scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
    scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
    scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
      linear1 = (LinearLayout)findViewById(R.id.linear1);
      linear2 = (LinearLayout)findViewById(R.id.linear2);
      linear3 = (LinearLayout)findViewById(R.id.linear3);
    Button output = (Button)findViewById(R.id.button);
    output.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String input1A = input1.getText().toString();
            String input1B = input2.getText().toString();
            int value1 = Integer.parseInt(input1A);
            int value2 = Integer.parseInt(input1B);
            Log.i("1",""+value1);
            Log.i("2",""+value2);
            linear1.removeAllViews();
            linear2.removeAllViews();
            linear3.removeAllViews();
            linear1.setScrollContainer(true);
            ImageView image ;
            for(int i=0;i<value1;i++){

                image = new ImageView(getApplicationContext());
                image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );

                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear1.addView(image);
                scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
            for(int i=0;i<value2;i++){
                image = new ImageView(getApplicationContext());
                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear2.addView(image);
                scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
            int sum = value1+value2;
            for(int i=0;i<sum;i++){
                image = new ImageView(getApplicationContext());
                image.setImageResource(R.drawable.ic_launcher);
                Log.i("i", i+"");
                linear3.addView(image);
                scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
        }
    });

}

}

Complete source code here

Upvotes: 2

Related Questions