5

Is it possible to add text from java coding of a list view to marquee scroll in Android. If yes can you please let me know how?

If needed I shall post the code used. This was the XML for listview used is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
     <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
    <ListView
        android:id="@+id/audiolist_listView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/backgroundimg"
        android:divider="@drawable/gradient"
        android:dividerHeight="1dp"
        android:scrollbars="vertical" >
    </ListView>

</LinearLayout>

The java class for listview is as follows

public class Audiovediolist extends Activity implements OnItemClickListener {
    private ListView audioList;
    private Intent frmHome;
    private ArrayList<HashMap<String, String>> dataList;
    private HashMap<String, String> map;
    private HashMap<String, String> data;
    private int Screen_Id;
    private AdView adView;
    String name;
    String mSign[] = { "Aries-Mesam", "Taurus-Vrushabham", "Gemini-Midhunam",
            "Cancer-Karkatakam", "Leo-Simham", "Virgo-Kanya", "Libra-Tula",
            "Scorpio-Vruchikam", "Sagittarius-Dhanussu", "Capricorn-Makaram",
            "Aquarius-Kumbham", "Pisces-Meenam" };
    Integer mImages[] = { R.drawable.meshamu, R.drawable.vrushabhamu,
            R.drawable.medhunam, R.drawable.karkatakam, R.drawable.simham,
            R.drawable.kanya, R.drawable.tula, R.drawable.vruchikam,
            R.drawable.dhanussu, R.drawable.makaramu, R.drawable.kumbhamu,
            R.drawable.meenamu };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_RIGHT_ICON);
        setContentView(R.layout.list);
        frmHome = getIntent();
        getIntentValues();
        initUI();
        setUI();
        uiListener();
    }

    private void initUI() {
        audioList = (ListView) findViewById(R.id.audiolist_listView);
        dataList = new ArrayList<HashMap<String, String>>();
        adView = new AdView(this, AdSize.SMART_BANNER, "a150b89c23af3b2");
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
        layout.setGravity(Gravity.TOP);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
    }

    private void uiListener() {
        audioList.setOnItemClickListener(this);
    }

    private void getIntentValues() {
        String id = frmHome.getStringExtra("Activity_Id");
        Screen_Id = Integer.parseInt(id);
        if (Screen_Id == 1) {
            getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                    R.drawable.audioo);
            this.setTitle(getResources().getString(R.string.audio));
            this.setTitleColor(getResources().getColor(R.color.Beige));
        } else {
            this.setTitle(getResources().getString(R.string.video));
            this.setTitleColor(getResources().getColor(R.color.Beige));
            getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                    R.drawable.videoo);
        }
    }

    private void setAdapter() {
        SimpleAdapter adapter = new SimpleAdapter(this, dataList,
                R.layout.list_row, new String[] { "Image", "text" }, new int[] {
                        R.id.audiolist_row_img, R.id.audiolist_row_tv });
        audioList.setAdapter(adapter);
    }

    private void setUI() {
        for (int a = 0; a <= 11; a++) {
            map = new HashMap<String, String>();
            map.put("Image", "" + mImages[a]);
            map.put("text", mSign[a]);
            dataList.add(map);
            map = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        setAdapter();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.gc();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adView.destroy();
    }

    /*
     * private class NextTask extends AsyncTask<Void,Void,Void> { ProgressDialog
     * progressDailog = new ProgressDialog(Audiovediolist.this);
     * 
     * @Override protected void onPreExecute() { super.onPreExecute();
     * progressDailog.setTitle("Please Wait");
     * progressDailog.setMessage("URL Data Loading ...");
     * progressDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     * progressDailog.setCancelable(true); progressDailog.show(); }
     * 
     * @SuppressWarnings("static-access")
     * 
     * @Override protected Void doInBackground(Void... params) { return null; }
     * 
     * @Override protected void onPostExecute(Void result) {
     * progressDailog.dismiss(); } }
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        data = dataList.get(position);
        //FinalConstants.AUDIO_VIDEO = "list";
        try {
            NetWorkCheck netWork = new NetWorkCheck();
            if (netWork.isNetWorkConnection(this)) {
                if (Screen_Id == 1) {
                    // new NextTask().execute();
                    Intent audioInt=new  Intent(getApplicationContext(),AudioView.class);
                     audioInt.setType(data.get("text"));

                     startActivity(audioInt);
                } else if (Screen_Id == 2) {
                    Intent vedioInt = new Intent(this, VideoPlayActivity.class);
                    vedioInt.setType(data.get("text"));
                    startActivity(vedioInt);
                }
            } else {
                netWork.alert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Problem while ListItemClick",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

The code for audio player where i need the text selected to scroll is as follows

public class AudioView extends Activity implements OnClickListener {
    private MediaPlayer mMediaPlayer;
    private ImageView mPlay;
    private ImageView mPause;
    private ImageView mBack;
    private ImageView mFar;
    private ProgressBar myProgressBar;
    private boolean boolFlag = false;
    private boolean isPausedInCall = false;
    private String url;
    private TelephonyManager mTelephoneMgr;
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
    private TextView tv;
    // private boolean serviceRunning;
    Notification notifyDetails;
    private AdView adView;


    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.marquee);
        Toast.makeText(this,"please wait.........",Toast.LENGTH_LONG).show();
        url=getIntent().getType();
        initUI();
        uiListener();

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyDetails = new Notification(R.drawable.mulug_icon, url,
                System.currentTimeMillis());
        mTelephoneMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mTelephoneMgr != null) {
            mTelephoneMgr.listen(phoneStateListener,
                    PhoneStateListener.LISTEN_CALL_STATE);
        }
        /*if(FinalConstants.AUDIO_VIDEO=="listitem"){
        LoginProgress task = new LoginProgress();
        task.applicationContext = AudioView.this;
        task.execute();
    }*/
    }

    private void initUI() {
        tv = (TextView) findViewById(R.id.text);
        mBack = (ImageView) findViewById(R.id.back);
        mPlay = (ImageView) findViewById(R.id.play);
        mPause = (ImageView) findViewById(R.id.pause);
        mFar = (ImageView) findViewById(R.id.fwd);
        myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
        myProgressBar.setProgressDrawable(getResources().getDrawable(
                R.drawable.green_progress));
        myProgressBar.setProgress(0);
        adView = new AdView(this, AdSize.BANNER, "a150b89c23af3b2");
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
        layout.setGravity(Gravity.TOP);
        layout.addView(adView);
        adView.loadAd(new AdRequest());
        tv.setText(url + "more Deails Click on www.mulugu.com");
        tv.setSelected(true);
    }
    private void uiListener() {
        mPlay.setOnClickListener(this);
        mPlay.setOnClickListener(this);
        mPause.setOnClickListener(this);
        mPause.setVisibility(android.view.View.INVISIBLE);
        mBack.setOnClickListener(this);
        mFar.setOnClickListener(this);
    }

    private Thread myThread = new Thread() {
        public void run() {
            while (mMediaPlayer.getCurrentPosition() < mMediaPlayer
                    .getDuration()) {
                try {
                    myProgressBar
                            .setProgress(mMediaPlayer.getCurrentPosition());
                } catch (Throwable t) {
                }
            }
        }
    };



    @Override
    protected void onStart() {
        super.onStart();
        this.setProgressBarIndeterminateVisibility(true);
        try {
            if (boolFlag == false) {
                System.out.println(FinalConstants.URL + "mobile-audio/" + url+ ".mp3");
                mMediaPlayer = MediaPlayer.create(
                        this,
                        Uri.parse(FinalConstants.URL + "mobile-audio/" + url
                                + ".mp3"));
            }
            myProgressBar.setMax(mMediaPlayer.getDuration());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Data not available",
                    Toast.LENGTH_SHORT).show();
            Log.e("ERRORS", "Data Not Exist in that Url...");
            super.onBackPressed();
            e.printStackTrace();
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mMediaPlayer.stop();
        // myThread.stop();
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
        System.gc();
    }

    @Override
    protected void onDestroy() {
        adView.destroy();
        super.onDestroy();
        System.gc();
    }



    public PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                System.out.println("ringing state");
                if (mMediaPlayer != null) {
                    pauseMedia();
                    isPausedInCall = true;
                }
                notificationShow();
            }
            if (state == TelephonyManager.CALL_STATE_IDLE) {
                System.out.println("callState Idle");
                if (mMediaPlayer != null) {
                    if (isPausedInCall) {
                        isPausedInCall = false;
                        playMedia();
                    }
                }
            }
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    private void pauseMedia() {
        mMediaPlayer.pause();
        changePlayerIcons(false);
    }

    private void playMedia() {
        mMediaPlayer.start();
        changePlayerIcons(true);
    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (!boolFlag) {
                try {
                    notificationShow();
                     mMediaPlayer.setLooping(false);
                     mMediaPlayer.prepareAsync();
                } catch (Exception e) {
                    Toast.makeText(this, "preparing", Toast.LENGTH_SHORT)
                            .show();
                }
                boolFlag = true;
                mMediaPlayer.start();
                myThread.start();
            } else {
                notificationShow();
                mMediaPlayer.getCurrentPosition();
                mMediaPlayer.start();
            }
            changePlayerIcons(true);
            myProgressBar.setProgress(mMediaPlayer.getCurrentPosition());
            mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    changePlayerIcons(false);
                    myProgressBar.setProgress(0);
                    onStart();
                }
            });
            mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                }
            });
            break;
        case R.id.pause:
            mMediaPlayer.pause();
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
            break;
        case R.id.back:
            int dur = mMediaPlayer.getCurrentPosition();
            int pos = (dur > 10000 ? dur - 5000 : 0);
            mMediaPlayer.seekTo(pos);
            break;
        case R.id.fwd:
            int curpos = mMediaPlayer.getCurrentPosition();
            int dur2 = mMediaPlayer.getDuration();
            int pos2 = (curpos + 5000 > dur2 ? dur2 : curpos + 5000);
            mMediaPlayer.seekTo(pos2);
            break;
        }
    }

    @SuppressWarnings("deprecation")
    private void notificationShow() {
        Context context = getApplicationContext();
        CharSequence contentTitle = "Mulugu Running";
        CharSequence contentText = url + " Weekly Predictions";

        Intent notifyIntent = new Intent(AudioView.this, AudioView.class);
        PendingIntent intent = PendingIntent.getActivity(AudioView.this, 0,
                notifyIntent, android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText,
                intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
    }

    private void changePlayerIcons(boolean b) {
        if (b) {
            mPlay.setVisibility(android.view.View.INVISIBLE);
            mPause.setVisibility(android.view.View.VISIBLE);
        } else {
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
        }
    }

    /*public class LoginProgress extends AsyncTask<Void, Void, Void> {

        private ProgressDialog dialog;
        protected Context applicationContext;

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            this.dialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            this.dialog = ProgressDialog.show(applicationContext, "Data Loading",
                    "Loading .....", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            LoadUrl();
            return null;
        }
        private void LoadUrl() {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.reset();
            try {
                mMediaPlayer.setDataSource(AudioView.this,Uri.parse(FinalConstants.URL + "mobile-audio/" + getIntent().getType()+ ".mp3"));
                mMediaPlayer.setLooping(false); // Set looping
                mMediaPlayer.prepare();
                myProgressBar.setMax(mMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }*/
}
SRINIVAS RAO
  • 219
  • 1
  • 4
  • 20
  • can you explain what you exactly need? I do not seem to get it – Sherif elKhatib Dec 19 '12 at 23:48
  • I need to get the text from a list view dynamically. Suppose I have a list view with 12 different names connecting to an audio player and I want to display which name is selected in a Marquee in the audio player screen. Is it possible. – SRINIVAS RAO Dec 20 '12 at 04:56
  • You want to get the name that shows on the item that the user have selected? :$ Or are you more interested in implementing a marquee? – Sherif elKhatib Dec 20 '12 at 07:08
  • The name of the item selected with scrolling functionality – SRINIVAS RAO Dec 20 '12 at 07:32
  • hope this may help you .. [add-marquee-effect](http://stackoverflow.com/questions/12525760/how-to-add-marquee-effect-to-listview) [display-auto-scorlling-text](http://myandroidtipsandtricks.blogspot.in/2012/03/how-to-display-auto-scorlling-text.html) – Surinder ツ Dec 26 '12 at 04:33

5 Answers5

3

If you need to move text around, maybe this will help:

http://developer.android.com/guide/topics/resources/animation-resource.html#translate-element

If you have a TextView with a text that is too big and you want it to move around so people can read the whole thing, try android:ellipsize="marquee" in the .xml file, or in the Java code: textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);

