How to combine a layout and a view? - java

I am making an 2D Android game with Android Studio and I have a activity_customer.xml file and I have a MyView a
but when I use like this, MyView a isn't seen on the screen but activity_customer.xml is seen on the screen :
Customer.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView a = new MyView(this);
setContentView(a);
setContentView(R.layout.activity_customer);
}
But if I use like this,MyView a is seen on the screen but activity_customer.xml isn't seen on the screen :
Customer.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView a = new MyView(this);
setContentView(R.layout.activity_customer);
setContentView(a);
}
So I want to seen both MyView a and activity_customer.xml ,How can I combine them in a one screen?
This is my activity_customer.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.asdf.catchtheball.main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Button" />
</RelativeLayout>

if you created your own view class you can add it to your activity view with something like this:
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.relativeLayout); //Your main element at activity's xml file
mainLayout.addView(yourView); // replace your 'a' object with 'yourView'

Related

Change TextView on content_main.xml in MainActivity.java

I'm using the Android Studio standard template "Navigation Drawer Activity" which contains the content_main.xml layout.
On this layout is my textView which I want to change the text from my MainActivity.java.
I tried following but nothing changes:
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.content_main, null);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/relativelayout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.w4ter.ledcontroldesign.MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView
android:text="Presets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:id="#+id/txt_head" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#61000000" />
</LinearLayout>
there is no need of LayoutInflater. Simply use:
TextView textView = (TextView) findViewById(R.id.txt_head);
texView.setText("Hello");
Hope this helps.
You just need to access the TextView component the normal way. With that template the content_main.xml is included inside the main layout, so you sould do as follows in your main activity (maybe inside the onCreate method or wherever you need):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
}
//...
}
Assuming that you have a app_bar_main.xml inside the main layout wich contains the content_main.xml. In the end everything is accessible from the main activity directly since the layouts are included in the main layout.
Hope it helps!

Code referencing internal library not working

I am trying to make my first library to learn and see how things work. What is supposed to happen is that my main activity (in the sample or whatever app uses the local library) will show a loading animation from the library. But when I add in the code, it does not show in the sample. I'm not getting any errors or anything. What could be going on?
Sample Main Activity:
public class MainActivity extends AppCompatActivity {
private ProgressBar spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//This is what isnt working. The spinner
spinner = (ProgressBar)findViewById(com.marlonjones.ezloadanimlibrary.R.id.main_progress);
library XML:
<RelativeLayout
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">
<ProgressBar
android:id="#+id/main_progress"
android:layout_height="50dp"
android:layout_width="60dp"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/main_ind_progress"/>
</RelativeLayout>
main_ind_progress:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/loader_main"
android:pivotX="50%"
android:pivotY="50%"/>
The drawable main_ind_progress, and the drawable loader_main are both inside of the library.
All code: https://github.com/MJonesDev/EZLoadAnimLibrary
The ProgressBar view isn't added to your current Activity i.e. you have no view of the type ProgressBar that is why it isn't showing anything
You can assign an id to the root RelativeLayout in the content_main layout file of your current Activity then in the OnCreate() method
RelativeLayout rL = (RelativeLayout) findViewById(R.id.your_id);
// and then add your spinner to the relative layout
rL.addView(snipper);
or what you can do is make a ProgressBar in your current app's content_main.xml file
<ProgressBar
android:id="#+id/my_progress_bar"
android:layout_height="50dp"
android:layout_width="60dp"
android:layout_centerInParent="true"
android:indeterminate="true"
/>
and assign it the main_ind_progress to it from your library
ProgressBar spinner=(ProgressBar) findViewById(R.id.your_progress_bar_id);
spinner.setIntermediateDrawable(com.marlonjones.ezloadanimlibrary.R.drawable.main_ind_progress);

viewPager not displaying fragment

for this project, I have tabs that have to display something under them and i'm using view pager but for some reason, the fragment layout isn't showing up on screen. I get a blank screen but there is no error or anything. I have been googling and searching for hours now and even started a new project just to make sure it was not a build error but still no luck in finding a solution to my problem.
I have done examples like this many times and used the exact same steps but for some reason, it just isn't working this time. Any help would be greatly appreciated
Here is my mainActivity class
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
ViewPager myViewPager;
SlidingTabLayout mySlidingTabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myViewPager = (ViewPager) findViewById(R.id.viewPager);
myViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), this));
mySlidingTabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mySlidingTabs.setViewPager(myViewPager);
}
}
Here is my custom adapter class
public class MyPagerAdapter extends FragmentPagerAdapter {
private Context context;
private String[] tabNames = {"Beverages", "Deli & Bakery", "Fresh Produce", "Health & Beauty", "Meat & Seafood", "Natural Foods", "Pet Care"};
public MyPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new Beverages();
}
else if (position == 1) {
return new DeliBakery();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
}
Here is my main activty.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">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<!-- The Sliding tab -->
<bello.myapplication.SlidingTabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sliding_tabs"/>
<!-- The pager that allows us to swipe between fragments -->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</LinearLayout>
and here is one of my tab's xml file
<?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">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab fragment 1"
android:textSize="50dp"
android:padding="5dp" />
</RelativeLayout>
Here is one of my fragment classes
public class Beverages extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.beverages_tab, container, false);
return view;
}
}
When something strange like this happens, try to use Log.i("title", "message") to track if every class/methods are being called.
In your case, it seens that your viewpager isn't in the screen.
If I understood your code, your <include> tag is including the second xml code you posted right?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab fragment 1"
android:textSize="50dp"
android:padding="5dp" />
</RelativeLayout>
It looks like your layout_height="match_parent" is filling all the space on your screen, giving no space to your ViewPager being inflated by the system, try to change it to layout_height="wrap_content and you should be done.
If not, try to change the background of your ViewPager to check if it is on the screen and is not being hided by something else.
Figured out the problem. The viewpager background color and the background color of the tabs where the same so it made it look like there was a black activity. So for anyone else out there with the same problem, try changing either the text color of your tabs.xml or change the background of your viewpager

