Android - Get Other Layout Widget - java

I've been trying to get a widget from another layout for past hours but still no luck. Can someone tell what is wrong with my current code?
public void refreshClicked(View view){
tab1 = LayoutInflater.from(view.getContext()).inflate(R.layout.tab_1, null);
Button b = (Button) tab1.findViewById(R.id.refresh);
b.setText("Updating...");
}
I've hooked my button onClick to refreshClicked(View view) method.
This method is under my MainActivity.java and I'm trying to get the Button with id refresh from tab_1.xml layout but the button text is not updating.
Thanks!

You seems to inflate a layout but never attach it to a parent view as the ViewGroup parameter is null. Then this layout inflated must not even been displayed anywhere.
If the button b already exists, no need to inflate the layout and retrieve the button form there, it will a different button.
Just retrieve the button from the actual existing layout.

Related

How to setOnClickListener to a button inside a ListView that also have the id of the item

I am creating an app that tracks the inventory for a shop.
The layout consists of a ListView that has a sale button for each item, and on clicking the sale button, the quantity of the item should be decreased by one and be updated in database.
So my question is how do i set the onClickListener to the button so that it also receives the id of the item for which the sale button was clicked for, as it will enable me to update only the specific item in the database.
This is an sample example
The Button in the fxml file must have an fx:id so that you can use the button properties in the code behind
it looks like this
<Button fx:id="myButton"/>
and then you can use it (e.g. in your controller) like that
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO
}
});
please check this also setOnClickListener

Focus effect in a RecyclerView - how to bring to front an item?

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:

How to Scroll Down Linear Layout When linearLayout is visible on button click [kotlin]

I have bunch of text view and edit text form inside a scroll view and also having a linear Layout below form inside same scroll view in this linear layout having bunch of text view vertical. Initially linear layout has visibility gone when user press "add more info",which is aling just above this linear layout, button then layout visibility is Visible. This is working fine as expected but what i want is when linear layout get visible then scroll view should scroll down so that user can see the extended data. I tried all but nothing can help me.
Please help me to solve this.Below is ShowHideLayout method.
Need Help
fun showHideLayout() {
if (linearLayoutAddMoreInfoExpand.visibility == GONE) {
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollView.post(Runnable {
scrollView.fullScroll(ScrollView.FOCUS_DOWN)
//scrollView.scrollTo(0,scrollView.bottom)
})
// scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
scrollView.scrollTo(0,scrollView.bottom)
//scrollView.smoothScrollBy(R.id.linearLayoutAddMoreInfoExpand,R.id.scrollBottom)
} else if (linearLayoutAddMoreInfoExpand.visibility== VISIBLE) {
linearLayoutAddMoreInfoExpand.visibility = GONE
mTextViewAddMoreInfo.setText("+ Add More Info")
}
}
You can solve this adding another scrollview before your exapandLinearLayout in xml file and call this scrollview on button click so your whenever your linear layout is it will scroll down. But it may not be best practice.
Your code will be like below
<Scrollview
android:id = #+id/scrollViewLayout
.....
....>
<LinearLayout....>
<--Remaining view-->
</LinearLayout>
</Scrollview>
And in Java on addmoreinfo button click call this scrollview like below:-
fun showHideLayout() {
if(linearLayoutAddMoreInfoExpand.visibility == GONE){
linearLayoutAddMoreInfoExpand.visibility = VISIBLE
mTextViewAddMoreInfo.setText("Close Add More Info")
scrollViewLayout.post(Runnable { scrollView.fullScroll(ScrollView.FOCUS_DOWN) })

Change textview of another layout

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.

How to remove defaut icon/title from top of main layout in Android?

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"

Categories