ralpo08
  • 81
  • 1
  • I need to get the text from a list view dynamically. Suppose I have a list view with 12 different names connecting to an audio player and I want to display which name is selected in a Marquee in the audio player screen. Is it possible. – SRINIVAS RAO Dec 20 '12 at 04:55
1

If the android libraries not enough, you can always use the java via coding. Create a thread class that gets the views and change conditions over time. You can create an intent to call that class.

Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35
1

You can use the TextSwitcher and apply the animation on it.

Try below code:

public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory,
    View.OnClickListener                    
  {
private TextSwitcher m_switcher;
private Button m_nextButton;
private int m_counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m_switcher = (TextSwitcher) findViewById(R.id.tsSwitcher);
    m_switcher.setFactory(this);

    Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
    Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
    m_switcher.setInAnimation(in);
    m_switcher.setOutAnimation(out);

    m_nextButton = (Button) findViewById(R.id.next);
    m_nextButton.setOnClickListener(this);
    updateCounter();
}

/**
 * Update counter value
 */
public void onClick(View v)
{
    m_counter++;
    updateCounter();
}

/**
 * Set text on textswitcher
 */
private void updateCounter()
{
    m_switcher.setText(String.valueOf(m_counter));
}

public View makeView()
{
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    t.setTextSize(36);
    return t;
}

