I have been following this guide and i have it running.
http://www.mkyong.com/android/android-tablayout-example/
I have added a bunch of new widgets to the tabs using java (image views and text fields) but its getting messy and i dont have as much control over the positioning and size as i would like to have since i cant use layouts
If i created a new layout using xml and i placed all my widgets perfectly using layouts etc is there a way i can populate a specific tab with that layout using java?
eg
rather than have code like this to add widgets
TextView textview = new TextView(this);
textview.setText("This is Android tab");
setContentView(textview);
i can just add a reference to an xml file and it will add all them widgets within the xml file
Use LayoutInflater http://developer.android.com/reference/android/view/LayoutInflater.html
LayoutInflater inflater = LayoutInflater.from(context); // if you're inside of activity then context = this
View viewInflatedFromXml = inflater.inflate(R.layout.your_layout, null);
Then you can do what you want with this view.
Related
I want to create a "focus" effect once the user clicks an item in the recycler view. Look at the image below, the second one is the effect I wish to have.
I've tried to get the RecyclerView's clicked item and bring it to the front and show a dark/transparent overlay covering all the others.
This is my view hierarchy:
RelativeLayout
--- Recycler View
--- View (dark overlay)
I've tried with view.bringToFront();, view.setZ();, view.setElevation(); but none of them works.
Now I guess this is about the layout hierarchy. How to solve this problem?
you can just change the background color of touch item view and text color. when user click on it .
I achieved this effect by sending the item view to the Window.DecorView.
You can just make a copy of the desired view you want to put in the front (in this case you want the layout used by the ViewHolder) and then
((ViewGroup)activity.getWindow().getDecorView()).addView(yourView);
after that you can set Translations according to the original view. The new view will be in front of the dark overlay in the same place the previous view is
EDIT: I have a sample of what you can do:
The following code is C# (using Xamarin.Android), it should be easy to turn to Java.
var productView = productsLayoutManager.FindViewByPosition(position);
var cardView = productView.FindViewById<CardView>(Resource.Id.product_layout);
int[] location = new int[2];
productView.GetLocationOnScreen(location);
int left = location[0];
int top = location[1];
selectedView = LayoutInflater.Inflate(Resource.Layout.ProductRow2, null);
productsAdapter.CloneItemViewHolder(productView, ref selectedView);
selectedView.LayoutParameters = new FrameLayout.LayoutParams(cardView.LayoutParameters);
((ViewGroup)activity.Window.DecorView).AddView(selectedView, cardView.Width, cardView.Height);
selectedView.SetX(left);
selectedView.SetY(top);
selectedView.RequestLayout();
activity.DimBackground();
The cardView is the main layout of the RecyclerView item Viewholder.
This is actual code in use, producing this:
I have a class "HomeActivity.class" and its layout "activity_home". I need to change the text of a TextView ("TV8") in another layout ("layout_profile"). I tried to use:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
but my app crush when try to set text at line 4.
Maybe the problem can be the inflated layout: in activty_home layout i have a frame layout that inflate layout_profile where is my textview TV8:
activity_home:
layout_profile :
Debug application
HOW CAN I CHANGE THE TEXT OF A TEXTVIEW (TV8) IN A LAYOUT(layout_profile) FROM A CLASS(activity_home) THAT HAVE ANOTHER LAYOUT(activty_home)??
I think there is an issue in line 3.
Try this:
setContentView(R.layout.activity_home);
ConstraintLayout profile=(ConstraintLayout)findViewById(R.id.layout_profile);
TextView TV8 = (TextView) profile.findViewById(R.id.TV8);
TV8.setText("MY TEXT IN TV8");
You are loading activity_home.xml in the Activity and try to access the view TV8 which is not in activty_home.xml.
R.id.TV8 is part of layout_profile not part of activity_home.xml.
You can access those view which is in layout which is currently loaded in the screen.
Your activity is loading the activity_main layout so you can access the views that are present in the layout.
You are trying to access the view present in layout_profile which is not loaded and not visible. Until its loaded you can't access the view.
And if the view is not visible there is no point changing the text of that view.
Rather you can store the value in a flag and can be used to change the text when layout_profile is loaded.
I'm currently working on creating a custom view which uses a xml layout file to render the view on the canvas. In this file I have a FrameLayout that is meant for dynamic content. I'm trying to change the content and in the custom view class with this method.
private void setFragment(Fragment fragment)
{
((ActionBarActivity) getContext()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_container, fragment)
.commit();
}
The problem with this is that I'm using the FragmentManager from the Activity this view is embedded in. Because of this the FragmentManager is referencing the Activity's layout file which does not contain my FrameLayout. I need this to be able to modify the content in the custom view's layout file so it can access the FrameLayout and change the content.
How can I go about accomplishing what I want? Any idea?
use callback pattern like here. But define an interface in your view class and implement it in activity.
Check out this picture:
WhoKnew title and WK? Icon http://puu.sh/5Ejo5.png
See the icon and title there? How do I go about removing that?
I'm not using any of the drag and drop or xml editing at all in eclipse for this project, because I have to dynamically create an arbitrary amount of buttons/text, and whatnot.
Basically, I've been going about it by doing something like this..
public createMainMenu(){
//make layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(backgroundColor);
//make/add text to layout
menuText.setText("Who Knew?");
menuText.setId(20001);
menuText.setTextSize(36);
menuText.setTextColor(textColor);
layout.addView(menuText);
//make a scrollview, then add the layout to the scrollview
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(layout);
//set content view to the scrollview
setContentView(sc);
}
Hopefully, I'm on the right track here on how I should be going about this. The app I have is working fine, I just need to get rid of that icon/title bar, if possible. I guess it's not a gamebreaker, but it's a bit annoying.
you can use this in your java class
requestWindowFeature(Window.FEATURE_NO_TITLE);
Add this to your manifest:
android:theme="#android:style/Theme.Light.NoTitleBar"
Say I add a header view to my list view using the typical method like so:
View header = getLayoutInflater().inflate(R.layout.list_header, null);
TextView headerText = (TextView) header.findViewById(R.id.my_textview);
headerText.setText("This is my header!");
myListView.addHeaderView(header);
myListView.setAdapter(adapter);
Then, later I need to alter the text of the header textview...
TextView headerText = (TextView) findViewById(R.id.my_textview);
headerText.setText("new header text!");
This doesn't appear to work, since the way I originally attached the header to the list was by inflating it...
How do I change the text?
You should simply store the reference to headerText that you used originally. Then call setText on it later.
Where are you executing the code to change the header text? If your not doing it on the UI thread the textview won't update.