how to get TextViews from .xml to Java file - java

<TableRow
android:id="#+id/Row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/cell31"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
<TextView
android:id="#+id/cell32"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
.... this is 5x5 row column table. with 25 cells in it.
My .xml file looks like this. I have bunch of rows in LinearLayout contains TableLayouts which contains TextViews as cells. I am making a basic game with them.
I am trying to assign each cell from .xml to TextView array in .java file. But i can't figure out how to make it. I need to change color of each cell based on some algorithm i made so i want to access of cells.
private TextView colorBoard[][];
this is my .java array.
I can check if they are clicked with
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
But i only can assign which cell clicked at moment.In my game color of cells change differently. Not the one you just clicked. Assume you clicked 1x3 and 4x4 may change colors.
I'm new to android and this is my first project so sorry if i'm missing something basic.

I think you should use ListView instead of TextView. In ListView you can use an array for assigning those things where-ever you want to use them.
You can create layout file like this:
<ListView>
android:weight="match_parent"
android:height="match_parent"
android:id="+id/lst_view"
</ListView>
And Your Java Code looks like this
String[] array={"stack","queue","arraylist"};
ListView lst;
//In onCreate function
lst=(ListView)findViewById(R.id.lst_view);
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
lst.setAdapter(adpt);
//function of listview
//arrayadapter is used for converting string type array into listview
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s;
s=((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
});

If you want to assign values to TextView Array.
Then do it like this in oncreate method after setContentView
colorBoard[0][0] = (TextView) findViewById(R.id.cell11);
colorBoard[0][1] = (TextView) findViewById(R.id.cell12);
You can use findviewbyId to get the TextView from xml to Java.
Suppose if you want to access cell44 when you tap on cell11, then you could do something like this.
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
colorBoard[0][0].setBackground()//As you wish
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
Once you have access to cell44 you can set the background color or any other properties to it.

Related

How to get a String value in an Object from a List?

I want to get a String value from an Object that consists of an ImageView and two TextView in my List by clicking on that specific Object. But the problem is, I cannot get the String that I want.
The toast message should show the value of second TextView but it shows whatever it likes.
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical"
android:onClick="chitChat" >
<ImageView
android:id="#+id/photoImageView"
android:layout_width="300dp"
android:layout_height="200dp"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Name" />
<TextView
android:id="#+id/uidTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
If you have any other suggestion that helps me to get any Object specific value from clicked Object, I would like to hear that.
Thank you very much in advance.
public void chitChat(View view) {
TextView otherUid = findViewById(R.id.uidTextView);
String othersUid = otherUid.getText().toString();
Toast.makeText(PartyActivity.this, othersUid, Toast.LENGTH_LONG).show();
if (user != null && user.isEmailVerified()) {
Intent myIntent = new Intent(PartyActivity.this, ChatActivity.class);
myIntent.putExtra("Other Uid", othersUid);
PartyActivity.this.startActivity(myIntent);
}
else {
//...
}
}
Replace below line
TextView otherUid = findViewById(R.id.uidTextView);
to:
TextView otherUid = view.findViewById(R.id.uidTextView);
It should work
...
Hi Sam,
You need to set the text of the second TextView before getting it.
You can do this adding android:text to the second textview in the xml to have static text (the default value).
To have dynamic text into your second textview just set the text when you are creating the element of the List.
Hope this help.
Cheers

Horizontal scroll and focus on the last number in a TextView

I am doing a calculator app.
I have a TextView where the user writes the numbers to elaborate.
The TextView is inside a HorizontalScrollView. The HorizontalScrollView is inside a vertical LinearLayout. The TextView is single line, and, when the user writes a long number, the number comes out of the screen.
The TextView should follow the last number added by the user, so he can view the last numbers added without scrolling the screen. I don't want an autoscroll function that repeat the scroll every second, i only want focus on the last number added.
This is the TextView
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/numberDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:hint="0"
android:scrollHorizontally="true"
android:maxLines="1"
android:layout_gravity="right"
android:textSize="70sp" />
</HorizontalScrollView>
Try this:
In your activity create variables for your scroll view and text view:
TextView textView;
HorizontalScrollView scrollView;
Initialize them in onCreate():
scrollView = (HorizontalScrollView) findViewById(R.id.scrollView); // Add this id to xml
textView = (TextView) findViewById(R.id.numberDisplay);
Each time you update text of your text view update scroll position of the scroll view:
scrollView.scrollTo(textView.getRight(), textView.getTop());
Update:
I realized that if you call textView.setText(...) and then immediately scrollView.scrollTo(...) it does not work since text view size is not updated immediately. You should rather update scoll position like this:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(textView.getRight(), textView.getTop());
}
});

Textview still visible even if listview gets an item

