Circular image in imageview from it's ID - android

I need to make the image that I get from database circular
I have made this code but I don't know how to complete it to make the image circular
please help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
name = (TextView) findViewById(R.id.names);
date = (TextView) findViewById(R.id.dates);
Story = (TextView) findViewById(R.id.story);
im = (ImageView) findViewById(R.id.img_car);
im.setScaleType(ImageView.ScaleType.FIT_XY);
//ScrollView scroller = new ScrollView(this);
//scroller.addView(Story);
Bundle extras = getIntent().getExtras();
int idf = extras.getInt("index");
Book selectedBook;
db = new SQLhelper(getApplicationContext());
// read the book with "id" from the database
selectedBook = db.readBook(++idf);
name.setText(selectedBook.getTitle());
date.setText(selectedBook.getdate());
Story.setText(selectedBook.getstory()+"\n \n ");
pname = selectedBook.getTitle();
test = selectedBook.getimage();
int id = getResources().getIdentifier("com.example.tarekhy:drawable/" + test, null, null);
im.setImageResource(id);
}
and this is xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#drawable/customborder">
<ImageView
android:id="#+id/img_car"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:contentDescription="#null"
android:gravity="center_vertical"
android:onClick="viewbig"
/>
<RelativeLayout
android:id="#+id/imgs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/layout_car_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/names"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:text="gfhbj"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_car_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_car_model"
android:orientation="horizontal" >
<TextView
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ScrollView android:id="#+id/textAreaScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_x="0px"
android:layout_y="25px"
android:scrollbars="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="#+id/fbedittext"
android:layout_marginTop="22dp"
android:id="#+id/story"
android:textStyle="bold"
android:layout_marginBottom="20px"
/>
</ScrollView>
"
</LinearLayout>
I think that I need to make a style
I tried to do this but I failed
Help please

You Need CircularImageView From CircularImageView
in xml file You have To Replace this code instead of Imageview :-<de.hdodenhof.circleimageview.CircleImageView
android:id ="#+id/img_car"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
app:border_width="2dp"
app:border_color="#color/dark" />
In Activity File Replace This Code instead of Imageview:-
CircleImageView im= (CircleImageView) findViewById(R.id.img_car);

Related

setText TextView in Fragment does not visualy show update

