Monday, December 24, 2012

ListView into ScrollView in Android

Hello Friends!
Today I am going to share very important code for ListView into ScrollView.
Many Android developers says: You should not put a ListView in a ScrollView because a ListView already a scrollview.

 But some times its really needed in project “ how can place a ListView into a ScrollView without it collapsing to its minimum height?

And finally I got it on stack over flow-

waste a lot of time in goggling so I don’t want some one do same so you can follow my blog 
step by step-

UPDATE: Please check my this post for in case any of issue-
http://www.androidhub4you.com/2014/03/android-listview-into-scrollview-issue.html

PrintScren:


Step1- Create an Activity name-MainActivity.java and create a listview.

package com.example.scrollviewlistviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
       private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
                     "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
       ListView myList;

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

              myList = (ListView) findViewById(R.id.listView);
              myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
              Helper.getListViewSize(myList);
      
       }
}
Step2- Create main.xml-
<ScrollView 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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="19dp"
            android:text="@string/hello_world" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_launcher" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</ScrollView>

Step3- Finally and important thing create another class Helper.java and put below given code-
package com.example.scrollviewlistviewdemo;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

Hope it will help you thanks!

Thursday, December 20, 2012

Custom Alert Dialog Title and Message Demo in Android


Hi Friends!
      Today I am sharing simple code for custom alert message and title demo...

1)Print Screen:

2)Code:
package com.example.customalertmessagedemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Creates textview for centre title
TextView myMsg = new TextView(this);
myMsg.setText("Succesfully send!");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
myMsg.setTextSize(20);
myMsg.setTextColor(Color.WHITE);
//set custom title
        builder.setCustomTitle(myMsg);
        builder.setMessage("Check your email and verify your username and password...");
        builder.setPositiveButton("OK", null);
        AlertDialog dialog = builder.show();
        //Create custom message
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
    }
}



Thanks...