Showing Toast when clicking on textview - android

In this example, I am trying to click on each of the 2 text views (mLayout1 and mLayout2),
and a toast will show up. The code compiles with no error, but when running there is no toast showing up.
Here is my code:
public class MainActivity extends ActionBarActivity {
private TextView mLayout1, mLayout2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.activity_main,
mLayout1 = (TextView) view.findViewById(R.id.item_1a);
mLayout1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"mLayout1 clicked",Toast.LENGTH_SHORT).show();
}
});
mLayout2 = (TextView) view.findViewById(R.id.item_1b);
mLayout2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"mLayout2 clicked",Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
and here is my xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.transitionexample.MainActivity" >
<TextView
android:id="#+id/item_1a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item 1a"
android:background="#color/green"
android:gravity="center"/>
<TextView
android:id="#+id/item_1b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:gravity="center"
android:background="#color/opaque_red"
android:text="Item 1b" />
</RelativeLayout>

your onCreateView is never called - this is not a fragment - add the #Override annotation and you will see ...

Try this:
android:clickable="true"
:
<TextView
android:clickable="true"
android:id="#+id/item_1a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item 1a"
android:background="#color/green"
android:gravity="center"/>

Related

Why is this ListView null?

I'm completing part of the Google Android Tutorial (this part to be precise) and I've hit a snag. I'm trying to set the Adapter of a ListView to a custom ArrayAdapter. Unfortunately, findViewById seems to be returning null (which means I can't set the adapter).
Here's the relevant code:
MainActivityFragment
private final String LOG_TAG = MainActivityFragment.class.getSimpleName();
private ArtistItemAdapter artistsAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
artistsAdapter = new ArtistItemAdapter(getActivity(), R.layout.list_item_artist, new Pager<Artist>().items);
ListView artistsList = (ListView) rootView.findViewById(R.id.listViewArtists);
artistsList.setAdapter(artistsAdapter);
return rootView;
}
#Override
public void onStart() {
EditText searchBar = (EditText) getView().findViewById(R.id.searchEditText);
searchBar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
ArtistsPager artists;
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
FetchArtistsTask artistsTaskCall = new FetchArtistsTask();
artistsTaskCall.execute(String.valueOf(v.getText()));
handled = true;
}
return handled;
}
});
}
MainActivity.onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
fragment_main.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchEditText"
android:inputType="textFilter"
android:imeOptions="actionSearch"
android:drawableLeft="#android:drawable/ic_menu_search"
android:hint="Search Artist"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listViewArtists"
android:layout_below="#+id/searchEditText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.example.android.spotifyproject.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
LogCat: http://pastebin.com/CUNF05Af
It's not the ListView that is null, it's the data you sent into the Adapter when you created it, so when you try to set the Adapter on the ListView it calls List.size() on the data(null), and that's where the error is.

Overlapping fragment with activity view

I am following a fragment example https://gist.github.com/daichan4649/2947119.
I am adding the fragment dynamically after button click event.But the fragment is overlapping with the button. And when I am pressing back key on this screen, activity is closed.
Need help to display only the fragment after button is clicked and if the back is pressed after adding fragment, then only the top fragment should be closed.
Here are my test code.
MainActivity.java
public class MainActivity extends FragmentActivity {
private static Button mBtn;
private static final String TAG_LIST = "list";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
return;
}
mBtn = (Button) findViewById(R.id.clickme);
mBtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = getFragmentManager().findFragmentByTag(TAG_LIST);
if (fragment == null) {
fragment = TestListFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, fragment, TAG_LIST);
ft.commit();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TestListFragment.java
public class TestListFragment extends ListFragment {
public static TestListFragment newInstance() {
return new TestListFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(android.R.id.content, container, false);
return rootView;
}
#SuppressLint("NewApi")
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text);
setListAdapter(adapter);
adapter.addAll(createDataList(10));
}
private static List<String> createDataList(int counts) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < counts; i++) {
list.add("i=" + i);
}
return list;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Toast.makeText(getActivity(), "List Item Clicked", Toast.LENGTH_SHORT).show();
//On item click, launch the DialogFragment
TestDialogFragment moveFragment = new TestDialogFragment( );
moveFragment.show(getFragmentManager(), "Test List Fragment");
}
TestDilogFragment.java
public class TestDialogFragment extends DialogFragment {
private View testDialogFragment = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Fill the layout as per Your requirement
testDialogFragment = inflater.inflate(R.layout.activity_list_fragment, container);
return testDialogFragment;
}
}
activity-main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/clickme"
android:text="ClickMe" />
</RelativeLayout>
activity_list_fragment.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Change your activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parentt" >
<Button
android:id="#+id/clickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<FrameLayout
android:id="#+id/container"
android:layout_above="#id/clickme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</FrameLayout>
</RelativeLayout>
You have a Button at the bottom and a ViewGroup which is a FrameLayout at the top. You add the Fragment to the container.
And in ManiActivtiy change to
ft.add(R.id.container, fragment, TAG_LIST);