I have a fragment which has a bundle passed to it. The bundle come through (validated with Log tags) and the textView update within the code (I know this because the I Log getText after the TextView is supposed to be updated). My problem is that the visual representation (the screen) doesn't show the update.
numberView = (TextView) getActivity().findViewById(R.id.textView20);
//From CantactLog()
Bundle arguments = getArguments();
if ( arguments!= null) {
sName = getArguments().getString("sName");
sNumber = getArguments().getString("sNumber");
if (sNumber != null){
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
String formatedNumber = null;
try {
Phonenumber.PhoneNumber numberProto = phoneUtil.parse(sNumber, "US");
//Since you know the country you can format it as follows:
formatedNumber = phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
Log.i(TAG, " Formated Number " + formatedNumber);
sNumber= formatedNumber;
numberView.setText(sNumber);
// mPhoneSearch.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
scallName = sName;
Log.i(TAG, "sNumber = "+sNumber);
Log.i(TAG,"get text =" + numberView.getText().toString());
}
}
Log out put:
I/GifHeaderParser: Formated Number (650) 555-1212
I/GifHeaderParser: sNumber = (650) 555-1212
I/GifHeaderParser: get text =(650) 555-1212
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_background"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical"
android:tag="landingFragmentTag">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:id="#+id/phoneSearch222"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#drawable/edit_text_box"
android:ems="10"
android:hint="#string/search_by_phone_number"
android:inputType="phone"
android:lines="1"
android:maxLength="14"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Choose from Contacts" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="25dp"
android:layout_weight="1"
android:orientation="vertical">
<Spinner
android:id="#+id/call_log"
style="#android:style/Widget.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/edit_text_box"
android:minHeight="50dp"
android:spinnerMode="dialog" />
</LinearLayout>
<LinearLayout
android:id="#+id/animatedLayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:orientation="vertical">
<Button
android:id="#+id/PhoneSearch"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:backgroundTint="#android:color/holo_orange_light"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="#string/search_phone_number"
android:textColor="#color/cardview_dark_background"
android:visibility="visible" />
</LinearLayout>
So I answered my own question, when I was calling the fragment to open, I was using .add instead of .replace in the fragment transfer.

getChildAt() InvocationTargetException

In xml of row_layout I've set a delete button as follows
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:background="#ffffff"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingLeft="10sp"
android:weightSum="100" >
<LinearLayout
android:id="#+id/Layout"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_weight="90"
android:orientation="vertical" >
<TextView
android:id="#+id/firstTextView"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10sp"
android:gravity="top"
android:text="Activity"
android:textColor="#000" />
<TextView
android:id="#+id/firstTextView2"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="top"
android:text="Sub-Activity"
android:textColor="#000" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/edit" />
<ImageView
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onClickDelete"
android:src="#drawable/neg" />
</LinearLayout>
here is onClickDelete method
public void onClickDelete(View v)
{
ListView lvItems = getListView();
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView child = (TextView) vwParentRow.getChildAt(0);
child.setText("Delete Clicked");
}
When i press delete, it shows invocationtargetexception at (TextView child = (TextView) vwParentRow.getChildAt(0);)
Sorry if this is very basic mistake. I am new to android.
Thank you for your reply.
It's better
TextView child = (TextView)findViewById("yourtextfieldIDHere");
child.setText("Delete Clicked");

Android - Capture ScreenShot Programmatically without title bar

I am developing an app in that I want to take a screen shot of current screen but with out title bar. I know the code to Capture a ScreenShot, but unable to customize it.
Code:
munchscreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
When I click on Clip then it should capture a screen shot of the screen shown below but should not include the part which I have circled..
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:background="#D8D5AA"
>
<LinearLayout
android:id="#+id/clip_from_web_linearlayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header"
android:orientation="horizontal"
>
<TextView
android:id="#+id/clip_from_web_textview_back"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Back"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_header_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip From Web"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
<TextView
android:id="#+id/clip_from_web_textview_clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clip"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
android:layout_below="#+id/clip_from_web_linearlayout1"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:layout_below="#+id/munchscreen"
android:contentDescription="#string/app_name"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/clip_from_web_bottom_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/header" >
<Button
android:id="#+id/clip_from_web_previous"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:background="#android:drawable/ic_media_previous"
android:layout_marginTop="2dp"
/>
<Button
android:id="#+id/clip_from_web_reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:drawable/ic_menu_rotate"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/clip_from_web_next"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toRightOf="#+id/clip_from_web_reload"
android:layout_marginLeft="102dp"
android:background="#android:drawable/ic_media_next"
android:layout_marginTop="2dp"
/>
</RelativeLayout>
Image before Capturing screen shot..
Image after Capturing screen shot..
Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"
/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.
try this...
munchscreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
contentView.setDrawingCacheEnabled(true);
contentView.buildDrawingCache();
Bitmap drawingCache = contentView.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), drawingCache);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
Hey friends I got the solution of above problem.
Follow below steps:-
activity_main.xml
<RelativeLayout
android:id="#+id/toolbar_rl"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#color/header_color"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="0"
tools:ignore="UselessParent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_back_24" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="#dimen/_10sdp"
android:layout_marginStart="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/one_time"
android:textColor="#color/white"
android:textSize="#dimen/_13sdp"
tools:ignore="RtlHardcoded" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:padding="#dimen/_8sdp"
android:src="#drawable/ic_menu_icon" />
</LinearLayout>
</RelativeLayout>
<ScrollView
android:id="#+id/tabScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="210dp"
android:adjustViewBounds="true"
android:background="#drawable/otp_background"
android:contentDescription="#string/app_name"
android:scaleType="fitXY">
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_16sdp"
android:background="#fff"
android:gravity="center"
android:text="Enter Unique Identification Number"
android:textColor="#color/header_color"
android:textSize="#dimen/_16sdp" />
<LinearLayout
android:id="#+id/layout_otp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/_26sdp"
android:gravity="top|center"
android:orientation="horizontal">
<EditText
android:id="#+id/editTextone"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="number"
android:maxLength="1"
android:nextFocusRight="#+id/editText_two"
tools:ignore="LabelFor">
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTexttwo"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextthree"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfour"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
<EditText
android:id="#+id/editTextfive"
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_42sdp"
android:inputType="numberDecimal"
android:maxLength="1"
tools:ignore="LabelFor" />
</LinearLayout>
<Button
android:id="#+id/btn_submit"
android:layout_width="#dimen/_220sdp"
android:layout_height="#dimen/_40sdp"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/_46sdp"
android:layout_marginLeft="#dimen/_16sdp"
android:layout_marginRight="#dimen/_16sdp"
android:layout_marginTop="#dimen/_26sdp"
android:background="#drawable/editbox_one"
android:text="#string/capture"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="#dimen/_16sdp"
android:typeface="serif" />
</TableLayout>
</ScrollView>
This xml looks like.
in MainActivity Add some code.
3.1 Declare the Button.
Button button_submit;
3.2 Initialize the button.
button_submit = (Button) findViewById(R.id.btn_submit);
3.3 Set set OnClickListener to this button and Call one method i.e
takeScreenshot();
Note:- When you click on this button that time automatically capture the screen without title bar and it will store in file explore with current date, time and.jpg extension(Ex. Fri May 11 151938 GMT+0530 2018.jpg) and this captured image as a bitmap if you want you can set this bitmap to ImageView .
The Code of takeScreenshot().
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
View u = findViewById(R.id.tabScroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.tabScroll);
z.setBackgroundColor(getResources().getColor(R.color.white));
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
The output be looks like..
I hope it will helpful for you.