I have an imageview, 2 textviews that needs to appear when the listview is empty. I want the 2nd textview to be clickable
as well. Here is my code -
<ImageView
android:id="#android:id/empty"
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/flower" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="120sp"
android:text="Warning" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="200sp"
android:text="Click Me" />
The image view disappears when the list view gets an item but the text views are still visible.
How can I make these 2 textviews disappear when the list view gets an item? Also how can I make my second text view clickable?
You can easily make them invisible. In your activity do this when you add an item to the list:
TextView tvXY = (TextView) findViewById(R.id.[TheIdHere]);
tvXY.setVisibility(View.GONE);
In your xml file you will have to change the id of the textviews too. Choose different ids for both of them. You can change the id by editing this:
#android:id/empty
To something like this.
#+id/[TheIdHere]
To make textView clickable you can setOnClickListener to that textView like this:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Get the ID of clicked radio button in android

Trying to get the id of the radio button that is checked in android, this is my XML code,
<RadioButton
android:id="#+id/RadioAuction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:onClick="showAuctionOptions"
android:textColor="#3DCC00"
android:text="#string/RadioButton1" />
<RadioButton
android:id="#+id/RadioBin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#FF0000"
android:text="Buy it now" />
so once it has been clicked it runs showAuctionOptions, this is my java code,
public void showAuctionOptions(View v){
if(findViewById(R.id.v=="RadioAuction")){
//Display start price
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
}
However this doesn't work, does anyone know why? Thanks.
Try
if (v == findViewById(R.id.RadioAuction)){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
If this doesn't cut it either, make sure you've set the on click listeners for those radio buttons.
Probably faster would be:
if (v.getId() == R.id.RadioAuction){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
And that way you can use the R values in a switch statement, if you have a long radio list:
switch (v.getId()) {
case R.id.RadioAuction:
// Do stuff
...
}

Android - How to get all the childrens under the custom listview even if we perform scrolling

I have a custom listview adapter with a checkbox,imageview, textview. and i also have a button and a checkbox(for select all) in my main layout(not in listview).
What here i want is to check all these listview checkboxes at once when i check my main layout's checkbox.And I want to get that related textview id as dynamically.
i tried few techniques using getChaildAT(i).but it's not working with my code.with this i'm able to check/uncheck only particular visible group of checkboxes.While scroll down to list it get failed.
My aim is to check/uncheck all the checkboxes even with scroll and get the related id's.So Pls provide any other way to get all the children count in that custom list view.
In main activity I've custom adapter code like this
filterlist.setAdapter(new CustomAdptr(this));
class CustomAdptr extends ArrayAdapter {
Activity context;
String[] eventtypename;
CustomAdptr(Activity context) {
super(context, android.R.layout.simple_list_item_multiple_choice, filterListdata);
rowView.clear();
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View viewrow = convertView;
if (viewrow == null) {
LayoutInflater inflater = context.getLayoutInflater();
viewrow = inflater.inflate(R.layout.filterlistrow, null);
}
ImageView iw = (ImageView)viewrow.findViewById(R.id.img);
try {
String image = filterListdata.get(position).getFilterImg();
byte[] imbbyte = com.cincinnati.parks.Base64.decode(image);;
Bitmap bmp=BitmapFactory.decodeByteArray(imbbyte,0,imbbyte.length);
iw.setImageBitmap(bmp);
}catch (Exception e) {
e.printStackTrace();
}
TextView tv = (TextView)viewrow.findViewById(R.id.title);
tv.setText(filterListdata.get(position).getFilterImageName());
final CheckBox chkbx = (CheckBox)viewrow.findViewById(R.id.checkbox);
chkbx.setChecked(true);
TextView selectedid = (TextView)viewrow.findViewById(R.id.rid);
selectedid.setText(String.valueOf(filterListdata.get(position).getFilterImgId()));
rowView.add(viewrow);
viewrow.setClickable(true);
viewrow.setFocusable(true);
checkboxImagelist.clear();
for(int i=0;i<filterListdata.size();i++) {
checkboxImagelist.add(filterListdata.get(i).getFilterImgId());
}
chkbx.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (chkbx.isChecked()) {
FilterImageId.clear();
checkboxImagelistall.clear();
topOkbuttongray.setVisibility(4);
topOkbutton.setVisibility(0);
rb = (CheckBox) v;
FilterImageId.add(filterListdata.get(position).getFilterImgId());
checkboxImagelist.addAll(FilterImageId);
checkboxImagelistall.addAll(FilterImageId);
}else {
FilterImageId.clear();
FilterImageId.add(filterListdata.get(position).getFilterImgId());
checkboxImagelistall.remove(FilterImageId);
checkboxImagelist.removeAll(FilterImageId);
if((checkboxImagelist.size()) == 0){
topOkbuttongray.setVisibility(0);
topOkbutton.setVisibility(4);
}
if (filterListdata.size() != checkboxImagelist.size()) all.setChecked(false);
}
if (filterListdata.size() == heckboxImagelist.size()) all.setChecked(true);
}
});
return (viewrow);
}
}
The below code is for check all button
OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
int Id;
System.out.println("enter set all button");
rb = (CheckBox) v;
if(rb.isChecked()) {
System.out.println("enter set all button rb.isChecked()");
topOkbutton.setVisibility(0);
topOkbuttongray.setVisibility(4);
checkboxImagelistall.clear();
checkboxImagelist.clear();
//markAll(true);
for(int i=0;i<filterListdata.size();i++) {
Id = filterListdata.get(i).getFilterImgId();
checkboxImagelistall.add(Id);
checkboxImagelist.add(Id);
}
System.out.println("In enable condition filterlist.getChildCount()============="+filterlist.getChildCount());
System.out.println("In enable condition filterlist.getAdapter().getCount()============="+filterlist.getAdapter().getCount());
/*for(int i=0; i < filterlist.getAdapter().getCount(); i++){*/
for(int i=0; i < filterlist.getChildCount(); i++){
LinearLayout itemLayout = (LinearLayout)filterlist.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkbox);
//CheckBox cb = (CheckBox)filterlist.getAdapter().getView(i, null, null);
cb.setChecked(true);
TextView tv = (TextView)filterlist.getChildAt(i).findViewById(R.id.rid);
rowIdstr.add(tv.getText().toString());
}
}else {
System.out.println("enter set all button rb.isChecked() Not");
all.setChecked(false);
rb.setChecked(false);
topOkbutton.setVisibility(4);
topOkbuttongray.setVisibility(0);
System.out.println("In disable condition filterlist.getChildCount()============="+filterlist.getChildCount());
System.out.println("In disable condition filterlist.getAdapter().getCount()============="+filterlist.getAdapter().getCount());
//markAll(false);
for(int i=0; i < filterlist.getChildCount(); i++){
//CheckBox cb = (CheckBox)filterlist.getAdapter().getView(i, null, null);
LinearLayout itemLayout = (LinearLayout)filterlist.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkbox);
cb.setChecked(false);
rowIdstr.clear();
checkboxImagelistall.clear();
}
}
}
};
all.setOnClickListener(radio_listener);
And I used below .xml files for custom listing
for check all button in main layout I used this
<CheckBox android:id="#+id/radioselectall"
android:layout_width="30dp" android:layout_height="20dp"
android:layout_marginLeft="10dp" android:layout_marginTop="4dp"
android:layout_marginRight="10dp" android:checked="true" />
And the listview is
<ListView
android:id="#+id/listm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp" />
</LinearLayout>
And another .xml file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
>
<CheckBox
android:layout_width="40dp"
android:scaleType="centerCrop"
android:id="#+id/checkbox"
android:layout_height="40dp"
android:layout_marginTop="4dp"
android:checked="true"/>
<ImageView
android:id="#+id/img"
android:scaleType="centerCrop"
android:layout_marginLeft="30dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="left"/>
<TextView
android:layout_width="150dp"
android:scaleType="centerCrop"
android:id="#+id/title"
android:layout_height="fill_parent"
android:textStyle="normal"
android:layout_marginTop="2dp"
android:layout_marginLeft="30dp"
android:textColor="#color/white"
android:textSize="15sp"
/>
<TextView
android:layout_width="wrap_content"
android:id="#+id/rid"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"
android:visibility="invisible"
/>
</LinearLayout>
There's no way to do that except storing the states in data underlying your adapter items, because there is actually finite number of recyclable items in a list view, it's significantly smaller than the possible number of data items.
So, my suggestion is either store an ArrayList of Booleans corresponding to the items in your filterListdata or add the booleans to the items stored in filterListdata. And then in getView() set checked state of the checkbox according to the boolean checked flag of the item.
This way, when you need to check all checkboxes in the ListView, you can run through all the items and set all checked flags to true, then call notifyDataSetChanged() to update visible items. And, of course, you'll have to set the checked flag in your OnClickListener for check box.
I'm not sure about what you mean by getting item ids though. Again, you can traverse your data array and for each item know whether it's checked or not.
Couple more comments:
Consider using OnCheckedChangeListener and the corresponding setter instead of OnClickListener for checkbox
Consider using only one listener instance for all checkboxes. Now you're creating a new one for each item. If the user ever wants to scroll your list fast, the app will have to create a massive load of useless new objects in the process, which will choke the GC (it'll have to constantly perform garbage collection) and the whole experience will be very chunky.
Use ViewHolder pattern to avoid creating bitmaps each time a new item needs to be displayed. This, too, can be a dramatic performance bottleneck.
After marking all checkboxes as true/false, call invalidate(); on list. This will refresh all the elements (visible or non-visible) in the list with latest properties set.
Try out.

Categories

Resources