Android opOptionsItemSelected menu doesn't work

I have a problem with a menu in my Android app: it displays when I click the button, but when I try to click the option which will start another activity, it does nothing. It doesn't crash, either.
Here's the code for the menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And here's the full code of my activity:
public class EventList extends Activity implements OnClickListener {
ImageView bookmark;
ImageView palau;
ImageView noimage;
TextView nom;
TextView cognom;
TextView data;
TextView descripcio;
Button map;
Button join;
Context ct;
ItemEvent item;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
item = (ItemEvent) getIntent().getSerializableExtra("item");
ct = this;
Toast.makeText(this, item.getNom(), Toast.LENGTH_LONG).show();
element_confi(); //line 63!
}
public void element_confi() {
bookmark = (ImageView) findViewById(R.id.bookmark);
nom = (TextView) findViewById(R.id.nom);
data= (TextView) findViewById(R.id.data);
descripcio = (TextView) findViewById(R.id.descripcio);
map = (Button) findViewById(R.id.mapa);
join = (Button) findViewById(R.id.join);
nom.setText(item.getNom());
data.setText(item.getDate());
descripcio.setText(item.getDescription());
if( item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1); //line 80!
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
map.setOnClickListener(this);
join.setOnClickListener(this);
ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(item.getRutaFoto1(),item.getRutaFoto2());
viewPager.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mapa:
Toast.makeText(ct, "mostrar mapa", Toast.LENGTH_LONG).show();
break;
case R.id.join:
Intent apuntar = new Intent(ct, JoinEvents.class);
startActivity(apuntar);
break;
}
}
private class ImageAdapter extends PagerAdapter {
private int[] mImages;
public ImageAdapter(int image1Recurso, int image2Recurso){
mImages = new int[] { image1Recurso,
image2Recurso,
};
}
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = EventList.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
Thank you very much for your help.
Yours sincerely,
Mauro.
EDIT:
Settings.java
(Some spots are pending polishing: don't mind them)
public class Settings extends Activity implements OnItemSelectedListener, OnClickListener {
CheckBox view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//onCheckBoxClicked();
//onRadioButtonClicked();
Spinner_Menu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
//checkbox
public void onCheckBoxClicked (View v)
{
view = (CheckBox)findViewById(R.id.Setting1);
boolean checking = ((CheckBox) v).isChecked();
switch (view.getId())
{
case R.id.Setting1:
if (checking)
{
// //checkbox checked!
}
else
{
//not checked!
}
break;
}
}
//Menú desplegable
public void Spinner_Menu()
{
Spinner spinner = (Spinner) findViewById(R.id.Settings2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onRadioButtonClicked(View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch (view.getId())
{
case R.id.Setting3_1:
if (checked)
{
//Option 1
}
break;
case R.id.Setting3_2:
if (checked)
{
//Option 2
}
break;
}
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
And the layout for EventList:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EventList" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
</ListView>
</LinearLayout>
<TextView
android:id="#+id/nom"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom Event"
android:layout_below="#+id/linear"
/>
<ImageView
android:id="#+id/bookmark"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/favorito"
android:layout_below="#+id/nom"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_below="#id/bookmark"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/view_pager"
android:text="TextView" />
<Button
android:id="#+id/mapa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/data"
android:text="Com arribar" />
<Button
android:id="#+id/join"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mapa"
android:text="Join event" />
<TextView
android:id="#+id/descripcio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/join"
android:text="Descripció event" />
</RelativeLayout>
Just in case, the Settings.xml file too:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Settings" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting1"/>
<CheckBox
android:id="#+id/Setting1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox1"
android:layout_below="#+id/linear1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear1">
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting2"/>
<Spinner
android:id="#+id/Settings2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Desplegable"
android:layout_below="#+id/linear2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear2">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting3"/>
<RadioButton
android:id="#+id/Setting3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció1"
android:layout_below="#+id/linear3"/>
<RadioButton
android:id="#+id/Setting3_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció2"
android:layout_below="#+id/Setting3_1"/>
</LinearLayout>
</RelativeLayout>
Thank you very much.
Try adding this to EventList.java:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.settings:
Intent settingsIntent = new Intent(this, Settings.class);
startActivity(settingsIntent);
return true;
}
}
can you please paste your event.xml as well as is Settings.class extending PreferenceActivity ?
Also this is happening sometimes if your are using eclipse. Just clean try to clean and rebuild your project.
if it wont work, then try:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.createOnOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.event, menu);
return true;
}

How to implement click listener for a button in android Page Viewer?

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device.
I have multiple layout in xml like main.xml, layout_first.xml, layout_second.xml and layout_third.xml
Now I have a simple button in my layout_first.xml, I want to implement a click listener for the button like
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
No I don't know where to put the above code
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myfivepanelpager"/>
</LinearLayout>
Here is my layout_first.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" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Here is my layout_second.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" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my layout_third.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" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my java code
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 3;
}
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position)
{
case 0:
resId = R.layout.layout_first;
break;
case 1:
resId = R.layout.layout_second;
break;
case 2:
resId = R.layout.layout_third;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
After View view = inflater.inflate(resId, null); add the following:
if(position == 0){
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}

Why Custom Control is not visible in activity_main.xml

This is the new code, It doesn't throw exceptions, but I can't see my custom control "Second". This is the new code:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<Button
android:id="#+id/addButton"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="Add" />
<EditText
android:id="#+id/newEditText"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_weight="3.24"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:id="#+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
This is the custom control: second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secodView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/expandButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.44"
android:text = "Expand"/>
<TextView
android:id="#+id/txtView"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:layout_weight="0.56"
android:ems="10"
android:text = "LOL"/>
</LinearLayout>
<RadioGroup
android:id="#+id/ratingRadioGroup"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/likeButton"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="Like" />
<RadioButton
android:id="#+id/dislikeButton"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:text="Dislike" />
</RadioGroup>
</LinearLayout>
Second.java class:
public class Second extends ViewGroup
{
private Button mExpandButton;
private RadioButton mLikeButton;
private RadioButton mDislikeButton;
private TextView mText;
private RadioGroup mLikeGroup;
private ViewGroup vGroup;
OnClickListener myClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
String s= mExpandButton.getText().toString();
if (s.equals("One Click"))
mExpandButton.setText("Other Click");
else
mExpandButton.setText("One Click");
}
};
OnCheckedChangeListener myCkeckListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (mLikeButton.isChecked())
mText.setText("I Like it");
if (mDislikeButton.isChecked())
mText.setText("I Don't Like it");
}
};
public Second(Context context)
{
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = LayoutInflater.from(context).inflate(R.layout.second, this, true);
mText = (TextView)this.findViewById(R.id.txtView);
mExpandButton = (Button) this.findViewById(R.id.expandButton);
mLikeGroup = (RadioGroup)this.findViewById(R.id.ratingRadioGroup);
mExpandButton.setOnClickListener(myClickListener);
mLikeGroup.setOnCheckedChangeListener(myCkeckListener);
//inflater.inflate(R.layout.second, null);
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4)
{
// TODO Auto-generated method stub
}
}
ActivityMain.java class, where the activity gets started:
public class MainActivity extends Activity
{
protected LinearLayout mList;
protected EditText mEditText;
protected Button mButton;
Second[] sec;
boolean unavez;
OnClickListener myClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (LinearLayout)findViewById(R.id.List);
mEditText = (EditText)findViewById(R.id.newEditText);
mButton = (Button)findViewById(R.id.addButton);
sec = new Second[4];
unavez = true;
for (int i=0; i<4; i++)
sec[i]= new Second(this);
}
#Override
protected void onStart()
{
super.onStart();
}
#Override protected void onResume()
{
super.onResume();
if (!unavez)
return;
for (int i=0; i<4; i++)
{
mList.addView(sec[i]);
//setContentView(sec[i]);
}
unavez = false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
When running, I can see the Button and the EditText in activity_main.xml, but I can't see my custom control in second.xml. If I use the line setContentView(sec[i]) instead of mList.addView(sec[i]) in onResume(), it gets worse: the EditText and the Button in activity_main.xml aren't visible and I get a blank layout.
How can I add my custom control to activity_main.xml and make it visible to user?
You elected to have Second inherit directly from ViewGroup. Since you did that, Second needs to be a real ViewGroup, with a real onLayout() implementation and everything else that goes along with being a ViewGroup. Do not just randomly override methods with do-nothing implementations, then complain when the do-nothing implementations do nothing.
Creating "custom controls" is a relatively advanced topic in Android. Newcomers to Android should focus on other solutions to whatever problem they think a "custom control" will somehow solve.

Categories

Resources