Android Wear NullPointerException can't set text on TextView

I am trying to write an Android Wear application. For now I've got a TextView and would like to set the text programmatically.
private TextView mTextView;
private TextView text;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
text = (TextView)findViewById(R.id.text);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
}};
text.setText("test");
}
When trying to run the application like that I get a nullpointerexception at
text.setText("test");
That led me to assume that the TextView "text" was not found. Normally on Android phone applications it is just fine to call the
text = (TextView)findViewById(R.id.text);
Directly after
setContentView();
But now there are things like the WatchViewStub and the setOnLayoutInflatedListener. Maybe there's something I've overseen?
However, how do I specify my TextView correctly on the Android Wear platform?
Edit:
activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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:id="#+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/rect_activity_my"
app:roundLayout="#layout/round_activity_my"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
rect_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MyActivity"
tools:deviceIds="wear_square">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
round_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MyActivity"
tools:deviceIds="wear_round">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
According to code from your Activity:
private TextView mTextView;
private TextView text;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
text = (TextView)findViewById(R.id.text);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
}};
text.setText("test");
}
Why do you have two TextViews? You have only one either in your rect_activity: or round_activity: layouts.
You have to understand how WatchViewStub works. You cannot access views from your rect/round layouts right after setContentView because they are not inflated yet. Like you did - you can access your views only in OnLayoutInflatedListener listener callback.
You have tried to find your mTextView inside this callback - which is the way you should do it.
What is wrong?
You should remove following lines:
text = (TextView)findViewById(R.id.text);
and this one: (it also causes your NullPointerException)
text.setText("test");
and keep only the mTextView field.
What should you do?
Add following like right after mTextView = (TextView)stub.findViewById(R.id.text);:
mTextView.setText("test");
It must be incide the OnLayoutInflatedListener.
Your final code should look like:
private TextView mTextView;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
mTextView.setText("test");
}};
}
You do not have TextView in activity_my.xml
text = (TextView)findViewById(R.id.text);
if rect_activity.xml and round_activity.xml are for different activities then you need to create new activities and call setContentView for those xml
Only after that you can use the views in your code.
Instead of adding an OnLayoutInflatedListener you can extend InsetActivity and set your content view in InsetActivity.onReadyForContent():
import android.support.wearable.activity.InsetActivity;
public class MainActivity extends InsetActivity {
...
#Override
public void onReadyForContent() {
setContentView(R.layout.activity_main);
someTextField = (TextView) findViewById(R.id.some_field);

Android ActionBar Tab Layout Not Full Width

I'm having some issues with the Actionbar tab layout. For some the tabs do not stretch across the full width of the device which is weird because after trawling through at least 10 examples all 10 had centered Actionbar's the full width of their devices. Perhaps it has something to do with testing on a tablet I'm not sure. Here's the an image of the problem as well as my code.
Thanks.
Container:
public class ContainerActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab()
.setText("Mat")
.setTabListener(new TabListener<MatchesFragment>(
this, "Match", MatchesFragment.class));
actionBar.addTab(tab);
...
The Container xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
Fragment:
public class MatchesFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.activity_matches, container, false);
return myFragmentView;
}
Fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="fill_horizontal"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/report_tab" />
</LinearLayout>
Try using a Global Layout Listener:
declare:
int requestLayoutCounter = 0;
inside onCreateView (rootView is the main View):
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new MyGlobalListenerClass());
Give some id to the container:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
then declare the OnGlobalLayoutListener for the rootView inside the Fragment:
private class MyGlobalListenerClass implements ViewTreeObserver.OnGlobalLayoutListener {
#Override
public void onGlobalLayout() {
View v = (View) rootView.findViewById(R.id.container);
Display display = this.getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int[] dimension = new int[2];
dimension[0] = size.x; //width of the screen
dimension[1] = size.y; //height
v.getLayoutParams().width = (displaySize[0]);
if (requestLayoutCounter < 25){ //weird code but works for me on different devices
topLayout.requestLayout();
requestLayoutCounter++;
}
}
}
this code will change the width of the container after the layout has been inflated, or after you change the content or orientation, let me know if it worked for you

Categories

Resources