How can I set an setOnClickPendingIntent for my entire widget layout? - java

I'd like to set an setOnClickPendingIntent for my entire widget layout but I haven't found a a way to do this. I guess it's something very trivial but I've overlooked it. Currently I'm setting an intent for each of my views in the layout and this makes my code very messy. Here's what I'm doing currently:
remView = new RemoteViews(ctxContext.getPackageName(), R.layout.initial);
Intent ittRetry = new Intent(ctxContext, SettingsActivity.class);
ittRetry.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
ittRetry.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, intInstance);
PendingIntent pitRetry = PendingIntent.getBroadcast(ctxContext, 0, ittRetry, PendingIntent.FLAG_UPDATE_CURRENT);
remView.setOnClickPendingIntent(R.id.widget_rows, pitRetry);
remView.setOnClickPendingIntent(R.id.widget_message, pitRetry);
wigManager.updateAppWidget(intInstance, remView);

Currently I'm setting an intent for each of my views in the layout and this makes my code very messy
However, that is your only option. You have to call setOnClickPendingIntent() for every widget for which you want to get control in a click.
You are welcome to use a loop, putting your widget IDs in an array and iterating over that array, calling setOnClickPendingIntent() in each pass.

You can create a transparent button on top of all your widget views at the end of your layout xml that covers the whole widget (match_parent is important!):
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
And reference it in your widget code:
[...]
views.setOnClickPendingIntent(R.id.button, pendingIntent);
[...]

My solution for this issue (years after the original question) is to set an id for the master layout. So my layout has a RelativeLayout and this has an id, which I can use for the pending intent.

Related

How to change the action bar color and open a file on click?

I'm working now on an app for Android and there are two things that I don't know how to do.
I want to change the colour of the action bar at the request of the user, so I want to change it dynamically. I see some answers that say that I need to use:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")));
But for some reason, it does not work for me.
I've added to my app option to download a file, but I do it in an unusual way so it does not create a notification when the download is complete. I want to give my user an easy way to get to the file. Can I create a button that opens the file when clicked?
Thanks!
1) For that I would recommend you having Example here Toolbar View and set it as your Actionbar. Just add this view at the top:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And then in Activity
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
After this you can change everything you want to the view myToolbar. Set background color etc. It will work. Using just Actionbar is old and depricated approach.
2) Yes you can, it is like opening new Activity with intent. Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.
Suppose If you have file type which categorized in document type you can use,
ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)
or If you going to handle some media file (Audio/Video)
you can use,
ACTION_VIEW
To identify MIME_TYPE of specific file you can use function
guessContentTypeFromName (String url)Link
Or getMimeTypeFromExtension(String extension)Link
Hope this helps :)
You can't change the color because you should call getSupportActionBar() and it's also for api >11;
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)‌​));
or
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")​));
About your second question, we need more info and your code where you download file. You totally can access files onClick but you need to know the file type and the directory where you store the files and target api to check runtime permissions for example
I suggest you ask another question with all your code and this info

How to change background of the app?

Is there a way to change background directly in the app? (!not homescreen wallpaper)
You need to specify the layout that its background will be changed. I guess you have a LinearLayout, so add an id to the layout (if it is not exist):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=#+id/parentLayout>
Then you can change the background in Java with:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.parentLayout);
linearLayout.setBackgroundResource(R.drawable.new_background);
Hope it helps you!
i have a link for you is easy to change background app.
Try something like that https://www.youtube.com/watch?v=mDKuWIlSNJE

Facebook LikeView change button size

I am using a LikeView in my android application and want to change the button size. Changing the view's size using layout parameters or the width/height at addView() does not help.
I'm using libGDX and have never worked with XML + libGDX, so a way to do it with code only would be nice!
Does anyone know how to do this? Currently I use the following code to create my LikeView:
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
application.layout is a RelativeLayout
Try to create your LikeView from XML (or inflate this layout at runtime):
<com.facebook.widget.LikeView
android:id="#+id/button_like"
android:layout_height="31dp"
android:layout_width="wrap_content" />
I finally fixed it using setScale on the view. It's not the most elegant solution, but it does the job.
likeButton.setScaleX(1.8f);
likeButton.setScaleY(1.8f);

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

Create a Custom ImageButton Class with FrameLayout and TextView

I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound

Categories

Resources