I want to get the editext value and bitmap image from the every view that has bean created dynamically.I am not getting how to use it.I have tried to get the details from view vx.But it's not happening.Here is my code.
Please help...
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
View addView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> value=new ArrayList<String>();
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
Button button = (Button)findViewById(R.id.all);
container = (LinearLayout)findViewById(R.id.container);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0, c = container.getChildCount(); i < c; i++) {
View vx = container.getChildAt(i);
}
}
});
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.row, null);
EditText textOut = (EditText)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textOut.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
}
});
final ImageView buttonimg = (ImageView)addView.findViewById(R.id.remove);
buttonimg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//((LinearLayout)addView.getParent()).removeView(addView);
buttonimg.setImageResource(R.drawable.logo);
}});
container.addView(addView);
}});
}
}
For retrieving the text:
((EditText)vx.findViewById(R.id.textout)).getText()
I am posting the answer since I haven't got the answer about how to get the imagebitmap. This code is working
ImageView v2=((ImageView) vx.findViewById(R.id.remove));
Bitmap bm=((BitmapDrawable)v2.getDrawable()).getBitmap();
Related
I am making a application which use Search bar to filter item in list view. when user search and choose an item they want, application will display activity of that item.
I try to find solution but each time user select item they want to go. the application always shows the activity of the first item. can anyone please help me how to solves this issue. below is my java code:
BottomNavigationView bottomNavigationView;
ListView listView;
FloatingActionButton share;
MaterialSearchBar timkiembar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading);
listView = findViewById(R.id.Lv);
listView.setSelected(true);
share = findViewById(R.id.share);
timkiembar = findViewById(R.id.timkiembar);
String[] storyname = getResources().getStringArray(R.array.stories_names);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.row,R.id.row_txt,storyname);
listView.setAdapter(adapter);
timkiembar.addTextChangeListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
}
#Override
public void afterTextChanged(Editable s) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Reading.this, BilingualEssayDetails.class);
intent.putExtra("story_key",position);
startActivity(intent);
}
});
and below is code of BilingualEssayDetails.class:
translate = findViewById(R.id.translate);
dich = findViewById(R.id.dich);
nihon = findViewById(R.id.nihon);
paint = findViewById(R.id.paint);
layout = findViewById(R.id.layout);
txt_essay = findViewById(R.id.txt_essay);
colors= getSharedPreferences("MyColor",MODE_PRIVATE);
int colorcode= colors.getInt("color", 0);
if (colorcode!=0){
layout.setBackgroundColor(colorcode);
}
int position = getIntent().getIntExtra("story_key",0);
if (position ==0){
txt_essay.setText(""+getString(R.string.essay1));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1));
}
});
}
if (position ==1){
txt_essay.setText(""+getString(R.string.essay2));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2));
}
});
}
Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.
Code:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
Add text watcher outside click listener like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can do some click action here
}
});
}
In your code change these below lines only
#Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}
I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.
Hello need help with a class trying to figure out how to create a first and last button in a fragment that will jump to the first item in a ViewPager and the last item in a ViewPager I'm a bit lost with how to implement. Here is the fragment.
public class CrimeFragment extends Fragment {
private static final String ARG_CRIME_ID = "crime_id";
private static final String DIALOG_DATE = "DialogDate";
private static final int REQUEST_DATE = 0;
private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;
private Spinner mSpinner;
private Button mFirstButton;
private Button mLastButton;
public static CrimeFragment newInstance(UUID crimeId) {
Bundle args = new Bundle();
args.putSerializable(ARG_CRIME_ID, crimeId);
CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date =
(Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mCrime.setDate(date);
updateDate();
}
}
private void updateDate() {
mDateButton.setText(mCrime.getDate().toString());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.setText(mCrime.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(
CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(
CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button) v.findViewById(R.id.crime_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});
mSpinner = (Spinner) v.findViewById(R.id.crime_severity);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.severity_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
mCrime.setSeverity((String) mSpinner.getSelectedItem());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
mCrime.setSolved(isChecked);
}
});
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return v;
}
}
Here is the Activity with the ViewPager.
public class CrimePagerActivity extends FragmentActivity{
private static final String EXTRA_CRIME_ID = "com.martin.android.criminalintent.crime_id";
private ViewPager mViewPager;
private List<Crime> mCrimes;
public static Intent newIntent(Context packageContext, UUID crimeId) {
Intent intent = new Intent(packageContext, CrimePagerActivity.class);
intent.putExtra(EXTRA_CRIME_ID, crimeId);
return intent;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mViewPager = (ViewPager) findViewById(R.id.activity_crime_pager_view_pager);
mCrimes = CrimeLab.get(this).getCrimes();
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
#Override
public Fragment getItem(int position) {
Crime crime = mCrimes.get(position);
return CrimeFragment.newInstance(crime.getId());
}
#Override
public int getCount() {
return mCrimes.size();
}
});
for (int i = 0; i < mCrimes.size(); i++) {
if (mCrimes.get(i).getId().equals(crimeId)) {
mViewPager.setCurrentItem(i);
break;
}
}
}
}
For the First button Im pretty sure I just need to set item to 0? For last button I need to set item to size - 1 just not sure how to do that.
In your CrimePagerActivity, write two method's to jump to the ViewPager first page and last page.
CrimePagerActivity.java:
public void jumpToFirstPage() {
mViewPager.setCurrentItem(0);
}
public void jumpToLastPage() {
mViewPager.setCurrentItem(mCrimes.size() - 1);
}
Call those method's from mFirstButton and mLastButton click listener method:
CrimeFragment.java:
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToFirstPage();
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((CrimePagerActivity)getActivity()).jumpToLastPage();
}
});
Hope this will work perfectly.
Ok this was a answer that I was given by the teacher incase anyone is interested.
mFirstButton = (Button) v.findViewById(R.id.first_crime_button);
mFirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(0);
}
});
mLastButton = (Button) v.findViewById(R.id.last_crime_button);
mLastButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewPager pager = (ViewPager) getActivity().findViewById(R.id.activity_crime_pager_view_pager);
pager.setCurrentItem(CrimeLab.get(getActivity()).getCrimes().size()-1);
}
});
Create two button and on click set
viewPager.setCurrentItem(0); // for top
and
viewPager.setCurrentItem(mCrimes.size()); // for last
I have a problem in my codes in AutocompleteTextView. When I search , after I click the word that I have search it will display some text or letters in AutocompleteTextView .
This is my the main page of the app Main of the app
After I search the word . It will display this This is the error image
This is my code :
MainActivity.java
public class MainActivity extends Activity {
Button AN,PL;
AutoCompleteTextView autocomplete;
Button clear;
ListView lv;
SearchView search_view;
String[] animals_names;
ArrayList<Main> animalslist;
AnimalsAdapter adapter;
#SuppressWarnings("rawtypes")
Class[] classes = {
A.class,
B.class,
C.class,
D.class,
E.class,
F.class,
G.class,
H.class,
I.class,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b_activity_main);
AN= (Button) findViewById(R.id.button1);
PL= (Button) findViewById(R.id.button2);
lv = (ListView) findViewById(R.id.list_view);
autocomplete = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
clear = (Button) findViewById(R.id.clear);
clear.setVisibility(View.INVISIBLE);
animals_names = getResources().getStringArray(R.array.animals_names);
animalslist = new ArrayList<Main>();
for (int i = 0; i < animals_names.length; i++) {
Main country = new Main(i,animals_names[i], classes[i]);
animalslist.add(country);
}
adapter = new AnimalsAdapter(getApplicationContext(), animalslist);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, animals_names);
autocomplete.setAdapter(this.adapter);
autocomplete.setThreshold(1);
autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Main item = (Main) parent.getAdapter().getItem(position);
Intent d = new Intent(MainActivity.this, item.getClazz());
startActivity(d);
/** Fading Transition Effect */
MainActivity.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
autocomplete.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
//do nothing
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0) {
clear.setVisibility(View.VISIBLE);
} else {
clear.setVisibility(View.GONE);
}
}
});
autocomplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
clear.setVisibility(View.VISIBLE);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
clear.setVisibility(View.GONE);
}
});
}
public void clear(View view) {
autocomplete.setText("");
clear.setVisibility(View.GONE);
}
Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {