Hi.
I'm new in Android Studio programming is and I am learning on my own.
I am making a tabLayout example, as follows:
activity_main.xml
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sin Venta"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2"
android:ems="20"
android:inputType="textPersonName"
android:singleLine="true"
android:text="$0.00" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="10dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
The xml of the first tab is as follows:
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:text="1"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/Boton_1"
android:layout_weight="1"
android:textSize="@android:dimen/app_icon_size" />
<Button
android:text="2"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/Boton_2"
android:textSize="@android:dimen/app_icon_size" />
<Button
android:text="3"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/Boton_3"
android:textSize="@android:dimen/app_icon_size" />
</LinearLayout>
...
etc.
</LinearLayout>
The code MainActivity is as follows:
package com.truiton.designsupporttabs;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Teclado"));
tabLayout.addTab(tabLayout.newTab().setText("Surtido"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// -------------------------------
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
*My intention is to modify the text property of the TextView object that is
in MainActivity from the first Tab, f**or which the code in the first tab
is as follows:*
package com.truiton.designsupporttabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class TabFragment1 extends Fragment
{
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.activity_main, null, true);
colocarTexto();
return view;
}
public void colocarTexto()
{
try
{
TextView CajaDolares = (TextView)
view.findViewById(R.id.textView2);
CajaDolares.setText("Hello");
}
catch (Exception ex){
Mensaje.mostrar("Error!", ex.getMessage(), "¿Y ahora?",
this.getActivity(), true);
Toast.makeText(this.getActivity(), ex.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}
But when trying to change the TextView text, something nonsense appears as
shown in the following image:
<https://lh3.googleusercontent.com/-i9B5QLdQlrA/WD2N_SkdtzI/AAAAAAAAAMk/7xFkCpyqbZU8hQdn-ouITbyQxNOoKIszwCLcB/s1600/Error%2B1.jpg>
Without trying to modify TextView text:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
view = inflater.inflate(R.layout.tab_fragment_1, container, false);
//view = inflater.inflate(R.layout.activity_main, null, true);
//colocarTexto();
return view;
}
The app would look like this:
<https://lh3.googleusercontent.com/-Aw3ff2VaS60/WD2QRxnUY3I/AAAAAAAAAM0/m5y526VVgg8wAtHMhSe3E4RVVSCEwHY1wCLcB/s1600/Error%2B2.jpg>
Any idea or suggestion or welcome.
Thanks.
Fabián
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/2afa40cd-aaa4-48ef-a902-1e3afe6c6b77%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.