Android setText ar the Master Page of the Tab

I have got the following code to set the values of the EditText at the header bar (master flow) but it failes and no values are assigned.
Would you please tell me how setText ?
The below is my code
(My Master Activity)
#SuppressWarnings("deprecation")
public class MainActivity extends Activity
{
private AsyncTask <Void , Void, Void> mRegisterTask ;
private String userid;
private String orderid;
...
View inflatedView = getLayoutInflater().inflate(R.layout.activity_main, null);
final EditText s = (EditText) inflatedView.findViewById(R.id.textView2);
userid = "99999";
s.setText(userid);
activity_main
<RelativeLayout 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"
android:layout_margin="0dp"
android:background="#drawable/bg_with_header"
android:padding="0dp"
tools:context=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:padding="0dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:orientation="vertical"
android:padding="0dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="82dp"
android:layout_margin="0dp"
android:layout_marginRight="50dp"
android:orientation="horizontal"
android:padding="0dp" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:src="#drawable/header_tv1231231231231bnvlogo" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="82dp"
android:layout_gravity="center"
android:layout_toLeftOf="#+id/headerLogin"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="300dp"
android:layout_height="86dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:showDividers="none"
android:tabStripEnabled="false" />
</HorizontalScrollView>
<RelativeLayout
android:id="#+id/headerLogin"
android:layout_width="wrap_content"
android:layout_height="82dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/txtbg"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding=" 5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#BDBDBD" />
<EditText
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/textView1"
android:background="#drawable/txtbg"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#BDBDBD" />
<TextView
android:id="#+id/textVieasas2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textVieasas"
android:layout_alignBottom="#+id/textVieasas"
android:layout_toRightOf="#+id/textView2"
android:text="Last Login Date"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/textVieasas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/textView2"
android:text="Sales Code"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:padding="0dp" />
</LinearLayout>
</TabHost>
you can also simple use this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText s = (EditText) findViewById(R.id.textView2);
userid = "99999";
s.setText(userid);
}
Do this way
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText s = (EditText) findViewById(R.id.textView2);
userid = "99999";
s.setText(userid);
// or
View inflatedView = getLayoutInflater().inflate(R.layout.activity_main, null);
final EditText s = (EditText) inflatedView.findViewById(R.id.textView2);
userid = "99999";
s.setText(userid);
setContentView(inflatedView);
}

Android ImageView is raising OnClick event when is hidden

