Hello Friends'
Here is a simple example of full screen dialog.
Two layout is used
One is main layout and other is dialog layout.
activity_main Layout file code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="@+id/dialogTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textSize="18sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:padding="5dp"
<--setting backgroung from drawable folder-->
android:background="@drawable/rectanguler_background"
android:text="Click Here For Full Screen Dialog" />
</LinearLayout>
full_screen_dialog_layout code
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue"
android:gravity="center_vertical" >
</LinearLayout>
<--used edit text to add search functionality-->
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="2dp"
<--setting backgroung from drawable folder-->
android:background="@drawable/rectanguler_background"
android:hint="Search..."
android:padding="5dp" />
<--List View to display list item-->
<ListView
android:id="@+id/cityListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
rectanguler_background file put this file in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="1dp"
android:top="1dp"
android:right="1dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
MainActivity java file code
package in.blogspot.longjamcode;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private TextView dialogTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialogTextView = (TextView) findViewById(R.id.dialogTextView);
dialogTextView.setOnClickListener(clickListener);
}
private OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.DeviceDefault_Light_ButtonBar); // making dialog full screen
//inflating dialog layout
dialog.setContentView(R.layout.full_screen_dialog_layout);
ListView cityListView = (ListView) dialog.findViewById(R.id.cityListView);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.city_array));
cityListView.setAdapter(adapter);
cityListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialogTextView.setText(parent.getAdapter().getItem(position).toString());
dialog.dismiss();
}
});
//adding search functionality
EditText searchEditText = (EditText) dialog.findViewById(R.id.searchEditText);
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
dialog.show();
}
};
}
Output:
For full source code click here
No comments:
Post a Comment