i have two activities in android studio.act1 with a button and act2 with an imageView. i want to click the button in act1 and make the image in act2 visible. and when i click button for second time, this time make the image invisible and do it again and again and again. how can i do that?
you must create this class;
public class PublicSharedPreferences {
public static void setDefaults(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
}
and then learn sharedpreferences enter link description here
Activity1;
public class Activity1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr != null) {
if (visibilityStr.equals("0")) {
Toast.makeText(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
visibilityStr = "1";
} else {
visibilityStr = "0";
Toast.makeText(Activity1.this, "it invisibled", Toast.LENGTH_SHORT).show();
}
} else {
visibilityStr = "1";
Toast.makeText(Activity1.this, "it visibled", Toast.LENGTH_SHORT).show();
}
PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
Intent intent = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(intent);
}
});
}
}
Activity2;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ImageView imgView = (ImageView) findViewById(R.id.imgView1);
String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());
if (visibilityStr.equals("0"))
imgView.setVisibility(View.INVISIBLE);
else
imgView.setVisibility(View.VISIBLE);
}
}
Layout1;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.c.a.myapplication.Activity1">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
Layout2;
<?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">
<ImageView
android:id="#+id/imgView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_camera"
android:visibility="invisible"/>
</LinearLayout>
Its work.
Try this code....
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Sample extends Activity {
ImageView img;
Button btn;
boolean clicked = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
btn = (Button) findViewById(R.id.t1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked = true;
Intent intent = new Intent(Sample.this, Dample.class);
intent.putExtra("value", true);
startActivity(intent);
}
});
}
}
//Dample.class In second activity
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
public class Dample extends Activity {
ImageView img;//use image view
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pimple);
img= (ImageView) findViewById(R.id.t1);
Boolean yourBool = getIntent().getExtras().getBoolean("value");
if (yourBool == true) {
img.setVisibility(View.VISIBLE);///use visibility code for imageview as mentioned above
}
}
}
How do I get the line in sync with the round ball images and how to change the text and image when clicked ?
I'd like to do something like this:
https://www.behance.net/gallery/13564677/Blog
I got the ListView but I just need help in building the straight line pass through the round ball images.
Thanks for your help!
My Code Follows:
My activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3d3d3"
>
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:divider="#null"
android:layout_gravity="left"
android:background="#A55676"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
custom_row.xml
?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="horizontal"
android:paddingLeft="35dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:background="#drawable/border"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="-5sp"
android:paddingTop="30dp"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="25dp"
android:drawablePadding="20dp"
android:paddingLeft="3dp"
android:paddingTop="25dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listview;
private ActionBarDrawerToggle drawerListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView) findViewById(R.id.drawerList);
drawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout);
MyAdapter myAdapter = new MyAdapter(this);
listview.setAdapter(myAdapter);
drawerLayout.setDrawerShadow(R.drawable.navbar_shadow, Gravity.LEFT);
drawerListener = new ActionBarDrawerToggle(this, drawerLayout,R.drawable.ic_drawer,R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerClosed(View drawerView)
{
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Drawer Closed", Toast.LENGTH_LONG).show();
}
#Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Drawer Opened", Toast.LENGTH_LONG).show();
}
};
drawerLayout.setDrawerListener(drawerListener);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setIcon(R.color.transparent);
listview.setOnItemClickListener(this);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}
public void selectItem(int position)
{// TODO Auto-generated method stub
listview.setItemChecked(position, true);
//setTitle(planets[position]);
}
public void setTitle(String title)
{
getActionBar().setTitle(title);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(drawerListener.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{ // TODO Auto-generated method stub
ImageView titleImageView = (ImageView)view.findViewById(R.id.imageView1);
TextView titleTextView = (TextView)view.findViewById(R.id.textView1);
switch(position)
{
case 0:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState()){
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 1:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 2:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 3:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
Intent intent = new Intent(getApplicationContext(),Featured.class);
startActivity(intent);
break;
case 4:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
case 5:
if (titleImageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.ic_new).getConstantState())
{
titleImageView.setImageResource(R.drawable.ic_new1);
titleTextView.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
titleImageView.setImageResource(R.drawable.ic_new);
titleTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
}
break;
default:
break;
}
}
}
class MyAdapter extends BaseAdapter
{
String[] socialSites;
int [] images = {R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new,R.drawable.ic_new};
Context context;
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
socialSites = context.getResources().getStringArray(R.array.social);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return socialSites.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return socialSites[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row,parent, false);
}
else
{
row=convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);
titleTextView.setText(socialSites[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
Use a ListView, and assign a 9-patch drawable to each item's background which will consist of a centered dot and the vertical line.
The whole trick will be to specify the strechable regions of your nine-patch properly (stretch the line and not the dot).
Once you are happy with that, just make a different dot appearance for the active element and use an XML selector drawable to assign either the default or activated state 9 patch to the background.
I am using Imageview 2 times in my MainActivity.java class by using LayoutInflater.In that activity i am inflate so many layouts by using scroll every thing works fine.But this imageview is not working properly.When user click on imageview it shows popup.It works when i am show first time inflate.It's not working second time inflate.
In my main activity contain edittext.when user enter High in edittext i want to show 2 imageview times.If user enter Low i want to show single imageview.One imageview shows top of the screen and one shows bottom of the screen.
Here my bit of code.
This is my imageview.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/attach_photo_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="#string/attach_photo_to_job"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/image_placeholder" />
</RelativeLayout>
I am using this layout in my main.xml by using .
And i am call that imageview.xml layout in My MainActivity.java by using LayoutInflater.
Here is my code:
public void getAudioRecordLayout() {
testView = testInflater.inflate(
getResources().getLayout(
R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) findViewById(R.id.attach_photo_camera_ImageView);
mIncludeHighLayout = (LinearLayout) findViewById(R.id.include_High);
mIncludeLowLayout = (LinearLayout) findViewById(R.id.include_Low);
mIncludeRecordAudioInWorkDetailLayout.setVisibility(View.VISIBLE);
mIncludeRecordAudioInRecordOfInspectionLayout
.setVisibility(View.GONE);
mWorkDetailAttachImageView.setOnClickListener(this);
}
Edit # 1 :
When i am showing imageview second time that imageview shows inside another inflate view.
Image view OnclickListiener :
if (v == mWorkDetailAttachImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
Edit # 2
MainActivity.java
package rajesh.dropdowndemo;
import java.security.PublicKey;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
ImageView imagView;
LinearLayout commonIncludeLayout, includeHighLayout;
int i = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
// getHighLayout();
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0,
Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1,
Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2,
Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(final int aa) {
// LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// View view = inflater.inflate(R.layout.imageview, null);
RelativeLayout outer;
if (aa == 1)
outer = (RelativeLayout) findViewById(R.id.image_layout);
else
outer = (RelativeLayout) findViewById(R.id.image_layout1);
imagView = (ImageView) outer
.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
if (aa == 1)
imagView.setTag(1);
else
imagView.setTag(2);
LayoutInflater inflater = LayoutInflater
.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 200);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
// get Image
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag(1) == i)
((ImageView) imagView.getTag(1)).setImageBitmap(thumbnail);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(thumbnail);
} else if (resultCode == RESULT_CANCELED) {
System.out.println(imagView.getTag());
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
if (requestCode == 200 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bit = (Bitmap) BitmapFactory.decodeFile(picturePath);
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag() == i)
((ImageView) imagView.getTag(1)).setImageBitmap(bit);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/high_layout"/>
</LinearLayout>
</RelativeLayout>
imageview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/background_dark" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="Attach Photo"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
dailog.xml
<?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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/take_new_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="Take New Photo" />
<Button
android:id="#+id/choose_from_existing_img_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Choose Existing" />
<Button
android:id="#+id/cancel_img_dialog_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</LinearLayout>
string.xml
<resources>
<string name="app_name">DropDownDemo</string>
<string name="alt0">High</string>
<string name="alt1">Low</string>
<string name="alt2">General</string>
</resources>
high_layout.xml
<?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" >
<include
layout="#layout/imageview"/>
</LinearLayout>
Please any one help me.
Try this..
testView = testInflater.inflate(getResources().getLayout(R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) testView.findViewById(R.id.attach_photo_camera_ImageView);
add in your click
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
}
});
Or
if (v.getId() == R.id.attach_photo_camera_ImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
EDIT
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
testView = inflater.inflate(R.layout.imageview, null);
EDIT 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include
android:id="#+id/image_layout"
layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include
android:id="#+id/image_layout1"
layout="#layout/imageview"/>
</LinearLayout>
</RelativeLayout>
JAVA
package rajesh.dropdowndemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
LinearLayout commonIncludeLayout,includeHighLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0, Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1, Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2, Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(int aa){
LinearLayout outer;
if(aa == 1)
outer = (LinearLayout) findViewById(R.id.image_layout);
else
outer = (LinearLayout) findViewById(R.id.image_layout1);
ImageView imagView = (ImageView) outer.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
}
Did you checked view in your onClick function like below..
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.attach_photo_camera_ImageView:
//your click code
break;
}
or try below code instad of your code..
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Where ever i click on ImageView the onclick method is not hapenning. i mean it is not redirecting to my main.xml
//package name : bunk
//My cesem.XML :
//just a Textview and a Image View
- Indented four spaces.
<?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="fill_parent"
android:padding="25dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Your Semester"
android:textSize="25dp"
android:gravity="center"
android:id="#+id/tvSemCe"
android:layout_marginBottom="20dp"
/>
<ImageView
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"
/>
</LinearLayout>
//Class file: Cesem.java
package com.bunk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class Cesem extends Activity implements OnClickListener{
ImageView back;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
setContentView(R.layout.main);
}
}
}
if (v.getId() == R.id.back)
instead of
if (v == back)
try to set the element as clickable:
back.setClickable(true);
Or maybe you can't see a feedback beacuse of the content of click callback (setContentView..)
Try to log something inside the click callback, such as
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
setContentView(R.layout.main);
}
}
Change your code as:
public class Cesem extends Activity implements OnClickListener{
ImageView back;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.back) {
Toast.makeText(getApplicationContext(), "Toast 1",Toast.LENGTH_SHORT).show();
Activity.this.setContentView(R.layout.main);
}
else
{
Toast.makeText(getApplicationContext(), "Toast 2",Toast.LENGTH_SHORT).show();
}
}
}
}
and register your Activity in manifest as:
<activity
android:name=".Cesem" />
I created a custom dialog with spinner and OK button. I have populated this spinner with some items and inflated the layout.If I click OK button dialog will dismiss.
I set the spinner
spinner.performCLick();
is there is any way to get spinner selected item and to close the dialog without pressing OK button. I have tried
button.performclick();
but no use.
see my below code it may help you.
package com.Test_dia;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class Test_diaActivity extends Activity {
private Button btn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showalert();
}
});
}
protected void showalert() {
// TODO Auto-generated method stub
final Dialog dia = new Dialog(this);
dia.setContentView(R.layout.dia);
final String a[] = { "select one", "android", "java", "php" };
Button btnok = (Button) dia.findViewById(R.id.button2);
Spinner spin = (Spinner) dia.findViewById(R.id.spinner1);
btnok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dia.dismiss();
}
});
spin.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, a));
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (arg2 > 0) {
Toast.makeText(Test_diaActivity.this,
"You Selected :" + a[arg2], Toast.LENGTH_SHORT)
.show();
dia.dismiss();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
dia.show();
}
}
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click here" />
</LinearLayout>
dia.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner1"
android:text="ok" />
This code is work for me perfectly ok.
enjoy....
EDIT (removed previous non-suitable answer)
I'm going to assume that your issue is that using setOnItemSelectedListener is firing 'onItemSelected' on startup (thus selecting the first item in the spinner without any user input) and you don't want that.
If that is the case, try the following.
Set a class variable:
private int newSpinner = 0;
Then in the setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
if (newSpinner != 0) {
// Do your code thing here
dismiss();
} else {
newSpinner++
}
}
});