You can define your own animations to get the marquee effect.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Here is the example

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/mywidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:lines="1"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textColor="#ff4500"
    android:text="Simple application that shows how to use marquee, with a long text" />

In java (If you are using a Listview and binding an adapter, then in getview when u inflate Layout and its view component, set the selected flag to true to your TextView.)

tv = (TextView) this.findViewById(R.id.tv);  
    tv.setSelected(true);  
Suresh
  • 7,785
  • 1
  • 16
  • 28
  • Sorry man this is just for marquee. I need the text selected in list view to be scrolling. – SRINIVAS RAO Dec 24 '12 at 19:49
  • The answers out there from other developers seems to be fine for marquee with many interesting solutions. But the requirement is still not clear enough. Would you like to put some light on it. Provide us an example. Add an image with some hints or some code snippets to make it clear. Thanks – Ajay Kumar Meher Dec 26 '12 at 04:48
  • Ok , So tell me how your list view looks like? is it a normal list view with Text only or it is a list view with custom row? If it is a simple listview with ArrayAdapter then you may have used a TextView from layout and passed an ID of that, if it is correct then you can use above solution. Anyways, good luck. – Suresh Dec 26 '12 at 06:47
0

By using below method you can convert your list of string values into a single string,

String listString = String.join(", ", list);

This will convert list values into string and with that you can achieve this. I know this is very late but I posted because there is no solution provided that's why.

Gokul
  • 53
  • 6