I have couple questions about android views. I'm newbie so if my question seems ridiculous please give me right hint :)
So I'm trying to make memory game. Here is my code of mainLayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android.background="Color.TRANSPARENT"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/top_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blaaaaaa1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blaaaaaa2" />
</LinearLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="3"
android:inputType="textPassword"
android:text="dsdsdsd"
android:visibility="invisible" >
<requestFocus />
</EditText>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/top_panel"
android:paddingBottom="50dp"
android:paddingTop="5dp"
android:weightSum="5" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_a4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_a5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_d1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_d2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_d3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_d4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_d5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/pic_e5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/pic9214" />
</RelativeLayout>
and here is code of mainActivity.
public class MainActivity extends Activity {
private OnClickListener paSharedOnClickListener = new OnClickListener();
private ImageView paPreviewView ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setBackgroundDrawableResource(R.drawable.old_paper_r);
ImageView img;
img = (ImageView) MainActivity.this.findViewById(R.id.imageView1) ;
img.setVisibility(-1);
this.paPreviewView = img;
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_a1);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_a2);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_a3);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_a4);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_a5);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_b1);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_b2);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_b3);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_b4);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_b5);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_c1);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_c2);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_c3);
img.setVisibility(-1);
img = (ImageView) findViewById(R.id.pic_c4);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_c5);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_d1);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_d2);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_d3);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_d4);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_d5);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_e1);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_e2);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_e3);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_e4);
img.setOnClickListener(paSharedOnClickListener);
img = (ImageView) findViewById(R.id.pic_e5);
img.setOnClickListener(paSharedOnClickListener);
}
private class OnClickListener implements View.OnClickListener
{
private boolean paIsPreviewShowed = false;
public void onClick(View arg0)
{
ImageView img = (ImageView)arg0;
Log.v("Click", String.format("ID %s %s", img.getId() , (ImageView) MainActivity.this.findViewById(R.id.imageView1) == img ) );
if (!this.paIsPreviewShowed)
{
this.paIsPreviewShowed = true;
Animation fadeIn = new AlphaAnimation( 0, 1);
fadeIn.setDuration(150);
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnim = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleAnim.setInterpolator(new LinearInterpolator());
scaleAnim.setRepeatCount(Animation.ZORDER_NORMAL);
scaleAnim.setDuration(250);
TranslateAnimation move = new TranslateAnimation(0, 0, 500,0);
move.setDuration(250);
animationSet.addAnimation(fadeIn);
animationSet.addAnimation(scaleAnim);
animationSet.setFillAfter(true);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.pic9012);
if (paPreviewView.getParent()==null )
{
RelativeLayout top_panel = (RelativeLayout) MainActivity.this.findViewById(R.id.mainPanel);
top_panel.addView(paPreviewView);
}
paPreviewView.setImageBitmap(bMap);
paPreviewView.startAnimation(animationSet);
}else
{
Animation fadeOut = new AlphaAnimation( 1, 0);
fadeOut.setFillAfter(true);
fadeOut.setDuration(250);
paPreviewView.startAnimation(fadeOut);
this.paIsPreviewShowed = false;
((RelativeLayout) paPreviewView.getParent()).removeView(paPreviewView);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Ok so, in first step after create Activiti I'm setting up OnClickListener to every card (pic_a1, pic_a2 ... pic_e5) and visiblity of ImageView paPreviewView to invisible (where i wanna show image of selected card). And then when i clicked to pic_a1 is paPreviewView showed. This is ok.
My questions are.
1) In my early version of this example I hid paPreviewView ImageView by calling
paPreviewView.setVisibility(View.INVISIBLE);
or
paPreviewView.setVisibility(View.GONE);
but it didn't work properly. Because method OnClickListener.onClick was called only from hidden paPreviewView ImageView. So i tried to remove OnClickListener from paPreviewView but it didn't help. Only working solution is remove (for hidding ) and add (for showing) paPreviewView from /to mainPanel.
My question is why? Why is paPreviewView raising onClick event when paPreviewView is hidden?
2) And my second question is. Is better solution than I made for this example (I mean card game) ?
Thanks a lot for any idea :)
The correct function to use in this case would be:
paPreviewView.setClickable(false);
ImageView.setVisibility() only changes the "visibility" of the view and it still has the listener registered.
You can use:
paPreviewView.setEnabled(false);
to not receive the click events.

Categories

Resources